Blame SOURCES/expat-2.2.5-Protect-against-malicious-namespace-declarations.patch

afa004
commit fd5473ef5873048eadef344a1f16f71ad8eefe99
afa004
Author: Tomas Korbar <tkorbar@redhat.com>
afa004
Date:   Mon Mar 14 12:17:41 2022 +0100
afa004
afa004
    Protect against malicious namespace declarations
afa004
afa004
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
afa004
index 581b9a4..6f3510b 100644
afa004
--- a/lib/xmlparse.c
afa004
+++ b/lib/xmlparse.c
afa004
@@ -661,8 +661,7 @@ XML_ParserCreate(const XML_Char *encodingName)
afa004
 XML_Parser XMLCALL
afa004
 XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
afa004
 {
afa004
-  XML_Char tmp[2];
afa004
-  *tmp = nsSep;
afa004
+  XML_Char tmp[2] = {nsSep, 0};
afa004
   return XML_ParserCreate_MM(encodingName, NULL, tmp);
afa004
 }
afa004
 
afa004
@@ -1288,8 +1287,7 @@ XML_ExternalEntityParserCreate(XML_Parser oldParser,
afa004
      would be otherwise.
afa004
   */
afa004
   if (parser->m_ns) {
afa004
-    XML_Char tmp[2];
afa004
-    *tmp = parser->m_namespaceSeparator;
afa004
+    XML_Char tmp[2] = {parser->m_namespaceSeparator, 0};
afa004
     parser = parserCreate(encodingName, &parser->m_mem, tmp, newDtd);
afa004
   }
afa004
   else {
afa004
@@ -3640,6 +3638,117 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
afa004
   return XML_ERROR_NONE;
afa004
 }
afa004
 
afa004
+static XML_Bool
afa004
+is_rfc3986_uri_char(XML_Char candidate) {
afa004
+  // For the RFC 3986 ANBF grammar see
afa004
+  // https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
afa004
+
afa004
+  switch (candidate) {
afa004
+  // From rule "ALPHA" (uppercase half)
afa004
+  case 'A':
afa004
+  case 'B':
afa004
+  case 'C':
afa004
+  case 'D':
afa004
+  case 'E':
afa004
+  case 'F':
afa004
+  case 'G':
afa004
+  case 'H':
afa004
+  case 'I':
afa004
+  case 'J':
afa004
+  case 'K':
afa004
+  case 'L':
afa004
+  case 'M':
afa004
+  case 'N':
afa004
+  case 'O':
afa004
+  case 'P':
afa004
+  case 'Q':
afa004
+  case 'R':
afa004
+  case 'S':
afa004
+  case 'T':
afa004
+  case 'U':
afa004
+  case 'V':
afa004
+  case 'W':
afa004
+  case 'X':
afa004
+  case 'Y':
afa004
+  case 'Z':
afa004
+
afa004
+  // From rule "ALPHA" (lowercase half)
afa004
+  case 'a':
afa004
+  case 'b':
afa004
+  case 'c':
afa004
+  case 'd':
afa004
+  case 'e':
afa004
+  case 'f':
afa004
+  case 'g':
afa004
+  case 'h':
afa004
+  case 'i':
afa004
+  case 'j':
afa004
+  case 'k':
afa004
+  case 'l':
afa004
+  case 'm':
afa004
+  case 'n':
afa004
+  case 'o':
afa004
+  case 'p':
afa004
+  case 'q':
afa004
+  case 'r':
afa004
+  case 's':
afa004
+  case 't':
afa004
+  case 'u':
afa004
+  case 'v':
afa004
+  case 'w':
afa004
+  case 'x':
afa004
+  case 'y':
afa004
+  case 'z':
afa004
+
afa004
+  // From rule "DIGIT"
afa004
+  case '0':
afa004
+  case '1':
afa004
+  case '2':
afa004
+  case '3':
afa004
+  case '4':
afa004
+  case '5':
afa004
+  case '6':
afa004
+  case '7':
afa004
+  case '8':
afa004
+  case '9':
afa004
+
afa004
+  // From rule "pct-encoded"
afa004
+  case '%':
afa004
+
afa004
+  // From rule "unreserved"
afa004
+  case '-':
afa004
+  case '.':
afa004
+  case '_':
afa004
+  case '~':
afa004
+
afa004
+  // From rule "gen-delims"
afa004
+  case ':':
afa004
+  case '/':
afa004
+  case '?':
afa004
+  case '#':
afa004
+  case '[':
afa004
+  case ']':
afa004
+  case '@':
afa004
+
afa004
+  // From rule "sub-delims"
afa004
+  case '!':
afa004
+  case '$':
afa004
+  case '&':
afa004
+  case '\'':
afa004
+  case '(':
afa004
+  case ')':
afa004
+  case '*':
afa004
+  case '+':
afa004
+  case ',':
afa004
+  case ';':
afa004
+  case '=':
afa004
+    return XML_TRUE;
afa004
+
afa004
+  default:
afa004
+    return XML_FALSE;
afa004
+  }
afa004
+}
afa004
+
afa004
 /* addBinding() overwrites the value of prefix->binding without checking.
afa004
    Therefore one must keep track of the old value outside of addBinding().
afa004
 */
afa004
@@ -3700,6 +3809,29 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
afa004
     if (!mustBeXML && isXMLNS
afa004
         && (len > xmlnsLen || uri[len] != xmlnsNamespace[len]))
afa004
       isXMLNS = XML_FALSE;
afa004
+
afa004
+    // NOTE: While Expat does not validate namespace URIs against RFC 3986
afa004
+    //       today (and is not REQUIRED to do so with regard to the XML 1.0
afa004
+    //       namespaces specification) we have to at least make sure, that
afa004
+    //       the application on top of Expat (that is likely splitting expanded
afa004
+    //       element names ("qualified names") of form
afa004
+    //       "[uri sep] local [sep prefix] '\0'" back into 1, 2 or 3 pieces
afa004
+    //       in its element handler code) cannot be confused by an attacker
afa004
+    //       putting additional namespace separator characters into namespace
afa004
+    //       declarations.  That would be ambiguous and not to be expected.
afa004
+    //
afa004
+    //       While the HTML API docs of function XML_ParserCreateNS have been
afa004
+    //       advising against use of a namespace separator character that can
afa004
+    //       appear in a URI for >20 years now, some widespread applications
afa004
+    //       are using URI characters (':' (colon) in particular) for a
afa004
+    //       namespace separator, in practice.  To keep these applications
afa004
+    //       functional, we only reject namespaces URIs containing the
afa004
+    //       application-chosen namespace separator if the chosen separator
afa004
+    //       is a non-URI character with regard to RFC 3986.
afa004
+    if (parser->m_ns && (uri[len] == parser->m_namespaceSeparator)
afa004
+        && ! is_rfc3986_uri_char(uri[len])) {
afa004
+      return XML_ERROR_SYNTAX;
afa004
+    }
afa004
   }
