From ba4b4c4b0acfbe2cd0e5a1f3d9fb47506b986af7 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 15:36:22 -0500 Subject: [PATCH 1/4] Made CMDB class insertion fail closed and rollback restore state A class the insertion API could not insert was treated as one that already existed, so {"vars":{"good":"x"},"classes":{"default: ":"any::"}} installed the variable and returned true. EvalContextClassPutTagsSet() chops trailing whitespace off a copy of the name for its emptiness test, refusing a default-namespace class of whitespace only, but files the class under the name as given. - A class name with whitespace at either end is invalid. - A class that exists is found by its canonical identity (namespace, canonified name) before inserting, not by the raw name, so 'pre-class' no longer replaces 'pre_class'. Existing classes stay benign and are left alone and never rolled back. - Any other failed class insertion rolls back what the call installed and makes LoadCMDBData() return false. The CMDBTestFailInsertion seam now covers classes too. - A variable that exists already makes the file invalid, so rollback only ever removes what the load itself added. The loader runs on a cleared context, before augments, so nothing legitimate is refused. Changelog: None Ticket: none Co-Authored-By: Claude Sonnet 5.5 --- libpromises/cmdb.c | 146 ++++++++++++++++++++++++++++++------- libpromises/cmdb.h | 6 +- tests/unit/cmdb_test.c | 161 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+), 28 deletions(-) diff --git a/libpromises/cmdb.c b/libpromises/cmdb.c index 9b624ac9679..e3759b53fbc 100644 --- a/libpromises/cmdb.c +++ b/libpromises/cmdb.c @@ -749,7 +749,15 @@ static const char *CMDBClassNameOfKey(const char *key) return (sep == NULL) ? key : (sep + 1); } -/** Does EvalContextClassPutTagsSet() have a name it can work with? */ +/** + * Does EvalContextClassPutTagsSet() have a name it can work with? + * + * That function chops trailing whitespace off a copy of the name and refuses + * the class if nothing is left (in the default namespace), yet files the class + * under the name as given, whitespace canonified to '_'. A name with + * whitespace at either end is thus either refused there or filed under a name + * its author did not write, so it is refused here. + */ static bool CheckCMDBClassName(const char *key) { const char *name = CMDBClassNameOfKey(key); @@ -758,6 +766,14 @@ static bool CheckCMDBClassName(const char *key) Log(LOG_LEVEL_ERR, "Invalid CMDB class '%s': the class name is empty", key); return false; } + if (isspace((unsigned char) name[0]) || + isspace((unsigned char) name[strlen(name) - 1])) + { + Log(LOG_LEVEL_ERR, + "Invalid CMDB class '%s': the class name starts or ends with whitespace", + key); + return false; + } if (strlen(name) >= CF_MAXVARSIZE) { Log(LOG_LEVEL_ERR, @@ -774,6 +790,13 @@ static bool AddCMDBClass(EvalContext *ctx, const char *key, StringSet *tags, con assert(key != NULL); assert(tags != NULL); + if (CMDBTestFailInsertion != NULL && CMDBTestFailInsertion(key)) + { + Log(LOG_LEVEL_ERR, "Class '%s' cannot be added (insertion failure forced by a test)", key); + StringSetDestroy(tags); + return false; + } + bool ret; Log(LOG_LEVEL_VERBOSE, "Installing CMDB class '%s'", key); @@ -938,10 +961,76 @@ static bool PrepareCMDBClasses(JsonElement *classes, const char *file_path, return all_valid; } +/** + * The namespace and name EvalContextClassPutTagsSet() files a CMDB class + * under: the key split the way AddCMDBClass() splits it, the name canonified + * the way ClassInit() does. Both results are owned by the caller. + */ +static void CMDBClassIdentity(const char *key, char **ns, char **name) +{ + const char *sep = strchr(key, ':'); + *ns = (sep != NULL) ? xstrndup(key, sep - key) : xstrdup(CMDB_NAMESPACE); + /* Not CanonifyName(): it returns a static buffer */ + *name = xstrdup(CMDBClassNameOfKey(key)); + CanonifyNameInPlace(*name); +} + +/** + * Is the class there already? EvalContextClassPutTagsSet() decides that by the + * name as given, so 'pre-class' is not found where 'pre_class' is, and the + * existing class would be replaced; ask by the canonical identity instead. + */ +static bool CMDBClassExists(const EvalContext *ctx, const char *key) +{ + char *ns; + char *name; + CMDBClassIdentity(key, &ns, &name); + const Class *existing = EvalContextClassGet(ctx, ns, name); + free(ns); + free(name); + return (existing != NULL && existing->scope == CONTEXT_SCOPE_NAMESPACE); +} + +/** + * Refuse a variable that exists already: installing it would replace the + * existing one, and RollbackCMDBPending() could not put that one back. + * Nothing is there to be replaced in practice, as the loader runs on a + * cleared context, before augments. + * + * @return false (logged) if any variable exists already + */ +static bool CMDBPendingCheckNoExistingVariables(const EvalContext *ctx, + const CMDBPending *pending) +{ + bool valid = true; + for (size_t i = 0; i < SeqLength(pending->variables); i++) + { + const CMDBPendingVariable *var = SeqAt(pending->variables, i); + /* The value of an empty list is NULL, so go by the type */ + DataType type = CF_DATA_TYPE_NONE; + EvalContextVariableGetPlaintext(ctx, var->ref, &type); + if (type != CF_DATA_TYPE_NONE) + { + char *address = VarRefToString(var->ref, true); + Log(LOG_LEVEL_ERR, + "Invalid variable specification in CMDB data: '%s' names the" + " variable '%s' that exists already", var->key, address); + free(address); + valid = false; + } + } + return valid; +} + /** * Take back what InstallCMDBPending() installed so far: the first * variables_installed variables and every class marked installed. A class * that was already there was not installed by this load and is left alone. + * + * Removing is restoring only because nothing it removes was there before: + * LoadCMDBData() refuses a variable that exists already, and + * InstallCMDBPending() does not touch a class that does (by its canonical + * identity, so an alias of one is left alone too). */ static void RollbackCMDBPending(EvalContext *ctx, CMDBPending *pending, size_t variables_installed) @@ -960,20 +1049,12 @@ static void RollbackCMDBPending(EvalContext *ctx, CMDBPending *pending, continue; } - /* Split the key and name the class the way AddCMDBClass() put it */ - char *ns = NULL; - const char *name = cls->key; - const char *sep = strchr(cls->key, ':'); - if (sep != NULL) - { - ns = xstrndup(cls->key, sep - cls->key); - name = sep + 1; - } - char *canonical = xstrdup(name); /* CanonifyName() returns a static buffer */ - CanonifyNameInPlace(canonical); - EvalContextClassRemove(ctx, (ns != NULL) ? ns : CMDB_NAMESPACE, canonical); - free(canonical); + char *ns; + char *name; + CMDBClassIdentity(cls->key, &ns, &name); + EvalContextClassRemove(ctx, ns, name); free(ns); + free(name); } } @@ -983,27 +1064,37 @@ static void RollbackCMDBPending(EvalContext *ctx, CMDBPending *pending, * that already exists is not an error and is left as it is. * * All or nothing in its own right, should the validation have a gap: at the - * first variable that cannot be installed, everything this call installed is - * removed again and false is returned. + * first class or variable that cannot be installed, everything this call + * installed is removed again and false is returned. * - * @return false if a variable could not be installed after all (details are - * logged); nothing is left installed then + * @return false if a class or variable could not be installed after all + * (details are logged); nothing is left installed then */ static bool InstallCMDBPending(EvalContext *ctx, CMDBPending *pending) { - /* Classes first: they cannot fail, and installing them before the - * variables lets a failing variable exercise the rollback of both. */ + /* Classes first: installing them before the variables lets a failing + * variable exercise the rollback of both. */ for (size_t i = 0; i < SeqLength(pending->classes); i++) { CMDBPendingClass *cls = SeqAt(pending->classes, i); + if (CMDBClassExists(ctx, cls->key)) + { + /* Not an error, and not ours to replace or to take back */ + Log(LOG_LEVEL_VERBOSE, + "Not adding CMDB class '%s', it exists already", cls->key); + continue; + } + cls->installed = AddCMDBClass(ctx, cls->key, cls->tags, cls->comment); cls->tags = NULL; /* consumed or destroyed by AddCMDBClass() */ if (!cls->installed) { - /* It does not have to be and error, it could be a result of - * the class already existing. Anyways, details about - * potential errors should have been logged already. */ - Log(LOG_LEVEL_DEBUG, "Did not add CMDB class '%s'", cls->key); + /* Details should have been logged already. */ + Log(LOG_LEVEL_ERR, + "Failed to add CMDB class '%s', removing what was installed from the file", + cls->key); + RollbackCMDBPending(ctx, pending, 0); + return false; } } @@ -1041,7 +1132,8 @@ static bool InstallCMDBPending(EvalContext *ctx, CMDBPending *pending) * An absent file is not an error. A top-level key other than 'vars', * 'variables' and 'classes' makes the file invalid, as does a section whose * value is not a JSON object (JSON null included), and two entries that - * name the same variable (one would silently replace the other). + * name the same variable (one would silently replace the other), or a + * variable that exists already. A class that exists already is left as it is. */ bool LoadCMDBData(EvalContext *ctx) { @@ -1120,6 +1212,10 @@ bool LoadCMDBData(EvalContext *ctx) { valid = false; } + if (!CMDBPendingCheckNoExistingVariables(ctx, &pending)) + { + valid = false; + } /* Step 2: install, but only if the whole file is good. */ bool success = false; diff --git a/libpromises/cmdb.h b/libpromises/cmdb.h index 1322b8c09c7..9510982704e 100644 --- a/libpromises/cmdb.h +++ b/libpromises/cmdb.h @@ -34,9 +34,9 @@ JsonElement *ReadJsonFile(const char *filename, LogLevel log_level, size_t size_ bool LoadCMDBData(EvalContext *ctx); /** - * Test seam, NULL in production: when set, a variable whose CMDB key makes it - * return true is treated as one that cannot be inserted into the EvalContext, - * which validation cannot otherwise produce. It lets a unit test exercise the + * Test seam, NULL in production: when set, a variable or class whose CMDB key + * makes it return true is treated as one that cannot be inserted into the + * EvalContext, which validation cannot otherwise produce. It lets a unit test exercise the * rollback of LoadCMDBData(). */ extern bool (*CMDBTestFailInsertion)(const char *key); diff --git a/tests/unit/cmdb_test.c b/tests/unit/cmdb_test.c index 38282320ff5..ada56424758 100644 --- a/tests/unit/cmdb_test.c +++ b/tests/unit/cmdb_test.c @@ -512,6 +512,162 @@ static void test_failed_insertion_rolls_back_everything(void) RemoveCMDBFile(); } +/* Is there a class of that name in the 'data' namespace, put there by + * something else than the CMDB (it has pre_tag, not the CMDB's tag)? */ +static bool ClassIsUntouched(const EvalContext *ctx, const char *name) +{ + const Class *cls = EvalContextClassGet(ctx, "data", name); + return (cls != NULL && + StringSetContains(cls->tags, "pre_tag") && + !StringSetContains(cls->tags, CMDB_SOURCE_TAG)); +} + +static void test_whitespace_class_name_installs_nothing(void) +{ + /* EvalContextClassPutTagsSet() refuses a default-namespace class whose + * name is only whitespace, and files "x " as "x_". The good variable and + * class beside it must not be installed either. */ + const char *const names[] = { + " ", "\\t", "default: ", "ns1: ", "trailing ", " leading", "tab\\t", + NULL + }; + const char *const absent[] = { "data:variables.good_var", NULL }; + const char *const absent_class[] = { + "good_class", "trailing_", "_leading", "tab_", NULL + }; + + for (size_t i = 0; names[i] != NULL; i++) + { + char json[CF_BUFSIZE]; + xsnprintf(json, sizeof(json), + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"good_class\": \"any::\", \"%s\": \"any::\" } }\n", + names[i]); + assert_rejected_and_nothing_installed(json, absent, absent_class); + } +} + +static void test_existing_class_is_not_an_error(void) +{ + /* A class that is there already stays as it is, and the rest of the file + * is installed; so does one that differs only in how it is spelled. */ + WriteCMDBFile( + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"pre_class\": \"any::\", \"pre-aliased\": \"any::\"," + " \"new_class\": \"any::\" } }\n"); + + EvalContext *ctx = EvalContextNew(); + assert_true(EvalContextClassPutSoftNS(ctx, "data", "pre_class", + CONTEXT_SCOPE_NAMESPACE, "pre_tag")); + assert_true(EvalContextClassPutSoftNS(ctx, "data", "pre_aliased", + CONTEXT_SCOPE_NAMESPACE, "pre_tag")); + assert_true(LoadCMDBData(ctx)); + + assert_string_equal(InstalledScalar(ctx, "data:variables.good_var"), "x"); + assert_true(ClassIsInstalled(ctx, "new_class")); + assert_false(ClassIsUntouched(ctx, "new_class")); + assert_true(ClassIsUntouched(ctx, "pre_class")); + assert_true(ClassIsUntouched(ctx, "pre_aliased")); + + EvalContextDestroy(ctx); + RemoveCMDBFile(); +} + +static void test_existing_variable_is_refused_and_left_alone(void) +{ + /* Installing it would replace it, and a rollback could only remove it: + * the file is refused as a whole and the variable keeps its value. */ + WriteCMDBFile( + "{ \"vars\": { \"first\": \"new\", \"other\": \"2\" },\n" + " \"classes\": { \"new_class\": \"any::\" } }\n"); + + EvalContext *ctx = EvalContextNew(); + VarRef *ref = VarRefParse("data:variables.first"); + assert_true(EvalContextVariablePut(ctx, ref, "old", CF_DATA_TYPE_STRING, + "pre_tag")); + VarRefDestroy(ref); + + assert_false(LoadCMDBData(ctx)); + + assert_string_equal(InstalledScalar(ctx, "data:variables.first"), "old"); + assert_false(VariableIsInstalled(ctx, "data:variables.other")); + assert_false(ClassIsInstalled(ctx, "new_class")); + + EvalContextDestroy(ctx); + RemoveCMDBFile(); +} + +static void test_rollback_removes_non_canonical_and_keeps_aliased(void) +{ + /* 'dash-class' is filed as 'dash_class' and rolled back by that name; + * 'pre-class' is the existing 'pre_class' by another spelling, so neither + * installing nor rolling back touches it. */ + WriteCMDBFile( + "{ \"vars\": { \"first\": \"1\", \"fail_me\": \"2\" },\n" + " \"classes\": { \"dash-class\": \"any::\", \"pre-class\": \"any::\" } }\n"); + + EvalContext *ctx = EvalContextNew(); + assert_true(EvalContextClassPutSoftNS(ctx, "data", "pre_class", + CONTEXT_SCOPE_NAMESPACE, "pre_tag")); + + /* Without the failure the class is there under the canonical name */ + CMDBTestFailInsertion = NULL; + assert_true(LoadCMDBData(ctx)); + assert_true(ClassIsInstalled(ctx, "dash_class")); + assert_true(ClassIsUntouched(ctx, "pre_class")); + EvalContextClear(ctx); + assert_true(EvalContextClassPutSoftNS(ctx, "data", "pre_class", + CONTEXT_SCOPE_NAMESPACE, "pre_tag")); + + CMDBTestFailInsertion = FailInsertionOfFailMe; + const bool loaded = LoadCMDBData(ctx); + CMDBTestFailInsertion = NULL; + assert_false(loaded); + + assert_false(ClassIsInstalled(ctx, "dash_class")); + assert_false(VariableIsInstalled(ctx, "data:variables.first")); + assert_true(ClassIsUntouched(ctx, "pre_class")); + + EvalContextDestroy(ctx); + RemoveCMDBFile(); +} + +static bool FailInsertionOfFailClass(const char *key) +{ + return StringEqual(key, "fail_class"); +} + +static void test_failed_class_insertion_rolls_back_everything(void) +{ + /* A class that cannot be inserted is not "one that exists": the classes + * before it are taken back, the ones after it and the variables are not + * installed, and a class that was there before is left alone. */ + WriteCMDBFile( + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"pre_class\": \"any::\", \"new-class\": \"any::\"," + " \"ns1:new_ns_class\": \"any::\"," + " \"fail_class\": \"any::\", \"late_class\": \"any::\" } }\n"); + + EvalContext *ctx = EvalContextNew(); + assert_true(EvalContextClassPutSoftNS(ctx, "data", "pre_class", + CONTEXT_SCOPE_NAMESPACE, "pre_tag")); + + CMDBTestFailInsertion = FailInsertionOfFailClass; + const bool loaded = LoadCMDBData(ctx); + CMDBTestFailInsertion = NULL; + assert_false(loaded); + + assert_false(VariableIsInstalled(ctx, "data:variables.good_var")); + assert_false(ClassIsInstalled(ctx, "new_class")); + assert_true(EvalContextClassGet(ctx, "ns1", "new_ns_class") == NULL); + assert_false(ClassIsInstalled(ctx, "fail_class")); + assert_false(ClassIsInstalled(ctx, "late_class")); + assert_true(ClassIsUntouched(ctx, "pre_class")); + + EvalContextDestroy(ctx); + RemoveCMDBFile(); +} + int main() { PRINT_TEST_BANNER(); @@ -536,6 +692,11 @@ int main() unit_test(test_unknown_top_level_key_installs_nothing), unit_test(test_duplicate_variable_installs_nothing), unit_test(test_failed_insertion_rolls_back_everything), + unit_test(test_whitespace_class_name_installs_nothing), + unit_test(test_existing_class_is_not_an_error), + unit_test(test_existing_variable_is_refused_and_left_alone), + unit_test(test_rollback_removes_non_canonical_and_keeps_aliased), + unit_test(test_failed_class_insertion_rolls_back_everything), }; int ret = run_tests(tests); From 60979c9db1c60309f70e1063c0a6c18283df282c Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 15:57:20 -0500 Subject: [PATCH 2/4] Updated libntech to get the strict JSON parse entry points Brings in JsonParseStrict() and JsonParseFileStrict() (djbclark/libntech tendcf-integration cc892efae, from fix/json-strict-parse), which refuse a duplicate object member and trailing input after the document. The commits are on the fork's tendcf-integration, not upstream NorthernTechHQ/libntech. Changelog: None Ticket: none Co-Authored-By: Claude Sonnet 5.5 --- libntech | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libntech b/libntech index d379dce2cf7..cc892efae25 160000 --- a/libntech +++ b/libntech @@ -1 +1 @@ -Subproject commit d379dce2cf79c4b95112cce482797e93ddcad5dc +Subproject commit cc892efae2597ea99749880c5eed01544018a4e6 From 9e5371b971791b5447e6bc1f08ac2ee132e72537 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 15:58:59 -0500 Subject: [PATCH 3/4] Made the CMDB loader refuse repeated members and trailing input host_specific.json was parsed with JsonParseFile(): a repeated object member replaced the earlier one before validation saw it ({"vars":{ "good":null,"good":"ok"}} loaded), and anything after the first complete document was ignored. LoadCMDBData() now reads the file with JsonParseFileStrict() and rejects either, installing nothing. ReadJsonFile() keeps its behaviour for the augments callers; both share a static helper that takes the strict flag. Also spells out the headers cmdb.c and cmdb_test.c used through others (class.h, ctype.h, set.h, string_lib.h, var_expressions.h). Unit tests: a repeated member at the top level, in classes, in variables and in a nested object, with an invalid earlier duplicate; trailing {}, a letter and a bracket; whitespace after the document still loads. Changelog: None Ticket: none Co-Authored-By: Claude Sonnet 5.5 --- libpromises/cmdb.c | 29 +++++++++++++++--- tests/unit/cmdb_test.c | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/libpromises/cmdb.c b/libpromises/cmdb.c index e3759b53fbc..f7d9ca45665 100644 --- a/libpromises/cmdb.c +++ b/libpromises/cmdb.c @@ -23,6 +23,7 @@ */ #include +#include /* isspace() */ #include #include #include /* StringSet */ @@ -32,6 +33,7 @@ #include /* GetDataDir() */ #include /* VarRef, StringContainsUnresolved() */ #include /* EvalContext*() */ +#include /* Class */ #include /* JoinPaths() */ #include @@ -49,12 +51,20 @@ bool (*CMDBTestFailInsertion)(const char *key) = NULL; -JsonElement *ReadJsonFile(const char *filename, LogLevel log_level, size_t size_max) +/** + * @param strict refuse what JsonParseFile() would repair or ignore: a repeated + * object member (it replaces the earlier one) and anything but + * whitespace after the document (it is not looked at) + */ +static JsonElement *ReadJsonFileMode(const char *filename, LogLevel log_level, + size_t size_max, bool strict) { assert(filename != NULL); JsonElement *doc = NULL; - JsonParseError err = JsonParseFile(filename, size_max, &doc); + JsonParseError err = strict ? + JsonParseFileStrict(filename, size_max, &doc) : + JsonParseFile(filename, size_max, &doc); if (err == JSON_PARSE_ERROR_NO_SUCH_FILE) { @@ -71,6 +81,11 @@ JsonElement *ReadJsonFile(const char *filename, LogLevel log_level, size_t size_ return doc; } +JsonElement *ReadJsonFile(const char *filename, LogLevel log_level, size_t size_max) +{ + return ReadJsonFileMode(filename, log_level, size_max, false); +} + /* What made the unexpanded-variable walk below stop: exactly one of these is * set, depending on which visitor stopped it. Both point into the JSON * document being walked and must not be freed. */ @@ -1121,7 +1136,8 @@ static bool InstallCMDBPending(EvalContext *ctx, CMDBPending *pending) * Load host_specific.json into the EvalContext, all or nothing. * * If the file is there but anything in it is unusable (unreadable, not - * valid JSON, not an object, a section of the wrong type, or any single + * valid JSON, a repeated member in any object, anything but whitespace after + * the document, not an object, a section of the wrong type, or any single * entry of 'vars', 'variables' or 'classes' that cannot be validated or * installed), nothing at all is installed from it, every problem found is * logged, and false is returned. A policy that guards an action by data it @@ -1152,10 +1168,13 @@ bool LoadCMDBData(EvalContext *ctx) return false; } - JsonElement *data = ReadJsonFile(file_path, LOG_LEVEL_ERR, HOST_SPECIFIC_DATA_MAX_SIZE); + /* Strict: a repeated member would have replaced the earlier one, and + * trailing input would have been ignored, before any validation saw it. */ + JsonElement *data = ReadJsonFileMode(file_path, LOG_LEVEL_ERR, + HOST_SPECIFIC_DATA_MAX_SIZE, true); if (data == NULL) { - /* Details are logged by ReadJsonFile() */ + /* Details are logged by ReadJsonFileMode() */ return false; } if (JsonGetType(data) != JSON_TYPE_OBJECT) diff --git a/tests/unit/cmdb_test.c b/tests/unit/cmdb_test.c index ada56424758..708d6e7b96c 100644 --- a/tests/unit/cmdb_test.c +++ b/tests/unit/cmdb_test.c @@ -1,6 +1,10 @@ #include #include +#include /* Class */ +#include /* StringSetContains */ +#include /* StringEqual */ +#include /* VarRefParse */ #include #include #include /* xsnprintf */ @@ -668,6 +672,69 @@ static void test_failed_class_insertion_rolls_back_everything(void) RemoveCMDBFile(); } +static void test_repeated_member_or_trailing_input_installs_nothing(void) +{ + /* What the lenient parser repaired or ignored before validation saw it: + * a repeated member replaced the earlier one (an invalid earlier value + * vanished, or a whole section did), and anything after the document was + * not looked at. Each file has a good variable and class next to it. */ + const char *const absent[] = { + "data:variables.good_var", "data:variables.v", "data:variables.good", + NULL + }; + const char *const absent_class[] = { "good_class", "c", NULL }; + const char *const files[] = { + /* repeated member at the top level (section replaced) */ + "{ \"vars\": { \"good_var\": \"x\" }, \"vars\": { \"good\": \"y\" },\n" + " \"classes\": { \"good_class\": \"any::\" } }\n", + /* repeated member, the earlier one invalid */ + "{ \"vars\": { \"good_var\": \"x\", \"good\": null, \"good\": \"ok\" },\n" + " \"classes\": { \"good_class\": \"any::\" } }\n", + /* repeated member in 'classes' */ + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"good_class\": \"any::\", \"c\": \"any::\"," + " \"c\": \"any::\" } }\n", + /* repeated member in 'variables', and in a nested object */ + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"variables\": { \"v\": { \"value\": \"1\" }, \"v\": { \"value\": \"2\" } },\n" + " \"classes\": { \"good_class\": \"any::\" } }\n", + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"variables\": { \"v\": { \"value\": \"1\", \"value\": \"2\" } },\n" + " \"classes\": { \"good_class\": \"any::\" } }\n", + /* trailing input */ + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"good_class\": \"any::\" } } {}\n", + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"good_class\": \"any::\" } }x\n", + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"variables\": {},\n" + " \"classes\": { \"good_class\": \"any::\" } } ]\n", + NULL + }; + + for (size_t i = 0; files[i] != NULL; i++) + { + assert_rejected_and_nothing_installed(files[i], absent, absent_class); + } +} + +static void test_whitespace_after_document_is_fine(void) +{ + /* The control for the above: the same kind of file, nothing repeated and + * nothing after the document but whitespace, loads. */ + WriteCMDBFile( + "{ \"vars\": { \"good_var\": \"x\" },\n" + " \"classes\": { \"good_class\": \"any::\" } } \n\t\n\n"); + + EvalContext *ctx = EvalContextNew(); + assert_true(LoadCMDBData(ctx)); + assert_string_equal(InstalledScalar(ctx, "data:variables.good_var"), "x"); + assert_true(ClassIsInstalled(ctx, "good_class")); + + EvalContextDestroy(ctx); + RemoveCMDBFile(); +} + int main() { PRINT_TEST_BANNER(); @@ -697,6 +764,8 @@ int main() unit_test(test_existing_variable_is_refused_and_left_alone), unit_test(test_rollback_removes_non_canonical_and_keeps_aliased), unit_test(test_failed_class_insertion_rolls_back_everything), + unit_test(test_repeated_member_or_trailing_input_installs_nothing), + unit_test(test_whitespace_after_document_is_fine), }; int ret = run_tests(tests); From b751fad5f38d2ca1aa62e87583cccc1afb935bcd Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 15:59:01 -0500 Subject: [PATCH 4/4] Added acceptance tests for repeated members and trailing input in CMDB data Each file has a good variable and class beside the offending text; the agent run must report the parse refusal and define neither. Changelog: None Ticket: none Co-Authored-By: Claude Sonnet 5.5 --- .../17-duplicate-member-installs-nothing.cf | 93 +++++++++++++++++++ ...-duplicate-member-installs-nothing.cf.json | 10 ++ .../18-trailing-input-installs-nothing.cf | 93 +++++++++++++++++++ ...18-trailing-input-installs-nothing.cf.json | 9 ++ 4 files changed, 205 insertions(+) create mode 100644 tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf create mode 100644 tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf.json create mode 100644 tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf create mode 100644 tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf.json diff --git a/tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf b/tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf new file mode 100644 index 00000000000..824c7b05fc4 --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf @@ -0,0 +1,93 @@ +# test that a repeated object member makes the CMDB loader install nothing from +# host_specific.json: the parser used to keep only the last of the two, so the +# file looked valid and the discarded entry was never checked +body common control +{ + inputs => { "../../default.sub.cf" }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +####################################################### +bundle agent init +{ + files: + "$(sys.workdir)$(const.dirsep)data$(const.dirsep)." + create => "true", + perms => mog( + "0700", "$(sys.user_data[username])", "$(sys.user_data[username])" + ); + + methods: + "prepare_host_specific_data" + usebundle => file_copy( + "$(this.promise_filename).json", + "$(sys.workdir)$(const.dirsep)data$(const.dirsep)host_specific.json" + ), + handle => "prepare_host_specific_data"; + + "empty_promises_cf" + usebundle => file_make("$(sys.inputdir)/promises.cf", ''); +} + +####################################################### +bundle agent check +{ + vars: + "error_output" + string => execresult( + "$(sys.cf_promises) --show-classes -f $(sys.inputdir)/promises.cf", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + "vars_output" + string => execresult( + "$(sys.cf_promises) --show-vars -f $(sys.inputdir)/promises.cf | $(G.grep) good_key", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + "classes_output" + string => execresult( + "$(sys.cf_promises) --show-classes -f $(sys.inputdir)/promises.cf | $(G.grep) cmdb_good_class", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + classes: + # The file is refused by the parser, and the loader says so. + "refusal_reported" + expression => regcmp( + ".*error: Could not parse JSON file .*host_specific\.json: Unwilling to parse json data with a duplicate object member.*", + "$(error_output)" + ); + + # Nothing is installed from it: neither the variable nor the class next + # to the offending text. + "good_entry_absent" + not => regcmp( + ".*good_key.*", + "$(vars_output)" + ); + + "good_class_absent" + not => regcmp( + ".*cmdb_good_class.*", + "$(classes_output)" + ); + + "ok" and => { "refusal_reported", "good_entry_absent", "good_class_absent" }; + + reports: + ok:: + "$(this.promise_filename) Pass"; + + !ok:: + "$(this.promise_filename) FAIL"; + + !ok.DEBUG:: + "error_output was: '$(error_output)'"; + "vars_output was: '$(vars_output)'"; + "classes_output was: '$(classes_output)'"; +} diff --git a/tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf.json b/tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf.json new file mode 100644 index 00000000000..93c9028821e --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/17-duplicate-member-installs-nothing.cf.json @@ -0,0 +1,10 @@ +{ + "vars": { + "good_key": "a literal value", + "repeated_key": "first", + "repeated_key": "second" + }, + "classes": { + "ns1:cmdb_good_class": "any::" + } +} diff --git a/tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf b/tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf new file mode 100644 index 00000000000..4f1c2d670b7 --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf @@ -0,0 +1,93 @@ +# test that input after the document makes the CMDB loader install nothing from +# host_specific.json: the parser used to stop after the first complete object +# and never look at what followed +body common control +{ + inputs => { "../../default.sub.cf" }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +####################################################### +bundle agent init +{ + files: + "$(sys.workdir)$(const.dirsep)data$(const.dirsep)." + create => "true", + perms => mog( + "0700", "$(sys.user_data[username])", "$(sys.user_data[username])" + ); + + methods: + "prepare_host_specific_data" + usebundle => file_copy( + "$(this.promise_filename).json", + "$(sys.workdir)$(const.dirsep)data$(const.dirsep)host_specific.json" + ), + handle => "prepare_host_specific_data"; + + "empty_promises_cf" + usebundle => file_make("$(sys.inputdir)/promises.cf", ''); +} + +####################################################### +bundle agent check +{ + vars: + "error_output" + string => execresult( + "$(sys.cf_promises) --show-classes -f $(sys.inputdir)/promises.cf", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + "vars_output" + string => execresult( + "$(sys.cf_promises) --show-vars -f $(sys.inputdir)/promises.cf | $(G.grep) good_key", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + "classes_output" + string => execresult( + "$(sys.cf_promises) --show-classes -f $(sys.inputdir)/promises.cf | $(G.grep) cmdb_good_class", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + classes: + # The file is refused by the parser, and the loader says so. + "refusal_reported" + expression => regcmp( + ".*error: Could not parse JSON file .*host_specific\.json: Unwilling to parse json data with trailing non-whitespace characters.*", + "$(error_output)" + ); + + # Nothing is installed from it: neither the variable nor the class next + # to the offending text. + "good_entry_absent" + not => regcmp( + ".*good_key.*", + "$(vars_output)" + ); + + "good_class_absent" + not => regcmp( + ".*cmdb_good_class.*", + "$(classes_output)" + ); + + "ok" and => { "refusal_reported", "good_entry_absent", "good_class_absent" }; + + reports: + ok:: + "$(this.promise_filename) Pass"; + + !ok:: + "$(this.promise_filename) FAIL"; + + !ok.DEBUG:: + "error_output was: '$(error_output)'"; + "vars_output was: '$(vars_output)'"; + "classes_output was: '$(classes_output)'"; +} diff --git a/tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf.json b/tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf.json new file mode 100644 index 00000000000..55016997715 --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/18-trailing-input-installs-nothing.cf.json @@ -0,0 +1,9 @@ +{ + "vars": { + "good_key": "a literal value" + }, + "classes": { + "ns1:cmdb_good_class": "any::" + } +} +{ "vars": { "other_key": "never looked at" } }