Skip to content

eBPF-based security platform

json: refuse a declared key given a value twice

Daniel Borkmann325d048d1be7
diff --git a/testing/unit/json.c b/testing/unit/json.c
index d8c7941b..a23f20a1 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -291,6 +291,37 @@ static void seen_flags_are_the_elements_own(void)
EXPECT_EQ(sub[0].seen, 0);
}
+static void declared_key_takes_one_value(void)
+{
+ static const char nested[] = "{\"o\":{\"k\":\"v\"},\"o\":{}}";
+ 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 doc d;
+
+ EXPECT_EQ(parse("{\"type\":\"a\",\"type\":\"b\"}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"type\":\"a\",\"type\":null}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"schema\":1,\"schema\":1}", &d), -EINVAL);
+ EXPECT_EQ(parse("{\"names\":[\"a\"],\"names\":[\"b\"]}", &d),
+ -EINVAL);
+ EXPECT_EQ(parse("{\"events\":[{\"type\":\"a\",\"type\":\"b\"}]}", &d),
+ -EINVAL);
+ EXPECT_EQ(json_parse(nested, strlen(nested), top, ARRAY_SIZE(top)),
+ -EINVAL);
+
+ ASSERT_EQ(parse("{\"type\":null,\"type\":\"b\"}", &d), 0);
+ EXPECT_STREQ(d.type, "b");
+ ASSERT_EQ(parse("{\"x\":1,\"x\":{},\"type\":\"t\"}", &d), 0);
+ EXPECT_STREQ(d.type, "t");
+ ASSERT_EQ(parse("{\"events\":[{\"type\":\"a\"},{\"type\":\"b\"}]}",
+ &d), 0);
+ EXPECT_EQ(d.events, 2);
+}
+
static void malformed_arrays_are_refused(void)
{
struct doc d;
@@ -417,6 +448,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(object_array_uses_element_schema),
UNIT_CASE(nested_array_then_trailing_scalar),
UNIT_CASE(seen_flags_are_the_elements_own),
+ UNIT_CASE(declared_key_takes_one_value),
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 427aa3f7..329f1225 100644
--- a/util/json.c
+++ b/util/json.c
@@ -495,6 +495,9 @@ static int parse_object(struct pctx *c)
break;
}
}
+ /* Readers that take the first or the last would differ. */
+ if (f && f->seen)
+ return -EINVAL;
ret = parse_value(c, f);
if (ret)
return ret;
diff --git a/util/json.h b/util/json.h
index 527b01bb..ae995aa7 100644
--- a/util/json.h
+++ b/util/json.h
@@ -99,7 +99,8 @@ struct json_field {
};
/*
- * -EINVAL on malformed input or a type mismatch, -E2BIG on a bounds
+ * -EINVAL on malformed input, a type mismatch or a declared key given a
+ * value twice in one object (null gives none), -E2BIG on a bounds
* violation, -ENOSPC when a string exceeds its dst_cap.
*/
int json_parse(const char *data, size_t len,