afa004
   isXML = isXML && len == xmlLen;
afa004
   isXMLNS = isXMLNS && len == xmlnsLen;
afa004
diff --git a/tests/runtests.c b/tests/runtests.c
afa004
index ecc6f47..eabd55d 100644
afa004
--- a/tests/runtests.c
afa004
+++ b/tests/runtests.c
afa004
@@ -7950,6 +7950,38 @@ START_TEST(test_ns_double_colon_doctype)
afa004
 }
afa004
 END_TEST
afa004
 
afa004
+START_TEST(test_ns_separator_in_uri) {
afa004
+  struct test_case {
afa004
+    enum XML_Status expectedStatus;
afa004
+    const char *doc;
afa004
+    XML_Char namesep;
afa004
+  };
afa004
+  struct test_case cases[] = {
afa004
+      {XML_STATUS_OK, "<doc xmlns='one_two' />", XCS('\n')},
afa004
+      {XML_STATUS_ERROR, "<doc xmlns='one
two' />", XCS('\n')},
afa004
+      {XML_STATUS_OK, "<doc xmlns='one:two' />", XCS(':')},
afa004
+  };
afa004
+
afa004
+  size_t i = 0;
afa004
+  size_t failCount = 0;
afa004
+  for (; i < sizeof(cases) / sizeof(cases[0]); i++) {
afa004
+    XML_Parser parser = XML_ParserCreateNS(NULL, cases[i].namesep);
afa004
+    XML_SetElementHandler(parser, dummy_start_element, dummy_end_element);
afa004
+    if (XML_Parse(parser, cases[i].doc, (int)strlen(cases[i].doc),
afa004
+                  /*isFinal*/ XML_TRUE)
afa004
+        != cases[i].expectedStatus) {
afa004
+      failCount++;
afa004
+    }
afa004
+    XML_ParserFree(parser);
afa004
+  }
afa004
+
afa004
+  if (failCount) {
afa004
+    fail("Namespace separator handling is broken");
afa004
+  }
afa004
+}
afa004
+END_TEST
afa004
+
afa004
+
afa004
 /* Control variable; the number of times duff_allocator() will successfully allocate */
afa004
 #define ALLOC_ALWAYS_SUCCEED (-1)
afa004
 #define REALLOC_ALWAYS_SUCCEED (-1)
afa004
@@ -12290,6 +12322,7 @@ make_suite(void)
afa004
     tcase_add_test(tc_namespace, test_ns_utf16_doctype);
afa004
     tcase_add_test(tc_namespace, test_ns_invalid_doctype);
afa004
     tcase_add_test(tc_namespace, test_ns_double_colon_doctype);
afa004
+    tcase_add_test(tc_namespace, test_ns_separator_in_uri);
afa004
 
afa004
     suite_add_tcase(s, tc_misc);
afa004
     tcase_add_checked_fixture(tc_misc, NULL, basic_teardown);