https://bugzilla.redhat.com/show_bug.cgi?id=1107420
Add a new define/create flag VIR_NETWORK_DEF_PARSE_VALIDATE_NAME
to disallow new networks to be defined/created using a name
comprised entirely of spaces.
Alter the networkxml2xmltest to add a test in order to prove
the failure occurs.
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/conf/network_conf.c | 9 ++++++++-
src/conf/network_conf.h | 7 +++++++
src/network/bridge_driver.c | 6 ++++--
tests/networkxml2xmlin/network-whitespace-name.xml | 6 ++++++
tests/networkxml2xmltest.c | 4 +++-
5 files changed, 28 insertions(+), 4 deletions(-)
create mode 100644 tests/networkxml2xmlin/network-whitespace-name.xml
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 34d132e506..2139f61c82 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1604,7 +1604,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
xmlNodePtr vlanNode;
xmlNodePtr metadataNode = NULL;
- virCheckFlags(0, NULL);
+ virCheckFlags(VIR_NETWORK_DEF_PARSE_VALIDATE_NAME, NULL);
if (VIR_ALLOC(def) < 0)
return NULL;
@@ -1619,6 +1619,13 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
if (virXMLCheckIllegalChars("name", def->name, "/") < 0)
goto error;
+ if ((flags & VIR_NETWORK_DEF_PARSE_VALIDATE_NAME) &&
+ virStringIsEmpty(def->name)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("name must contain at least one non blank
character"));
+ goto error;
+ }
+
/* Extract network uuid */
tmp = virXPathString("string(./uuid[1])", ctxt);
if (!tmp) {
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 6373b783c6..53c187b075 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -293,6 +293,13 @@ enum {
virNetworkDefPtr
virNetworkDefCopy(virNetworkDefPtr def, unsigned int flags);
+typedef enum {
+ /* Perform extra name validation on new network names which
+ * will cause failure to parse the XML. Initially just that a
+ * name cannot be all white space. */
+ VIR_NETWORK_DEF_PARSE_VALIDATE_NAME = 1 << 0,
+} virNetworkDefParseFlags;
+
virNetworkDefPtr
virNetworkDefParseXML(xmlXPathContextPtr ctxt,
unsigned int flags);
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index eef0d83c10..33e28c3666 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -3608,8 +3608,9 @@ networkCreateXML(virConnectPtr conn,
virNetworkDefPtr def;
virNetworkPtr net = NULL;
virObjectEventPtr event = NULL;
+ unsigned int parse_flags = VIR_NETWORK_DEF_PARSE_VALIDATE_NAME;
- if (!(newDef = virNetworkDefParseString(xml, 0)))
+ if (!(newDef = virNetworkDefParseString(xml, parse_flags)))
goto cleanup;
if (virNetworkCreateXMLEnsureACL(conn, newDef) < 0)
@@ -3660,8 +3661,9 @@ networkDefineXML(virConnectPtr conn,
virNetworkObjPtr obj = NULL;
virNetworkPtr net = NULL;
virObjectEventPtr event = NULL;
+ unsigned int parse_flags = VIR_NETWORK_DEF_PARSE_VALIDATE_NAME;
- if (!(def = virNetworkDefParseString(xml, 0)))
+ if (!(def = virNetworkDefParseString(xml, parse_flags)))
goto cleanup;
if (virNetworkDefineXMLEnsureACL(conn, def) < 0)
diff --git a/tests/networkxml2xmlin/network-whitespace-name.xml
b/tests/networkxml2xmlin/network-whitespace-name.xml
new file mode 100644
index 0000000000..31d54985b4
--- /dev/null
+++ b/tests/networkxml2xmlin/network-whitespace-name.xml
@@ -0,0 +1,6 @@
+<network>
+ <name> </name>
+ <uuid>81ff0d90-c91e-6742-64da-4a736edb9a9b</uuid>
+ <bridge name="virbr0"/>
+ <mac address='12:34:56:78:9A:BC'/>
+</network>
diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c
index 7828995df1..42063f9904 100644
--- a/tests/networkxml2xmltest.c
+++ b/tests/networkxml2xmltest.c
@@ -32,8 +32,9 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
int ret;
testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS;
virNetworkDefPtr dev = NULL;
+ unsigned int parse_flags = VIR_NETWORK_DEF_PARSE_VALIDATE_NAME;
- if (!(dev = virNetworkDefParseFile(inxml, 0))) {
+ if (!(dev = virNetworkDefParseFile(inxml, parse_flags))) {
result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE;
goto cleanup;
}
@@ -160,6 +161,7 @@ mymain(void)
DO_TEST_PARSE_ERROR("passthrough-duplicate");
DO_TEST("metadata");
DO_TEST("set-mtu");
+ DO_TEST_PARSE_ERROR("network-whitespace-name");
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
--
2.17.1