From 9e309c635a57a8c157af574daf57e61940c24bbd Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Thu, 12 Nov 2026 09:08:35 -0500 Subject: [PATCH] Fixed crash on JSON null values in host_specific.json Reject null scalar values and keep loading valid entries. Changelog: Title Ticket: none Signed-off-by: Daniel JB Clark --- libpromises/cmdb.c | 23 +++++-- .../06_host_specific_data/13-null-values.cf | 61 +++++++++++++++++++ .../13-null-values.cf.json | 14 +++++ 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf create mode 100644 tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf.json diff --git a/libpromises/cmdb.c b/libpromises/cmdb.c index dd25fdc978d..cbe87e1a4ed 100644 --- a/libpromises/cmdb.c +++ b/libpromises/cmdb.c @@ -130,12 +130,23 @@ static bool AddCMDBVariable(EvalContext *ctx, const char *key, const VarRef *ref bool ret; if (JsonGetElementType(data) == JSON_ELEMENT_TYPE_PRIMITIVE) { - char *value = JsonPrimitiveToString(data); - Log(LOG_LEVEL_VERBOSE, "Installing CMDB variable '%s:%s.%s=%s'", - ref->ns, ref->scope, key, value); - ret = EvalContextVariablePutTagsSetWithComment(ctx, ref, value, CF_DATA_TYPE_STRING, - tags, comment); - free(value); + if (JsonGetPrimitiveType(data) == JSON_PRIMITIVE_TYPE_NULL) + { + /* JsonPrimitiveToString() returns NULL for JSON null and there is + * no scalar to install in its place. */ + Log(LOG_LEVEL_ERR, "Invalid value in '%s' variable specification in CMDB data" + " (null is not a supported value)", key); + ret = false; + } + else + { + char *value = JsonPrimitiveToString(data); + Log(LOG_LEVEL_VERBOSE, "Installing CMDB variable '%s:%s.%s=%s'", + ref->ns, ref->scope, key, value); + ret = EvalContextVariablePutTagsSetWithComment(ctx, ref, value, CF_DATA_TYPE_STRING, + tags, comment); + free(value); + } } else if ((JsonGetType(data) == JSON_TYPE_ARRAY) && JsonArrayContainsOnlyPrimitives(data)) diff --git a/tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf b/tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf new file mode 100644 index 00000000000..33dfeba6e06 --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf @@ -0,0 +1,61 @@ +# test of the host_specific.json functionality: JSON null values are rejected +# per entry, the rest of the CMDB data is still installed (used to crash); +# lists containing null keep installing, with the nulls skipped +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: + "command" + string => "$(sys.cf_promises) --show-vars -f $(sys.inputdir)/promises.cf | $(G.grep) -E 'CMDB data|value_from_|null_'"; + + # The offending entries are reported in the order they appear in the CMDB + # data, the remaining variables are installed and shown by --show-vars; + # lists with a null still install with the nulls skipped (RlistFromContainer() + # drops them), exactly as before the null values were rejected. + "expected" + string => concat( + "\s*error: Invalid value in 'null_scalar'.*null is not a supported value.*", + "\s+error: Invalid value in 'null_value'.*null is not a supported value.*", + "\s+error: Invalid value in 'null_bare'.*null is not a supported value.*", + "\s+data:variables\.good_variables\s+value_from_variables\s+source=cmdb", + "\s+data:variables\.good_vars\s+value_from_vars\s+source=cmdb", + "\s+data:variables\.null_container\s+\{\s*\"nested\":\s*null\s*\}\s+source=cmdb", + "\s+data:variables\.null_list\s+\{\"a\"\}\s+source=cmdb", + "\s+data:variables\.null_value_list\s+\{\"a\"\}\s+source=cmdb\s*" + ); + + methods: + "" + usebundle => dcs_passif_output( + $(expected), "", $(command), $(this.promise_filename) + ); +} diff --git a/tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf.json b/tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf.json new file mode 100644 index 00000000000..92ca7ac753a --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf.json @@ -0,0 +1,14 @@ +{ + "vars": { + "null_scalar": null, + "null_list": ["a", null], + "good_vars": "value_from_vars", + "null_container": { "nested": null } + }, + "variables": { + "null_value": { "value": null }, + "null_bare": null, + "null_value_list": { "value": ["a", null] }, + "good_variables": { "value": "value_from_variables" } + } +}