From 2e8901ce84abc846c391fb0b904eb23fbb4b671c Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Wed, 28 Oct 2026 12:58:29 +0100 Subject: [PATCH 1/4] config UPDATE public LY_HAVE_CBOR macro The CBOR format is built only when libcbor is found, but this was recorded in the private ENABLE_CBOR_SUPPORT definition, so an application had no way to tell whether LYD_CBOR works. Replace the private definition with LY_HAVE_CBOR in the installed ly_config.h so that both libyang and its users test the same macro. --- CMakeLists.txt | 3 +-- src/ly_config.h.in | 3 +++ src/printer_data.c | 4 ++-- src/tree_data.c | 8 ++++---- src/tree_data.h | 2 +- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d60bc4f82..61a9851415 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -342,6 +342,7 @@ endif() find_package(CBOR) if(CBOR_FOUND) + set(LY_HAVE_CBOR 1) list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c) list(APPEND headers src/lcbor.h) list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c) @@ -466,10 +467,8 @@ target_link_libraries(yang ${PCRE2_LIBRARIES}) # link libcbor if found if(CBOR_FOUND) if(TARGET yangobj) - target_compile_definitions(yangobj PRIVATE ENABLE_CBOR_SUPPORT) target_include_directories(yangobj PRIVATE ${CBOR_INCLUDE_DIR}) endif() - target_compile_definitions(yang PRIVATE ENABLE_CBOR_SUPPORT) target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR}) target_link_libraries(yang ${CBOR_LIBRARY}) endif() diff --git a/src/ly_config.h.in b/src/ly_config.h.in index 807b589acb..487ebf8e64 100644 --- a/src/ly_config.h.in +++ b/src/ly_config.h.in @@ -89,4 +89,7 @@ /** regex.h BSD flag */ #cmakedefine HAVE_REG_STARTEND +/** libyang built with CBOR support (libcbor), ::LYD_CBOR is usable */ +#cmakedefine LY_HAVE_CBOR + #endif /* LY_CONFIG_H_ */ diff --git a/src/printer_data.c b/src/printer_data.c index ebd162e7e1..8faa4c0d86 100644 --- a/src/printer_data.c +++ b/src/printer_data.c @@ -38,7 +38,7 @@ lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, u case LYD_LYB: ret = lyb_print_data(out, root, options); break; -#ifdef ENABLE_CBOR_SUPPORT +#ifdef LY_HAVE_CBOR case LYD_CBOR: ret = cbor_print_data(out, root, options); break; @@ -47,7 +47,7 @@ lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, u LOGERR(root ? LYD_CTX(root) : NULL, LY_ENOT, "CBOR format not supported, libcbor not found."); ret = LY_ENOT; break; -#endif /* ENABLE_CBOR_SUPPORT */ +#endif /* LY_HAVE_CBOR */ case LYD_UNKNOWN: LOGERR(root ? LYD_CTX(root) : NULL, LY_EINVAL, "Unknown data output format."); ret = LY_EINVAL; diff --git a/src/tree_data.c b/src/tree_data.c index 50514b17b3..3514e23c9f 100644 --- a/src/tree_data.c +++ b/src/tree_data.c @@ -134,7 +134,7 @@ lyd_parse(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **f case LYD_LYB: r = lyd_parse_lyb(ctx, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, &lydctx); break; -#ifdef ENABLE_CBOR_SUPPORT +#ifdef LY_HAVE_CBOR case LYD_CBOR: r = lyd_parse_cbor(ctx, NULL, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx); break; @@ -143,7 +143,7 @@ lyd_parse(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **f LOGARG(ctx, format); r = LY_EINVAL; break; -#endif /* ENABLE_CBOR_SUPPORT */ +#endif /* LY_HAVE_CBOR */ case LYD_UNKNOWN: LOGARG(ctx, format); r = LY_EINVAL; @@ -487,7 +487,7 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in case LYD_LYB: rc = lyd_parse_lyb(ctx, parent, &first, in, parse_options, val_opts, int_opts, &parsed, &lydctx); break; -#ifdef ENABLE_CBOR_SUPPORT +#ifdef LY_HAVE_CBOR case LYD_CBOR: rc = lyd_parse_cbor(ctx, NULL, parent, &first, in, parse_options, val_opts, int_opts, &parsed, NULL, &lydctx); break; @@ -496,7 +496,7 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in LOGARG(ctx, format); rc = LY_EINVAL; break; -#endif /* ENABLE_CBOR_SUPPORT */ +#endif /* LY_HAVE_CBOR */ case LYD_UNKNOWN: LOGARG(ctx, format); rc = LY_EINVAL; diff --git a/src/tree_data.h b/src/tree_data.h index f6f5b78a3b..e4f5475821 100644 --- a/src/tree_data.h +++ b/src/tree_data.h @@ -599,7 +599,7 @@ typedef enum { LYD_XML, /**< XML instance data format */ LYD_JSON, /**< JSON instance data format */ LYD_LYB, /**< LYB instance data format */ - LYD_CBOR /**< CBOR instance data format */ + LYD_CBOR /**< CBOR instance data format, available only if LY_HAVE_CBOR is defined */ } LYD_FORMAT; /** @} */ From a4426398ae827d47299973e528227c9c2622229b Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Wed, 28 Oct 2026 12:58:41 +0100 Subject: [PATCH 2/4] parser cbor FEATURE RESTCONF notification envelope lyd_parse_op() accepted only JSON for LYD_TYPE_NOTIF_RESTCONF, so a CBOR-encoded notification (RFC 8040 envelope in RFC 9254 encoding, as sent by UDP-Notif) could not be parsed with its eventTime. The CBOR input is decoded as a whole, so the envelope is checked directly in the loaded map and eventTime is created in the envelope wherever it is placed, unlike the streaming JSON parser. Also cover a notification nested in a list and one with an anydata payload in the CBOR round-trip tests. --- src/parser_cbor.c | 121 ++++++++++++++++++++++++++++++ src/parser_data.h | 7 +- src/parser_internal.h | 16 ++++ src/tree_data.c | 12 ++- tests/modules/yang/cbor-test.yang | 7 ++ tests/utests/data/test_cbor.c | 102 +++++++++++++++++++++++++ 6 files changed, 260 insertions(+), 5 deletions(-) diff --git a/src/parser_cbor.c b/src/parser_cbor.c index 82c9c11559..5161f6dda0 100644 --- a/src/parser_cbor.c +++ b/src/parser_cbor.c @@ -1429,6 +1429,12 @@ lydcbor_subtree_r(struct lyd_cbor_ctx *lydctx, struct lyd_node *parent, struct l /* process the node name */ lydcbor_parse_name(key_str, key_len, &name, &name_len, &prefix, &prefix_len, &is_meta); + if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && !is_meta && !prefix_len && + !ly_strncmp("eventTime", name, name_len)) { + /* already parsed into the envelope by lyd_parse_cbor_restconf() */ + continue; + } + if (!is_meta || name_len || prefix_len) { /* get the schema node */ r = lydcbor_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext); @@ -1679,3 +1685,118 @@ lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st } return rc; } + +LY_ERR +lyd_parse_cbor_restconf(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, + struct lyd_node **first_p, struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p) +{ + LY_ERR r, rc = LY_SUCCESS; + struct lyd_cbor_ctx *lydctx = NULL; + const cbor_item_t *root, *notif, *event_time = NULL; + struct cbor_pair *pairs; + struct lyd_node *node; + char *str = NULL; + size_t str_len; + uint32_t i; + + assert(ctx && in && first_p && envp && lydctx_p); + + *envp = NULL; + + rc = lyd_parse_cbor_init(ctx, in, parse_opts, val_opts, &lydctx); + LY_CHECK_GOTO(rc, cleanup); + lydctx->int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME; + + /* parse "notification" */ + root = lydctx->cborctx->cbor_data; + pairs = cbor_map_handle(root); + if ((cbor_map_size(root) != 1) || !cbor_isa_string(pairs[0].key)) { + LOGVAL(ctx, NULL, LYVE_DATA, "Expected a single CBOR map member \"ietf-restconf:notification\"."); + rc = LY_EVALID; + goto cleanup; + } + LY_CHECK_GOTO(rc = lydcbor_item_to_string(ctx, pairs[0].key, &str, &str_len), cleanup); + if (ly_strncmp("ietf-restconf:notification", str, str_len)) { + LOGVAL(ctx, NULL, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"ietf-restconf:notification\".", + (int)str_len, str); + rc = LY_EVALID; + goto cleanup; + } else if (!cbor_isa_map(pairs[0].value)) { + LOGVAL(ctx, NULL, LYVE_SYNTAX, "Expecting CBOR %s but %s found.", lycbor_token2str(CBOR_TYPE_MAP), + lycbor_token2str(cbor_typeof(pairs[0].value))); + rc = LY_EVALID; + goto cleanup; + } + notif = pairs[0].value; + + rc = lyd_create_opaq(ctx, "notification", 12, "ietf-restconf", 13, "ietf-restconf", 13, NULL, 0, NULL, + LY_VALUE_CBOR, NULL, LYD_VALHINT_STRING, envp); + LY_CHECK_GOTO(rc, cleanup); + + /* find eventTime, it can be anywhere in the map */ + pairs = cbor_map_handle(notif); + for (i = 0; i < cbor_map_size(notif); ++i) { + if (!cbor_isa_string(pairs[i].key)) { + continue; + } + + free(str); + LY_CHECK_GOTO(rc = lydcbor_item_to_string(ctx, pairs[i].key, &str, &str_len), cleanup); + if (ly_strncmp("eventTime", str, str_len)) { + continue; + } + + if (event_time) { + LOGVAL(ctx, NULL, LYVE_DATA, "Duplicate notification \"eventTime\" node."); + rc = LY_EVALID; + goto cleanup; + } + event_time = pairs[i].value; + } + if (!event_time) { + LOGVAL(ctx, NULL, LYVE_DATA, "Missing notification \"eventTime\" node."); + rc = LY_EVALID; + goto cleanup; + } else if (!cbor_isa_string(event_time)) { + LOGVAL(ctx, NULL, LYVE_SYNTAX, "Expecting CBOR %s but %s found.", lycbor_token2str(CBOR_TYPE_STRING), + lycbor_token2str(cbor_typeof(event_time))); + rc = LY_EVALID; + goto cleanup; + } + + /* parse eventTime into the envelope */ + free(str); + LY_CHECK_GOTO(rc = lydcbor_item_to_string(ctx, event_time, &str, &str_len), cleanup); + rc = lyd_create_opaq(ctx, "eventTime", 9, NULL, 0, NULL, 0, str, str_len, NULL, LY_VALUE_CBOR, NULL, + LYD_VALHINT_STRING, &node); + LY_CHECK_GOTO(rc, cleanup); + lyd_insert_child(*envp, node); + LY_CHECK_GOTO(rc = lyd_parser_notif_eventtime_validate(node), cleanup); + + /* parse the notification */ + r = lydcbor_subtree_r(lydctx, NULL, first_p, parsed, notif); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + + if (!lydctx->op_node) { + LOGVAL(ctx, NULL, LYVE_DATA, "Missing the operation node."); + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + } + + /* finish linking metadata */ + r = lydcbor_metadata_finish(lydctx, first_p); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + +cleanup: + free(str); + if (rc) { + lyd_cbor_ctx_free((struct lyd_ctx *)lydctx); + } else { + *lydctx_p = (struct lyd_ctx *)lydctx; + + /* the CBOR context is no more needed */ + lycbor_ctx_free(lydctx->cborctx); + lydctx->cborctx = NULL; + } + return rc; +} diff --git a/src/parser_data.h b/src/parser_data.h index e2d498ac79..714a1d089a 100644 --- a/src/parser_data.h +++ b/src/parser_data.h @@ -367,7 +367,7 @@ enum lyd_type { LYD_TYPE_RPC_RESTCONF, /**< message-body of a RESTCONF operation input parameters ([ref](https://www.rfc-editor.org/rfc/rfc8040.html#section-3.6.1)) */ - LYD_TYPE_NOTIF_RESTCONF, /**< RESTCONF JSON notification data + LYD_TYPE_NOTIF_RESTCONF, /**< RESTCONF JSON or CBOR notification data ([ref](https://www.rfc-editor.org/rfc/rfc8040.html#section-6.4)), to parse a notification in XML, use ::LYD_TYPE_NOTIF_NETCONF */ LYD_TYPE_REPLY_RESTCONF /**< message-body of a RESTCONF operation output parameters @@ -413,8 +413,9 @@ enum lyd_type { * * - ::LYD_TYPE_NOTIF_RESTCONF: * - @p parent - must be NULL, the whole notification is expected; - * - @p format - must be ::LYD_JSON, XML-formatted notifications are parsed using ::LYD_TYPE_NOTIF_NETCONF; - * - @p tree - must be provided, all the RESTCONF-specific JSON objects will be returned here as + * - @p format - can be both ::LYD_JSON and ::LYD_CBOR, XML-formatted notifications are parsed using + * ::LYD_TYPE_NOTIF_NETCONF; + * - @p tree - must be provided, all the RESTCONF-specific JSON/CBOR objects will be returned here as * a separate opaque data tree, even if the function fails, this may be returned; * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation; * diff --git a/src/parser_internal.h b/src/parser_internal.h index b350973863..269875ac47 100644 --- a/src/parser_internal.h +++ b/src/parser_internal.h @@ -375,6 +375,22 @@ LY_ERR lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance * struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p); +/** + * @brief Parse CBOR data as a RESTCONF notification. + * + * @param[in] ctx libyang context. + * @param[in] in Input structure. + * @param[in] parse_opts Options for parser, see @ref dataparseroptions. + * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions. + * @param[in,out] first_p Pointer to the first top-level parsed node. + * @param[out] envp Parsed envelope with eventTime, may be returned possibly even on an error. + * @param[out] parsed Set to add all the parsed siblings into. + * @param[out] lydctx_p Data parser context to finish validation. + * @return LY_ERR value. + */ +LY_ERR lyd_parse_cbor_restconf(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, + struct lyd_node **first_p, struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p); + /** * @brief Validate eventTime date-and-time value. * diff --git a/src/tree_data.c b/src/tree_data.c index 3514e23c9f..034da90f09 100644 --- a/src/tree_data.c +++ b/src/tree_data.c @@ -426,7 +426,7 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in proto_msg = 1; break; case LYD_TYPE_NOTIF_RESTCONF: - LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL); + LY_CHECK_ARG_RET(ctx, (format == LYD_JSON) || (format == LYD_CBOR), !parent, tree, op, LY_EINVAL); proto_msg = 1; break; @@ -451,9 +451,17 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in if (format == LYD_XML) { /* parse the NETCONF (or RESTCONF XML) message */ rc = lyd_parse_xml_netconf(ctx, parent, &first, in, parse_options, val_opts, data_type, &envp, &parsed, &lydctx); - } else { + } else if (format == LYD_JSON) { /* parse the RESTCONF message */ rc = lyd_parse_json_restconf(ctx, parent, &first, in, parse_options, val_opts, data_type, &envp, &parsed, &lydctx); + } else { +#ifdef LY_HAVE_CBOR + /* parse the RESTCONF CBOR notification */ + rc = lyd_parse_cbor_restconf(ctx, in, parse_options, val_opts, &first, &envp, &parsed, &lydctx); +#else + LOGARG(ctx, format); + rc = LY_EINVAL; +#endif /* LY_HAVE_CBOR */ } if (rc) { if (envp) { diff --git a/tests/modules/yang/cbor-test.yang b/tests/modules/yang/cbor-test.yang index 4874318649..6532e05cd8 100644 --- a/tests/modules/yang/cbor-test.yang +++ b/tests/modules/yang/cbor-test.yang @@ -174,6 +174,12 @@ module cbor-test { leaf lf-name { type string; } + + notification notif-user { + leaf lf-reason { + type string; + } + } } /* ===== Nested lists / leaf-lists ===== @@ -261,6 +267,7 @@ module cbor-test { leaf lf-message { type string; } + anydata anydata-payload; } /* ===== Augment ===== */ diff --git a/tests/utests/data/test_cbor.c b/tests/utests/data/test_cbor.c index 5253d72d8a..2e1d99e195 100644 --- a/tests/utests/data/test_cbor.c +++ b/tests/utests/data/test_cbor.c @@ -210,6 +210,8 @@ test_operations(void **state) const struct test_case tests[] = { {"rpc", "{\"cbor-test:rpc-reset\":{\"lf1\":true}}"}, {"notification", "{\"cbor-test:notif-alarm\":{\"lf-severity\":\"warning\",\"lf-message\":\"Something\"}}"}, + {"notification-nested", "{\"cbor-test:list-user\":[{\"key-id\":1,\"notif-user\":{\"lf-reason\":\"x\"}}]}"}, + {"notification-anydata", "{\"cbor-test:notif-alarm\":{\"anydata-payload\":{\"list-user\":[{\"key-id\":1}]}}}"}, }; for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) { @@ -474,6 +476,104 @@ test_negint(void **state) CHECK_LOG_CTX("Invalid type uint8 value \"-18446744073709551616\".", "/cbor-test:lf-uintg8", 1); } +/* CBOR RESTCONF notification envelope parts */ +#define CBOR_ENVP_KEY "\x78\x1a" "ietf-restconf:notification" +#define CBOR_EVENT_TIME "\x69" "eventTime" "\x74" "2013-12-21T00:01:00Z" +#define CBOR_NOTIF "\x75" "cbor-test:notif-alarm" "\xa1" "\x6a" "lf-message" "\x61" "m" + +static void +test_restconf_notification(void **state) +{ + struct lyd_node *tree, *ntf; + struct ly_in *in; + uint32_t i; + const char *tests[] = { + /* eventTime first, as RFC 8040 shows it */ + "\xa1" CBOR_ENVP_KEY "\xa2" CBOR_EVENT_TIME CBOR_NOTIF, + /* eventTime last, map members are unordered */ + "\xa1" CBOR_ENVP_KEY "\xa2" CBOR_NOTIF CBOR_EVENT_TIME, + /* indefinite-length map, as the libyang CBOR printer encodes maps */ + "\xa1" CBOR_ENVP_KEY "\xbf" CBOR_NOTIF CBOR_EVENT_TIME "\xff", + }; + + for (i = 0; i < sizeof tests / sizeof *tests; ++i) { + assert_int_equal(LY_SUCCESS, ly_in_new_memory_chunk(tests[i], strlen(tests[i]), &in)); + assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_CBOR, LYD_TYPE_NOTIF_RESTCONF, + LYD_PARSE_STRICT, &tree, &ntf)); + ly_in_free(in, 0); + + CHECK_LYD_STRING_PARAM(tree, "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}", + LYD_JSON, LYD_PRINT_SHRINK); + CHECK_LYD_STRING_PARAM(ntf, "{\"cbor-test:notif-alarm\":{\"lf-message\":\"m\"}}", LYD_JSON, LYD_PRINT_SHRINK); + lyd_free_all(tree); + lyd_free_all(ntf); + } +} + +static void +test_restconf_notification_invalid(void **state) +{ + struct lyd_node *tree, *ntf; + struct ly_in *in; + uint32_t i; + + const struct { + const char *data; + const char *msg; + const char *path; + } tests[] = { + { + "\xa1" CBOR_NOTIF, + "Unexpected object \"cbor-test:notif-alarm\" instead of \"ietf-restconf:notification\".", NULL + }, + { + "\xa2" CBOR_ENVP_KEY "\xa0" CBOR_NOTIF, + "Expected a single CBOR map member \"ietf-restconf:notification\".", NULL + }, + { + "\xa1" CBOR_ENVP_KEY "\x01", + "Expecting CBOR map but unsigned integer found.", NULL + }, + { + "\xa1" CBOR_ENVP_KEY "\xa1" CBOR_NOTIF, + "Missing notification \"eventTime\" node.", NULL + }, + { + "\xa1" CBOR_ENVP_KEY "\xa2" "\x69" "eventTime" "\x01" CBOR_NOTIF, + "Expecting CBOR string but unsigned integer found.", NULL + }, + { + "\xa1" CBOR_ENVP_KEY "\xa3" CBOR_EVENT_TIME CBOR_EVENT_TIME CBOR_NOTIF, + "Duplicate notification \"eventTime\" node.", NULL + }, + { + "\xa1" CBOR_ENVP_KEY "\xa2" "\x69" "eventTime" "\x64" "2013" CBOR_NOTIF, + "Unsatisfied pattern - \"2013\" does not match \"[0-9]{4}-(1[0-2]|0[1-9])-(0[1-9]|[1-2][0-9]|3[0-1])" + "T(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?" + "(Z|[\\+\\-]((1[0-3]|0[0-9]):([0-5][0-9])|14:00))?\".", "/yang:date-and-time" + }, + { + "\xa1" CBOR_ENVP_KEY "\xa1" CBOR_EVENT_TIME, + "Missing the operation node.", NULL + }, + }; + + for (i = 0; i < sizeof tests / sizeof *tests; ++i) { + assert_int_equal(LY_SUCCESS, ly_in_new_memory_chunk(tests[i].data, strlen(tests[i].data), &in)); + assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_CBOR, LYD_TYPE_NOTIF_RESTCONF, 0, + &tree, &ntf)); + CHECK_LOG_CTX(tests[i].msg, tests[i].path, 1); + ly_in_free(in, 0); + lyd_free_all(tree); + } + + /* unknown data length */ + assert_int_equal(LY_SUCCESS, ly_in_new_memory(tests[0].data, &in)); + assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_CBOR, LYD_TYPE_NOTIF_RESTCONF, 0, &tree, &ntf)); + CHECK_LOG_CTX("Failed to parse CBOR data (unknown data length).", NULL, 0); + ly_in_free(in, 0); +} + int main(void) { @@ -487,6 +587,8 @@ main(void) UTEST(test_attributes, setup), UTEST(test_invalid_input, setup), UTEST(test_negint, setup), + UTEST(test_restconf_notification, setup), + UTEST(test_restconf_notification_invalid, setup), }; return cmocka_run_group_tests(tests, NULL, NULL); From d6881899e962acae57475a1fc81f4389b3c029a1 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Wed, 28 Oct 2026 12:58:41 +0100 Subject: [PATCH 3/4] tree data BUGFIX check RESTCONF op format LYD_TYPE_RPC_RESTCONF and LYD_TYPE_REPLY_RESTCONF did not check the format, so LYB or CBOR input was passed to the JSON RESTCONF parser. Only XML and JSON are documented and supported, reject the rest. --- src/tree_data.c | 3 ++- tests/utests/data/test_cbor.c | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/tree_data.c b/src/tree_data.c index 034da90f09..545f2cd5d6 100644 --- a/src/tree_data.c +++ b/src/tree_data.c @@ -422,7 +422,8 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in break; case LYD_TYPE_RPC_RESTCONF: case LYD_TYPE_REPLY_RESTCONF: - LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL); + LY_CHECK_ARG_RET(ctx, (format == LYD_XML) || (format == LYD_JSON), parent, parent->schema, + parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL); proto_msg = 1; break; case LYD_TYPE_NOTIF_RESTCONF: diff --git a/tests/utests/data/test_cbor.c b/tests/utests/data/test_cbor.c index 2e1d99e195..1f0d3143f0 100644 --- a/tests/utests/data/test_cbor.c +++ b/tests/utests/data/test_cbor.c @@ -513,7 +513,7 @@ test_restconf_notification(void **state) static void test_restconf_notification_invalid(void **state) { - struct lyd_node *tree, *ntf; + struct lyd_node *tree, *ntf, *rpc; struct ly_in *in; uint32_t i; @@ -572,6 +572,15 @@ test_restconf_notification_invalid(void **state) assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_CBOR, LYD_TYPE_NOTIF_RESTCONF, 0, &tree, &ntf)); CHECK_LOG_CTX("Failed to parse CBOR data (unknown data length).", NULL, 0); ly_in_free(in, 0); + + /* RESTCONF RPC is not supported in CBOR */ + assert_int_equal(LY_SUCCESS, lyd_new_inner(NULL, ly_ctx_get_module_implemented(UTEST_LYCTX, "cbor-test"), + "rpc-reset", 0, &rpc)); + assert_int_equal(LY_SUCCESS, ly_in_new_memory_chunk(tests[0].data, strlen(tests[0].data), &in)); + assert_int_equal(LY_EINVAL, lyd_parse_op(UTEST_LYCTX, rpc, in, LYD_CBOR, LYD_TYPE_RPC_RESTCONF, 0, &tree, NULL)); + CHECK_LOG_CTX("Invalid argument (format == LYD_XML) || (format == LYD_JSON) (lyd_parse_op()).", NULL, 0); + ly_in_free(in, 0); + lyd_free_tree(rpc); } int From 4f2c72a2e6eacb4421a2e55de35dec8f01b7a7a9 Mon Sep 17 00:00:00 2001 From: Roman Janota Date: Thu, 29 Oct 2026 13:29:26 +0100 Subject: [PATCH 4/4] parser cbor BUGFIX indefinite-length strings lydcbor_item_to_string() read only definite-length strings, an indefinite-length (chunked) text or byte string, valid per RFC 8949, was read as an empty string. Concatenate its chunks. --- src/parser_cbor.c | 72 ++++++++++++++++++++++++++++++----- tests/utests/data/test_cbor.c | 6 +++ 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/parser_cbor.c b/src/parser_cbor.c index 5161f6dda0..1d3e1dd7d7 100644 --- a/src/parser_cbor.c +++ b/src/parser_cbor.c @@ -374,6 +374,50 @@ lydcbor_check_string(const struct ly_ctx *ctx, const char *str, size_t str_len) return LY_SUCCESS; } +/** + * @brief Concatenate the chunks of an indefinite-length CBOR text or byte string. + * + * @param[in] ctx Context for logging. + * @param[in] item Indefinite-length string item. + * @param[out] str_val String value (allocated, caller must free). + * @param[out] str_len String length. + * @return LY_ERR value. + */ +static LY_ERR +lydcbor_chunks_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char **str_val, size_t *str_len) +{ + ly_bool text = cbor_isa_string(item); + cbor_item_t **chunks; + uint32_t i, count; + size_t len; + + chunks = text ? cbor_string_chunks_handle(item) : cbor_bytestring_chunks_handle(item); + count = (uint32_t)(text ? cbor_string_chunk_count(item) : cbor_bytestring_chunk_count(item)); + + /* total length */ + for (i = 0; i < count; ++i) { + *str_len += text ? cbor_string_length(chunks[i]) : cbor_bytestring_length(chunks[i]); + } + + *str_val = malloc(*str_len + 1); + LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM); + + /* copy the chunks */ + len = 0; + for (i = 0; i < count; ++i) { + if (text) { + memcpy(*str_val + len, cbor_string_handle(chunks[i]), cbor_string_length(chunks[i])); + len += cbor_string_length(chunks[i]); + } else { + memcpy(*str_val + len, cbor_bytestring_handle(chunks[i]), cbor_bytestring_length(chunks[i])); + len += cbor_bytestring_length(chunks[i]); + } + } + (*str_val)[len] = '\0'; + + return LY_SUCCESS; +} + /** * @brief Convert a CBOR item to a string representation. * @@ -429,19 +473,27 @@ lydcbor_item_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char * break; } case CBOR_TYPE_BYTESTRING: - *str_len = cbor_bytestring_length(item); - *str_val = malloc(*str_len + 1); - LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM); - memcpy(*str_val, cbor_bytestring_handle(item), *str_len); - (*str_val)[*str_len] = '\0'; + if (cbor_bytestring_is_indefinite(item)) { + LY_CHECK_RET(lydcbor_chunks_to_string(ctx, item, str_val, str_len)); + } else { + *str_len = cbor_bytestring_length(item); + *str_val = malloc(*str_len + 1); + LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM); + memcpy(*str_val, cbor_bytestring_handle(item), *str_len); + (*str_val)[*str_len] = '\0'; + } LY_CHECK_ERR_RET(ret = lydcbor_check_string(ctx, *str_val, *str_len), free(*str_val); *str_val = NULL, ret); break; case CBOR_TYPE_STRING: - *str_len = cbor_string_length(item); - *str_val = malloc(*str_len + 1); - LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM); - memcpy(*str_val, cbor_string_handle(item), *str_len); - (*str_val)[*str_len] = '\0'; + if (cbor_string_is_indefinite(item)) { + LY_CHECK_RET(lydcbor_chunks_to_string(ctx, item, str_val, str_len)); + } else { + *str_len = cbor_string_length(item); + *str_val = malloc(*str_len + 1); + LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM); + memcpy(*str_val, cbor_string_handle(item), *str_len); + (*str_val)[*str_len] = '\0'; + } LY_CHECK_ERR_RET(ret = lydcbor_check_string(ctx, *str_val, *str_len), free(*str_val); *str_val = NULL, ret); break; case CBOR_TYPE_FLOAT_CTRL: diff --git a/tests/utests/data/test_cbor.c b/tests/utests/data/test_cbor.c index 1f0d3143f0..76b2a875e5 100644 --- a/tests/utests/data/test_cbor.c +++ b/tests/utests/data/test_cbor.c @@ -450,6 +450,9 @@ test_invalid_input(void **state) CHECK_PARSE_LYD_PARAM_LEN("\xa1\x70" "cbor-test:lf-str" "\x62" "a\xff", 21, LYD_CBOR, LYD_PARSE_ONLY, 0, LY_EVALID, tree); CHECK_LOG_CTX("Invalid character 0xff.", "/cbor-test:lf-str", 1); + CHECK_PARSE_LYD_PARAM_LEN("\xa1\x70" "cbor-test:lf-str" "\x7f" "\x62" "ab" "\x63" "\0cd" "\xff", 27, LYD_CBOR, + LYD_PARSE_ONLY, 0, LY_EVALID, tree); + CHECK_LOG_CTX("Invalid character 0x0.", "/cbor-test:lf-str", 1); #endif assert_int_equal(LY_EVALID, lyd_new_path(NULL, UTEST_LYCTX, "/cbor-test:lf-intg8", "1000", 0, &tree)); @@ -494,6 +497,9 @@ test_restconf_notification(void **state) "\xa1" CBOR_ENVP_KEY "\xa2" CBOR_NOTIF CBOR_EVENT_TIME, /* indefinite-length map, as the libyang CBOR printer encodes maps */ "\xa1" CBOR_ENVP_KEY "\xbf" CBOR_NOTIF CBOR_EVENT_TIME "\xff", + /* indefinite-length strings */ + "\xa1" "\x7f" "\x6e" "ietf-restconf:" "\x6c" "notification" "\xff" + "\xa2" "\x69" "eventTime" "\x7f" "\x6a" "2013-12-21" "\x6a" "T00:01:00Z" "\xff" CBOR_NOTIF, }; for (i = 0; i < sizeof tests / sizeof *tests; ++i) {