From 71f157875afd0396bc4d851d9a61bc884078ca18 Mon Sep 17 00:00:00 2001 From: Affan Ziya Date: Tue, 17 Nov 2026 14:38:31 +0530 Subject: [PATCH] json BUGFIX reject non-hex digits in unicode escapes --- src/json.c | 4 ++-- tests/utests/basic/test_json.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/json.c b/src/json.c index 042ea9b519..09b72d0639 100644 --- a/src/json.c +++ b/src/json.c @@ -222,8 +222,8 @@ lyjson_string(struct lyjson_ctx *jsonctx) /* Basic Multilingual Plane character \uXXXX */ ADD_CHECK_OVERFLOW_GOTO(offset, 1, error); for (value = i = 0; i < 4; i++) { - if (!in[offset + i]) { - LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX, "Invalid basic multilingual plane character \"%s\".", c); + if (!isxdigit(in[offset + i])) { + LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX, "Invalid basic multilingual plane character \"%.6s\".", c); goto error; } else if (isdigit(in[offset + i])) { u = (in[offset + i] - '0'); diff --git a/tests/utests/basic/test_json.c b/tests/utests/basic/test_json.c index f50ac0498e..edd032be49 100644 --- a/tests/utests/basic/test_json.c +++ b/tests/utests/basic/test_json.c @@ -484,6 +484,25 @@ test_string(void **state) CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", NULL, 1); CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1); + /* escaped character */ + str = "\"\\u003c\\u003C\""; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_SUCCESS, lyd_parse_value_fragment(ctx, "/test:l", in, LYD_JSON, 0, 0, 0, &tree)); + assert_string_equal("<<", lyd_get_value(tree)); + lyd_free_all(tree); + tree = NULL; + + /* non-hexadecimal digits in an escaped character */ + str = "\"\\u002s\""; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_EVALID, lyd_parse_value_fragment(ctx, "/test:l", in, LYD_JSON, 0, 0, 0, &tree)); + CHECK_LOG_CTX("Invalid basic multilingual plane character \"\\u002s\".", NULL, 1); + + str = "\"\\u004\"\""; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_EVALID, lyd_parse_value_fragment(ctx, "/test:l", in, LYD_JSON, 0, 0, 0, &tree)); + CHECK_LOG_CTX("Invalid basic multilingual plane character \"\\u004\"\".", NULL, 1); + ly_in_free(in, 0); }