Skip to content

eBPF-based security platform

json: refuse an element that is not an object in an array of objects

Daniel Borkmann42d019a24d34
diff --git a/testing/unit/json.c b/testing/unit/json.c
index a23f20a1..2e2b8495 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -328,6 +328,24 @@ static void malformed_arrays_are_refused(void)
EXPECT_EQ(parse("{\"names\":[123]}", &d), -EINVAL);
EXPECT_EQ(parse("{\"names\":[\"a\",\"b\"", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"names\":[{\"\":\"a\"}]}", &d), -EINVAL);
+}
+
+/*
+ * An element that is not an object would land in the first field of
+ * the element schema, a type in place of a whole record.
+ */
+static void object_array_takes_objects_only(void)
+{
+ struct doc d;
+
+ EXPECT_EQ(parse("{\"events\":[\"init\"]}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"events\":[1]}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"events\":[null]}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"events\":[{\"type\":\"a\"},true]}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"events\":[[]]}", &d), -EINVAL);
+ ASSERT_EQ(parse("{\"events\":[{},{\"type\":\"a\"}]}", &d), 0);
+ EXPECT_EQ(d.events, 2);
}
static void integer_takes_json_integers_only(void)
@@ -450,6 +468,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(seen_flags_are_the_elements_own),
UNIT_CASE(declared_key_takes_one_value),
UNIT_CASE(malformed_arrays_are_refused),
+ UNIT_CASE(object_array_takes_objects_only),
UNIT_CASE(integer_takes_json_integers_only),
UNIT_CASE(integer_holds_any_s64_and_no_more),
UNIT_CASE(discarded_number_follows_the_grammar),
diff --git a/util/json.c b/util/json.c
index 329f1225..e07d9408 100644
--- a/util/json.c
+++ b/util/json.c
@@ -572,7 +572,9 @@ static int parse_array_into(struct pctx *c, struct json_field *f)
skip_ws(c);
if (c->p >= c->end)
return -EINVAL;
- if (*c->p == '{') {
+ if (f->n_sub == 1 && (!f->sub[0].key || !f->sub[0].key[0])) {
+ ret = parse_value(c, &f->sub[0]);
+ } else if (*c->p == '{') {
struct json_field *save_f = c->fields;
size_t save_n = c->n_fields;
@@ -581,8 +583,10 @@ static int parse_array_into(struct pctx *c, struct json_field *f)
ret = parse_object(c);
c->fields = save_f;
c->n_fields = save_n;
+ } else if (!f->n_sub) {
+ ret = parse_value(c, NULL);
} else {
- ret = parse_value(c, f->n_sub ? &f->sub[0] : NULL);
+ return -EINVAL; /* not an object, as sub[] asks */
}
if (ret)
return ret;
diff --git a/util/json.h b/util/json.h
index ae995aa7..37c88c42 100644
--- a/util/json.h
+++ b/util/json.h
@@ -78,8 +78,10 @@ enum json_vtype {
};
/*
- * 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
+ * An array of scalars declares one sub field with an empty key, which
+ * takes each element; any other sub[] takes objects alone, and none
+ * discards the elements. 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.
*/