|
| 1 | +// Import Node.js Dependencies |
| 2 | +import { test, describe } from "node:test"; |
| 3 | +import assert from "node:assert"; |
| 4 | +import { readFileSync } from "node:fs"; |
| 5 | +import fs from "node:fs/promises"; |
| 6 | + |
| 7 | +// Import Internal Dependencies |
| 8 | +import { AstAnalyser } from "../../src/index.js"; |
| 9 | + |
| 10 | +const FIXTURE_URL = new URL("fixtures/dataExfiltration/", import.meta.url); |
| 11 | + |
| 12 | +describe("data exfiltration", () => { |
| 13 | + test("it should report a warning in case of `JSON.stringify(sensitiveData) for member expression`", async() => { |
| 14 | + const fixturesDir = new URL("memberExpression/", FIXTURE_URL); |
| 15 | + const fixtureFiles = await fs.readdir(fixturesDir); |
| 16 | + |
| 17 | + for (const fixtureFile of fixtureFiles) { |
| 18 | + const fixture = readFileSync(new URL(fixtureFile, fixturesDir), "utf-8"); |
| 19 | + const { warnings: outputWarnings } = new AstAnalyser( |
| 20 | + { |
| 21 | + optionalWarnings: true |
| 22 | + } |
| 23 | + ).analyse(fixture); |
| 24 | + |
| 25 | + const [firstWarning] = outputWarnings; |
| 26 | + assert.strictEqual(outputWarnings.length, 1); |
| 27 | + assert.deepEqual(firstWarning.kind, "data-exfiltration"); |
| 28 | + assert.strictEqual(firstWarning.value, `${fixtureFile.split(".").slice(0, 2).join(".")}`); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + test("it should report a warning in case of `JSON.stringify(sensitiveData) for direct call expression`", async() => { |
| 33 | + const fixturesDir = new URL("directCallExpression/", FIXTURE_URL); |
| 34 | + const fixtureFiles = await fs.readdir(fixturesDir); |
| 35 | + |
| 36 | + for (const fixtureFile of fixtureFiles) { |
| 37 | + const fixture = readFileSync(new URL(fixtureFile, fixturesDir), "utf-8"); |
| 38 | + const { warnings: outputWarnings } = new AstAnalyser( |
| 39 | + { |
| 40 | + optionalWarnings: true |
| 41 | + } |
| 42 | + ).analyse(fixture); |
| 43 | + |
| 44 | + const [firstWarning] = outputWarnings; |
| 45 | + assert.strictEqual(outputWarnings.length, 1); |
| 46 | + assert.deepEqual(firstWarning.kind, "data-exfiltration"); |
| 47 | + assert.strictEqual(firstWarning.value, `${fixtureFile.split(".").slice(0, 2).join(".")}`); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("should only generate one warning when multiple detection of data exfiltration occurs", () => { |
| 52 | + const code = ` |
| 53 | + import os from "os"; |
| 54 | +
|
| 55 | + JSON.stringify(os.userInfo()); |
| 56 | + JSON.stringify(os.userInfo()); |
| 57 | + JSON.stringify(os.networkInterfaces()); |
| 58 | + `; |
| 59 | + |
| 60 | + const { warnings: outputWarnings } = new AstAnalyser( |
| 61 | + { |
| 62 | + optionalWarnings: true |
| 63 | + } |
| 64 | + ).analyse(code); |
| 65 | + |
| 66 | + const [firstWarning] = outputWarnings; |
| 67 | + assert.strictEqual(outputWarnings.length, 1); |
| 68 | + assert.deepEqual(firstWarning.kind, "data-exfiltration"); |
| 69 | + assert.strictEqual(firstWarning.value, "os.userInfo, os.networkInterfaces"); |
| 70 | + assert.strictEqual(firstWarning.location?.length, 3); |
| 71 | + }); |
| 72 | + |
| 73 | + test("should not generate a warning when serializing return value of every function call", () => { |
| 74 | + const code = ` |
| 75 | + function foo (){ |
| 76 | + return "foo"; |
| 77 | + } |
| 78 | + JSON.stringify(foo()); |
| 79 | + `; |
| 80 | + const { warnings: outputWarnings } = new AstAnalyser( |
| 81 | + { |
| 82 | + optionalWarnings: true |
| 83 | + } |
| 84 | + ).analyse(code); |
| 85 | + |
| 86 | + assert.strictEqual(outputWarnings.length, 0); |
| 87 | + }); |
| 88 | + |
| 89 | + test("should not generate a warning when os is not imported", () => { |
| 90 | + const code = ` |
| 91 | + const os = { |
| 92 | + userInfo(){ |
| 93 | + return {}; |
| 94 | + }, |
| 95 | + cpus(){ |
| 96 | + return []; |
| 97 | + }, |
| 98 | + networkInterfaces(){ |
| 99 | + return []; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + JSON.stringify(os.userInfo()); |
| 104 | + JSON.stringify(os.networkInterfaces()); |
| 105 | + JSON.stringify(os.cpus()); |
| 106 | + `; |
| 107 | + const { warnings: outputWarnings } = new AstAnalyser( |
| 108 | + { |
| 109 | + optionalWarnings: true |
| 110 | + } |
| 111 | + ).analyse(code); |
| 112 | + |
| 113 | + assert.strictEqual(outputWarnings.length, 0); |
| 114 | + }); |
| 115 | + |
| 116 | + test("should not generate a warning when dns is not imported", () => { |
| 117 | + const code = ` |
| 118 | + const dns = { |
| 119 | + getServers(){ |
| 120 | + return []; |
| 121 | + } |
| 122 | + } |
| 123 | + JSON.stringify(getServers()); |
| 124 | + `; |
| 125 | + const { warnings: outputWarnings } = new AstAnalyser( |
| 126 | + { |
| 127 | + optionalWarnings: true |
| 128 | + } |
| 129 | + ).analyse(code); |
| 130 | + |
| 131 | + assert.strictEqual(outputWarnings.length, 0); |
| 132 | + }); |
| 133 | +}); |
0 commit comments