Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 154 additions & 8 deletions libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ void JsonWriteCompact(Writer *const w, const JsonElement *const element)
static JsonParseError JsonParseAsObject(
void *lookup_context,
JsonLookup *lookup_function,
bool reject_duplicates,
const char **data,
JsonElement **json_out);

Expand Down Expand Up @@ -2176,7 +2177,9 @@ const char *JsonParseErrorToString(const JsonParseError error)
"CFEngine was not built with libyaml support",
[JSON_PARSE_ERROR_LIBYAML_FAILURE] = "libyaml internal failure",
[JSON_PARSE_ERROR_NO_SUCH_FILE] = "No such file or directory",
[JSON_PARSE_ERROR_NO_DATA] = "No data"};
[JSON_PARSE_ERROR_NO_DATA] = "No data",
[JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY] =
"Unwilling to parse json data with a duplicate object member"};

return parse_errors[error];
}
Expand Down Expand Up @@ -2440,6 +2443,7 @@ static JsonParseError JsonParseAsPrimitive(
static JsonParseError JsonParseAsArray(
void *const lookup_context,
JsonLookup *const lookup_function,
const bool reject_duplicates,
const char **const data,
JsonElement **const json_out)
{
Expand Down Expand Up @@ -2490,7 +2494,11 @@ static JsonParseError JsonParseAsArray(
}
JsonElement *child_array = NULL;
JsonParseError err = JsonParseAsArray(
lookup_context, lookup_function, data, &child_array);
lookup_context,
lookup_function,
reject_duplicates,
data,
&child_array);
if (err != JSON_PARSE_OK)
{
JsonDestroy(array);
Expand All @@ -2511,7 +2519,11 @@ static JsonParseError JsonParseAsArray(
}
JsonElement *child_object = NULL;
JsonParseError err = JsonParseAsObject(
lookup_context, lookup_function, data, &child_object);
lookup_context,
lookup_function,
reject_duplicates,
data,
&child_object);
if (err != JSON_PARSE_OK)
{
JsonDestroy(array);
Expand Down Expand Up @@ -2618,9 +2630,22 @@ static const char *unquoted_key_with_colon(const char *data)
return data + i;
}

/**
* Does the object under construction already hold a member called key?
* JsonObjectAppendElement() would silently replace it.
*/
static bool JsonParseIsDuplicateKey(
const bool reject_duplicates,
const JsonElement *const object,
const char *const key)
{
return reject_duplicates && JsonObjectGet(object, key) != NULL;
}

static JsonParseError JsonParseAsObject(
void *const lookup_context,
JsonLookup *const lookup_function,
const bool reject_duplicates,
const char **const data,
JsonElement **const json_out)
{
Expand Down Expand Up @@ -2685,6 +2710,13 @@ static JsonParseError JsonParseAsObject(
return err;
}
assert(property_name);
if (JsonParseIsDuplicateKey(
reject_duplicates, object, property_name))
{
free(property_name);
JsonDestroy(object);
return JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY;
}
}
break;

Expand Down Expand Up @@ -2712,7 +2744,11 @@ static JsonParseError JsonParseAsObject(
{
JsonElement *child_array = NULL;
JsonParseError err = JsonParseAsArray(
lookup_context, lookup_function, data, &child_array);
lookup_context,
lookup_function,
reject_duplicates,
data,
&child_array);
if (err != JSON_PARSE_OK)
{
free(property_name);
Expand All @@ -2737,7 +2773,11 @@ static JsonParseError JsonParseAsObject(
{
JsonElement *child_object = NULL;
JsonParseError err = JsonParseAsObject(
lookup_context, lookup_function, data, &child_object);
lookup_context,
lookup_function,
reject_duplicates,
data,
&child_object);
if (err != JSON_PARSE_OK)
{
free(property_name);
Expand Down Expand Up @@ -2792,6 +2832,13 @@ static JsonParseError JsonParseAsObject(
property_name = xstrndup(*data, ws - *data);
*data = colon;

if (JsonParseIsDuplicateKey(
reject_duplicates, object, property_name))
{
free(property_name);
JsonDestroy(object);
return JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY;
}
break;
}
else if (property_name != NULL)
Expand Down Expand Up @@ -2888,9 +2935,10 @@ JsonParseError JsonParseAll(const char **const data, JsonElement **const json_ou
return error;
}

JsonParseError JsonParseWithLookup(
static JsonParseError JsonParseDocument(
void *const lookup_context,
JsonLookup *const lookup_function,
const bool reject_duplicates,
const char **const data,
JsonElement **const json_out)
{
Expand All @@ -2906,12 +2954,20 @@ JsonParseError JsonParseWithLookup(
if (**data == '{')
{
return JsonParseAsObject(
lookup_context, lookup_function, data, json_out);
lookup_context,
lookup_function,
reject_duplicates,
data,
json_out);
}
else if (**data == '[')
{
return JsonParseAsArray(
lookup_context, lookup_function, data, json_out);
lookup_context,
lookup_function,
reject_duplicates,
data,
json_out);
}
else if (IsWhitespace(**data))
{
Expand All @@ -2926,6 +2982,48 @@ JsonParseError JsonParseWithLookup(
return JSON_PARSE_ERROR_NO_DATA;
}

JsonParseError JsonParseWithLookup(
void *const lookup_context,
JsonLookup *const lookup_function,
const char **const data,
JsonElement **const json_out)
{
return JsonParseDocument(
lookup_context, lookup_function, false, data, json_out);
}

JsonParseError JsonParseStrict(
const char **const data, JsonElement **const json_out)
{
assert(json_out != NULL);
*json_out = NULL;

const JsonParseError error =
JsonParseDocument(NULL, NULL, true, data, json_out);
if (error != JSON_PARSE_OK)
{
return error;
}

/* Like JsonParseAll(), the parser has left data on the last byte it
* used, so only whitespace may follow until the terminating null. */
const char *ch = *data;
if (*ch != '\0')
{
ch++;
}
for (; *ch != '\0'; ch++)
{
if (!IsWhitespace(*ch))
{
JsonDestroy(*json_out);
*json_out = NULL;
return JSON_PARSE_ERROR_INVALID_END;
}
}
return JSON_PARSE_OK;
}

JsonParseError JsonParseAnyFile(
const char *const path,
const size_t size_max,
Expand Down Expand Up @@ -2970,6 +3068,54 @@ JsonParseError JsonParseFile(
return JsonParseAnyFile(path, size_max, json_out, false);
}

JsonParseError JsonParseFileStrict(
const char *const path,
const size_t size_max,
JsonElement **const json_out)
{
assert(json_out != NULL);
*json_out = NULL;

const int fd = safe_open(path, O_RDONLY);
if (fd == -1)
{
return JSON_PARSE_ERROR_NO_SUCH_FILE;
}

bool truncated = false;
Writer *const contents = FileReadFromFd(fd, size_max, &truncated);
struct stat sb;
const bool have_size = (fstat(fd, &sb) == 0);
close(fd);
if (contents == NULL)
{
return JSON_PARSE_ERROR_NO_SUCH_FILE;
}
if (truncated)
{
WriterClose(contents);
return JSON_PARSE_ERROR_TRUNCATED;
}

const char *data = StringWriterData(contents);
JsonParseError error;
if (!have_size || (size_t) sb.st_size != StringWriterLength(contents))
{
/* The Writer is a C string: a null byte ends it early and hides
* the rest of the file from the parser, so fewer bytes arrive than
* the file holds. A file that changed while it was read is refused
* too. */
error = JSON_PARSE_ERROR_INVALID_END;
}
else
{
error = JsonParseStrict(&data, json_out);
}

WriterClose(contents);
return error;
}

bool JsonWalk(JsonElement *element,
JsonElementVisitor object_visitor,
JsonElementVisitor array_visitor,
Expand Down
30 changes: 30 additions & 0 deletions libutils/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ typedef enum
JSON_PARSE_ERROR_NO_SUCH_FILE,
JSON_PARSE_ERROR_NO_DATA,
JSON_PARSE_ERROR_TRUNCATED,
JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY,

JSON_PARSE_ERROR_MAX
} JsonParseError;
Expand Down Expand Up @@ -604,6 +605,22 @@ JsonParseError JsonParse(const char **data, JsonElement **json_out);
*/
JsonParseError JsonParseAll(const char **data, JsonElement **json_out);

/**
@brief Parse the whole string, refusing input a lenient parse would repair
@param data [in] Pointer to the string to parse
@param json_out Resulting JSON object, NULL on error
@note In contrast to JsonParse() and JsonParseAll(), a document with two
members of the same name in one object, at any depth, is refused
with JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY before the earlier
member is replaced, rather than silently keeping the last one.
As in JsonParseAll(), trailing non-whitespace characters are refused
with JSON_PARSE_ERROR_INVALID_END. Names are compared as the parser
decodes them, which is the comparison JsonObjectGet() makes, so no
member is ever replaced.
@returns See JsonParseError and JsonParseErrorToString
*/
JsonParseError JsonParseStrict(const char **data, JsonElement **json_out);

/**
@brief Parse a string to create a JsonElement
@param lookup_data [in] Evaluation context for variable lookups
Expand Down Expand Up @@ -645,6 +662,19 @@ JsonParseError JsonParseAnyFile(
JsonParseError JsonParseFile(
const char *path, size_t size_max, JsonElement **json_out);

/**
* @brief Parse JSON from a file as JsonParseStrict() parses a string.
* @param path Path to the file
* @param size_max Maximum size to read in memory; a larger file is refused
* with JSON_PARSE_ERROR_TRUNCATED, as in JsonParseFile()
* @param json_out Resulting JSON object, NULL on error
* @note A null byte in the file is refused with JSON_PARSE_ERROR_INVALID_END,
* as it would otherwise end the text early and hide what follows.
* @return See JsonParseError and JsonParseErrorToString
*/
JsonParseError JsonParseFileStrict(
const char *path, size_t size_max, JsonElement **json_out);

const char *JsonParseErrorToString(JsonParseError error);


Expand Down
Loading
Loading