]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virXMLParseHelper: Add root XML node name validation capability
authorPeter Krempa <pkrempa@redhat.com>
Wed, 14 Apr 2021 11:12:12 +0000 (13:12 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 19 Apr 2021 12:43:58 +0000 (14:43 +0200)
Some callers want to validate the root XML node name. Add the capability
to the parser helper to prevent open-coding.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virxml.c
src/util/virxml.h

index 83644af8ce356a2be7b6e40aa50722cbd6885095..d0d9494009955b670a13100c18ab63286486ac94 100644 (file)
@@ -1020,11 +1020,15 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
  * @filename: file to be parsed or NULL if string parsing is requested
  * @xmlStr: XML string to be parsed in case filename is NULL
  * @url: URL of XML document for string parser
+ * @rootelement: Optional name of the expected root element
  * @ctxt: optional pointer to populate with new context pointer
  *
  * Parse XML document provided either as a file or a string. The function
  * guarantees that the XML document contains a root element.
  *
+ * If @rootelement is not NULL, the name of the root element of the parsed XML
+ * is vaidated against
+ *
  * Returns parsed XML document.
  */
 xmlDocPtr
@@ -1032,11 +1036,13 @@ virXMLParseHelper(int domcode,
                   const char *filename,
                   const char *xmlStr,
                   const char *url,
+                  const char *rootelement,
                   xmlXPathContextPtr *ctxt)
 {
     struct virParserData private;
     g_autoptr(xmlParserCtxt) pctxt = NULL;
     g_autoptr(xmlDoc) xml = NULL;
+    xmlNodePtr rootnode;
     const char *docname;
 
     if (filename)
@@ -1075,18 +1081,26 @@ virXMLParseHelper(int domcode,
         return NULL;
     }
 
-    if (xmlDocGetRootElement(xml) == NULL) {
+    if (!(rootnode = xmlDocGetRootElement(xml))) {
         virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR,
                               "%s", _("missing root element"));
 
         return NULL;
     }
 
+    if (rootelement &&
+        !virXMLNodeNameEqual(rootnode, rootelement)) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("expecting root element of '%s', not '%s'"),
+                       rootelement, rootnode->name);
+        return NULL;
+    }
+
     if (ctxt) {
         if (!(*ctxt = virXMLXPathContextNew(xml)))
             return NULL;
 
-        (*ctxt)->node = xmlDocGetRootElement(xml);
+        (*ctxt)->node = rootnode;
     }
 
     return g_steal_pointer(&xml);
index 022ead58b5dab2f05b48618d429d45d00faf9c64..38da2931a408c325135096af393616ab1fb77de2 100644 (file)
@@ -150,6 +150,7 @@ virXMLParseHelper(int domcode,
                   const char *filename,
                   const char *xmlStr,
                   const char *url,
+                  const char *rootelement,
                   xmlXPathContextPtr *ctxt);
 
 const char *
@@ -166,7 +167,7 @@ virXMLPickShellSafeComment(const char *str1,
  * Return the parsed document object, or NULL on failure.
  */
 #define virXMLParse(filename, xmlStr, url) \
-    virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL)
+    virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL, NULL)
 
 /**
  * virXMLParseString:
@@ -178,7 +179,7 @@ virXMLPickShellSafeComment(const char *str1,
  * Return the parsed document object, or NULL on failure.
  */
 #define virXMLParseString(xmlStr, url) \
-    virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL)
+    virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, NULL)
 
 /**
  * virXMLParseFile:
@@ -189,7 +190,7 @@ virXMLPickShellSafeComment(const char *str1,
  * Return the parsed document object, or NULL on failure.
  */
 #define virXMLParseFile(filename) \
-    virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL)
+    virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, NULL)
 
 /**
  * virXMLParseCtxt:
@@ -204,7 +205,7 @@ virXMLPickShellSafeComment(const char *str1,
  * Return the parsed document object, or NULL on failure.
  */
 #define virXMLParseCtxt(filename, xmlStr, url, pctxt) \
-    virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, pctxt)
+    virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL, pctxt)
 
 /**
  * virXMLParseStringCtxt:
@@ -218,7 +219,7 @@ virXMLPickShellSafeComment(const char *str1,
  * Return the parsed document object, or NULL on failure.
  */
 #define virXMLParseStringCtxt(xmlStr, url, pctxt) \
-    virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, pctxt)
+    virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt)
 
 /**
  * virXMLParseFileCtxt:
@@ -231,7 +232,7 @@ virXMLPickShellSafeComment(const char *str1,
  * Return the parsed document object, or NULL on failure.
  */
 #define virXMLParseFileCtxt(filename, pctxt) \
-    virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, pctxt)
+    virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, pctxt)
 
 int
 virXMLSaveFile(const char *path,