Skip to content

Commit 02a2d05

Browse files
authored
refactor(probe/isSerializeEnv): simplify tracing & add new UT with a spread (#392)
1 parent facb858 commit 02a2d05

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

.changeset/eager-coats-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/js-x-ray": minor
3+
---
4+
5+
Simplify tracing validation & add new spread test for the probe

workspaces/js-x-ray/src/probes/isSerializeEnv.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ function validateNode(
4747
}
4848

4949
if (firstArg.type === "Identifier") {
50-
const data = tracer.getDataFromIdentifier("process.env");
51-
if (data !== null && data.assignmentMemory.some(({ name }) => name === firstArg.name)) {
50+
const data = tracer.getDataFromIdentifier(firstArg.name);
51+
if (data !== null) {
5252
return [true];
5353
}
5454
}
@@ -71,12 +71,16 @@ function main(
7171
return ProbeSignals.Skip;
7272
}
7373

74-
function initialize(sourceFile: SourceFile) {
75-
sourceFile.tracer.trace("process.env", {
76-
followConsecutiveAssignment: true
77-
}).trace("JSON.stringify", {
78-
followConsecutiveAssignment: true
79-
});
74+
function initialize(
75+
{ tracer }: SourceFile
76+
) {
77+
tracer
78+
.trace("process.env", {
79+
followConsecutiveAssignment: true
80+
})
81+
.trace("JSON.stringify", {
82+
followConsecutiveAssignment: true
83+
});
8084
}
8185

8286
export default {

workspaces/js-x-ray/test/probes/isSerializeEnv.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ test("should be able to detect reassigned JSON.stringify", () => {
8080
assert.strictEqual(warning.value, "JSON.stringify(process.env)");
8181
});
8282

83+
test("should be able to detect serialization of process.env using a SpreadElement", () => {
84+
const str = `
85+
const env = {...process.env};
86+
JSON.stringify(env);
87+
`;
88+
const ast = parseScript(str);
89+
const sastAnalysis = getSastAnalysis(isSerializeEnv).execute(ast.body);
90+
91+
assert.strictEqual(sastAnalysis.warnings().length, 1);
92+
const warning = sastAnalysis.getWarning("serialize-environment");
93+
assert.strictEqual(warning.kind, "serialize-environment");
94+
assert.strictEqual(warning.value, "JSON.stringify(process.env)");
95+
});
96+
8397
test("should not detect other JSON.stringify calls", () => {
8498
const str = "JSON.stringify({ foo: 'bar' })";
8599
const ast = parseScript(str);

0 commit comments

Comments
 (0)