-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f3ff97
commit aeec6fd
Showing
3 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
library/vulnerabilities/js-injection/checkContextForJsInjection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as t from "tap"; | ||
import { checkContextForJsInjection } from "./checkContextForJsInjection"; | ||
|
||
t.test("it returns correct path", async () => { | ||
t.same( | ||
checkContextForJsInjection({ | ||
js: "const x = 1 + 1; fetch();", | ||
operation: "eval", | ||
context: { | ||
cookies: {}, | ||
headers: {}, | ||
remoteAddress: "ip", | ||
method: "POST", | ||
url: "url", | ||
query: {}, | ||
body: { | ||
calc: "1 + 1; fetch()", | ||
}, | ||
source: "express", | ||
route: "/", | ||
routeParams: {}, | ||
}, | ||
}), | ||
{ | ||
operation: "eval", | ||
kind: "js_injection", | ||
source: "body", | ||
pathToPayload: ".calc", | ||
metadata: { | ||
js: "const x = 1 + 1; fetch();", | ||
}, | ||
payload: "1 + 1; fetch()", | ||
} | ||
); | ||
}); |
92 changes: 92 additions & 0 deletions
92
library/vulnerabilities/js-injection/detectJsInjection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as t from "tap"; | ||
import { detectJsInjection } from "./detectJsInjection"; | ||
|
||
t.test("it detects JS injections", async (t) => { | ||
t.same( | ||
detectJsInjection( | ||
'1 + 1; console.log("hello")', | ||
'1 + 1; console.log("hello")' | ||
), | ||
true | ||
); | ||
t.same(detectJsInjection("const x = 1 + 1; fetch();", "+ 1; fetch()"), true); | ||
t.same( | ||
detectJsInjection("const test = 'Hello World!'; //';", "Hello World!'; //"), | ||
true | ||
); | ||
t.same( | ||
detectJsInjection( | ||
"if (username === 'admin' || 1 === 1) { return true; } //') {}", | ||
"admin' || 1 === 1) { return true; } //" | ||
), | ||
true | ||
); | ||
t.same( | ||
detectJsInjection( | ||
"packet.readDateTimeString('abc'); process.exit(1); // ');", | ||
"abc'); process.exit(1); //" | ||
), | ||
true | ||
); | ||
t.same( | ||
detectJsInjection( | ||
"const window={}; alert('!'); return window.__NUXT__", | ||
"alert('!');" | ||
), | ||
true | ||
); | ||
t.same( | ||
detectJsInjection( | ||
"const obj = { test: 'value', isAdmin: true }; //'};", | ||
"value', isAdmin: true }; //" | ||
), | ||
true | ||
); | ||
}); | ||
|
||
t.test("does not detect JS injections", async (t) => { | ||
t.same(detectJsInjection("1 + 1", "1 + 1"), false); | ||
t.same(detectJsInjection("1 + 1", "const x = 1 + 1; x"), false); | ||
t.same(detectJsInjection("1 + 1", "1 + 1; console.log('hello')"), false); | ||
t.same(detectJsInjection("1 + 1", "1"), false); | ||
t.same(detectJsInjection("1 + 1", "abc"), false); | ||
t.same(detectJsInjection("const x = 'test'", "test"), false); | ||
t.same(detectJsInjection("const x = 'test'", ""), false); | ||
t.same(detectJsInjection("const test = 'abcde_123';", "abcde_123"), false); | ||
t.same(detectJsInjection("const test = [1, 2, 3];", "1, 2, 3"), false); | ||
|
||
t.same( | ||
detectJsInjection("const test = 'Hello World!';", "Hello World!"), | ||
false | ||
); | ||
t.same( | ||
detectJsInjection( | ||
"if(username === 'admin' || 1 === 1) { return true; }", | ||
"admin" | ||
), | ||
false | ||
); | ||
t.same( | ||
detectJsInjection("const obj = { test: 'value', isAdmin: true };", "value"), | ||
false | ||
); | ||
}); | ||
|
||
t.test("test source type", async (t) => { | ||
t.same( | ||
detectJsInjection( | ||
"const test: string = 'Hello World!'; console.log('test'); //';", | ||
"Hello World!'; console.log('test'); //", | ||
0 | ||
), | ||
false // Cannot be parsed as JS, it's TS | ||
); | ||
t.same( | ||
detectJsInjection( | ||
"const test: string = 'Hello World!'; console.log('test'); //';", | ||
"Hello World!'; console.log('test'); //", | ||
1 | ||
), | ||
true | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters