Skip to content

Commit b5f0074

Browse files
authored
Merge pull request #641 from AikidoSec/data-schema-non-string-keys
Simplify getDataSchema & add __proto__ test
2 parents 5a13a32 + 597093f commit b5f0074

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

library/agent/api-discovery/getDataSchema.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,25 @@ t.test("test max properties", async (t) => {
185185
const schema2 = getDataSchema(obj2);
186186
t.same(Object.keys(schema2.properties!).length, 100);
187187
});
188+
189+
t.test("it ignores __proto__ property", async (t) => {
190+
const data = {
191+
__proto__: { malicious: "data" },
192+
test: "value",
193+
0: "zero",
194+
[Symbol("sym")]: "symbolValue",
195+
};
196+
197+
const schema = getDataSchema(data);
198+
t.same(schema, {
199+
type: "object",
200+
properties: {
201+
test: {
202+
type: "string",
203+
},
204+
0: {
205+
type: "string",
206+
},
207+
},
208+
});
209+
});

library/agent/api-discovery/getDataSchema.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,15 @@ export function getDataSchema(data: unknown, depth = 0): DataSchema {
6565
// If the depth is less than the maximum depth, get the schema for each property
6666
if (depth < maxDepth) {
6767
let propertiesCount = 0;
68-
for (const key in data) {
68+
for (const key of Object.keys(data)) {
6969
if (propertiesCount >= maxProperties) {
7070
break;
7171
}
7272
propertiesCount++;
73-
if (Object.prototype.hasOwnProperty.call(data, key)) {
74-
schema.properties![key] = getDataSchema(
75-
(data as { [index: string]: unknown })[key],
76-
depth + 1
77-
);
78-
}
73+
schema.properties![key] = getDataSchema(
74+
(data as { [index: string]: unknown })[key],
75+
depth + 1
76+
);
7977
}
8078
}
8179

0 commit comments

Comments
 (0)