From 85dab9852d6763de55718d1182ea63a4c86cab1b Mon Sep 17 00:00:00 2001 From: Affan Ziya Date: Thu, 19 Nov 2026 20:53:18 +0530 Subject: [PATCH] xml BUGFIX check characters of CDATA sections lyxml_parse_value() checks every character of a text or attribute value with ly_getutf8(), but the content of a CDATA section was copied into the value verbatim. Control characters and invalid UTF-8 were therefore accepted inside although the same bytes are rejected as plain text, and since the data parsers store terms with is_utf8 set, the string type plugin did not check them either. Such values were printed back as XML that is not well-formed and JSON that is not valid UTF-8. The CDATA content is now checked in the loop that already scans it. --- src/xml.c | 8 +++++--- tests/utests/basic/test_xml.c | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/xml.c b/src/xml.c index 881385a284..da70ab477a 100644 --- a/src/xml.c +++ b/src/xml.c @@ -465,7 +465,7 @@ lyxml_parse_value(struct lyxml_ctx *xmlctx, char endchar, char **value, uint32_t uint32_t offset; /* read offset in input buffer */ uint32_t len; /* length of the output string (write offset in output buffer) */ uint32_t size = 0; /* size of the output buffer */ - uint32_t n, u; + uint32_t n, u, c, parsed; ly_bool ws = 1; assert(xmlctx); @@ -564,8 +564,10 @@ lyxml_parse_value(struct lyxml_ctx *xmlctx, char endchar, char **value, uint32_t in += ly_strlen_const("in); } else if (!is_xmlws(in[n])) { diff --git a/tests/utests/basic/test_xml.c b/tests/utests/basic/test_xml.c index 5712f34685..7fdf6ceb8d 100644 --- a/tests/utests/basic/test_xml.c +++ b/tests/utests/basic/test_xml.c @@ -240,6 +240,25 @@ test_text(void **state) lyd_free_all(tree); tree = NULL; + str = ""; + assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, str, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); + assert_non_null(tree); + lyd_free_all(tree); + tree = NULL; + + /* CDATA characters are checked the same as text */ + str = ""; + assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, str, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); + CHECK_LOG_CTX("Invalid character 0x1.", NULL, 1); + + str = ""; + assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, str, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); + CHECK_LOG_CTX("Invalid character 0xff.", NULL, 1); + + str = ""; + assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, str, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); + CHECK_LOG_CTX("Invalid character 0xf0.", NULL, 1); + /* invalid characters in string */ str = "