Skip to content

eBPF-based security platform

json: reset nested seen flags for every element and document

Daniel Borkmann3f6834f5e51a
diff --git a/testing/unit/json.c b/testing/unit/json.c
index 0ede84f4..d8c7941b 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -218,6 +218,79 @@ static void nested_array_then_trailing_scalar(void)
EXPECT_STREQ(d.last_type, "init");
}
+struct nested {
+ struct json_field *meta, *tag_field;
+ int meta_seen[4], tag_seen[4];
+ char id[8], tag[8];
+ int n;
+};
+
+static int nested_on_elem(void *ctx)
+{
+ struct nested *x = ctx;
+
+ if (x->n < 4) {
+ x->meta_seen[x->n] = x->meta->seen;
+ x->tag_seen[x->n] = x->tag_field->seen;
+ }
+ x->n++;
+ return 0;
+}
+
+static int parse_nested(const char *text, struct nested *x)
+{
+ struct json_array sink = { nested_on_elem, x };
+ struct json_field meta_sub[] = {
+ { "tag", JSON_T_STRING, x->tag, sizeof(x->tag), 0, NULL, 0 },
+ };
+ struct json_field item_sub[] = {
+ { "id", JSON_T_STRING, x->id, sizeof(x->id), 0, NULL, 0 },
+ { "meta", JSON_T_OBJECT, NULL, 0, 0, meta_sub,
+ ARRAY_SIZE(meta_sub) },
+ };
+ struct json_field f[] = {
+ { "items", JSON_T_ARRAY, &sink, 0, 0, item_sub,
+ ARRAY_SIZE(item_sub) },
+ };
+
+ memset(x, 0, sizeof(*x));
+ x->meta = &item_sub[1];
+ x->tag_field = &meta_sub[0];
+ return json_parse(text, strlen(text), f, ARRAY_SIZE(f));
+}
+
+/*
+ * A field an element or a document leaves out is not given, however
+ * deep in the schema it sits and whatever the one before gave.
+ */
+static void seen_flags_are_the_elements_own(void)
+{
+ static const char second[] = "{}";
+ static const char first[] = "{\"o\":{\"k\":\"v\"}}";
+ char k[8];
+ struct json_field sub[] = {
+ { "k", JSON_T_STRING, k, sizeof(k), 0, NULL, 0 },
+ };
+ struct json_field top[] = {
+ { "o", JSON_T_OBJECT, NULL, 0, 0, sub, ARRAY_SIZE(sub) },
+ };
+ struct nested x;
+
+ ASSERT_EQ(parse_nested("{\"items\":[{\"id\":\"a\",\"meta\":"
+ "{\"tag\":\"x\"}},{\"id\":\"b\"}]}", &x), 0);
+ ASSERT_EQ(x.n, 2);
+ EXPECT_EQ(x.meta_seen[0], 1);
+ EXPECT_EQ(x.tag_seen[0], 1);
+ EXPECT_EQ(x.meta_seen[1], 0);
+ EXPECT_EQ(x.tag_seen[1], 0);
+
+ ASSERT_EQ(json_parse(first, strlen(first), top, ARRAY_SIZE(top)), 0);
+ EXPECT_EQ(sub[0].seen, 1);
+ ASSERT_EQ(json_parse(second, strlen(second), top, ARRAY_SIZE(top)), 0);
+ EXPECT_EQ(top[0].seen, 0);
+ EXPECT_EQ(sub[0].seen, 0);
+}
+
static void malformed_arrays_are_refused(void)
{
struct doc d;
@@ -343,6 +416,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(string_array_dispatches_each_element),
UNIT_CASE(object_array_uses_element_schema),
UNIT_CASE(nested_array_then_trailing_scalar),
+ UNIT_CASE(seen_flags_are_the_elements_own),
UNIT_CASE(malformed_arrays_are_refused),
UNIT_CASE(integer_takes_json_integers_only),
UNIT_CASE(integer_holds_any_s64_and_no_more),
diff --git a/util/json.c b/util/json.c
index cad0cda2..427aa3f7 100644
--- a/util/json.c
+++ b/util/json.c
@@ -274,6 +274,18 @@ struct pctx {
size_t n_fields;
};
+static void reset_seen(struct json_field *fields, size_t n)
+{
+ size_t i;
+
+ for (i = 0; i < n; i++) {
+ fields[i].seen = 0;
+ if (fields[i].type == JSON_T_OBJECT ||
+ fields[i].type == JSON_T_ARRAY)
+ reset_seen(fields[i].sub, fields[i].n_sub);
+ }
+}
+
static void skip_ws(struct pctx *c)
{
while (c->p < c->end) {
@@ -553,10 +565,7 @@ static int parse_array_into(struct pctx *c, struct json_field *f)
return 0;
}
for (;;) {
- size_t i;
-
- for (i = 0; i < f->n_sub; i++)
- f->sub[i].seen = 0;
+ reset_seen(f->sub, f->n_sub);
skip_ws(c);
if (c->p >= c->end)
return -EINVAL;
@@ -744,13 +753,11 @@ int json_parse(const char *data, size_t len,
.p = data, .end = data + len,
.fields = fields, .n_fields = n_fields,
};
- size_t i;
int ret;
if (len > JSON_MAX_BYTES)
return -E2BIG;
- for (i = 0; i < n_fields; i++)
- fields[i].seen = 0;
+ reset_seen(fields, n_fields);
skip_ws(&c);
if (c.p >= c.end || *c.p != '{')
diff --git a/util/json.h b/util/json.h
index af32ffd9..527b01bb 100644
--- a/util/json.h
+++ b/util/json.h
@@ -78,7 +78,8 @@ enum json_vtype {
};
/*
- * The sub fields' seen flags reset per element; on_elem may copy the
+ * The seen flags of the sub fields, and of the fields nested in them,
+ * reset per element; the dst buffers do not. on_elem may copy the
* element out of the scratch dst buffers before the next one overwrites
* them, and returns a negative errno to abort.
*/