|
| 1 | +import * as t from "tap"; |
| 2 | +import { createTestAgent } from "../../helpers/createTestAgent"; |
| 3 | +import { wrap } from "../../helpers/wrap"; |
| 4 | +import { checkContextForSqlInjection } from "../../vulnerabilities/sql-injection/checkContextForSqlInjection"; |
| 5 | +import { SQLDialectPostgres } from "../../vulnerabilities/sql-injection/dialects/SQLDialectPostgres"; |
| 6 | +import { Context, getContext, runWithContext } from "../Context"; |
| 7 | +import { markUnsafe } from "./markUnsafe"; |
| 8 | + |
| 9 | +function createContext(): Context { |
| 10 | + return { |
| 11 | + remoteAddress: "::1", |
| 12 | + method: "POST", |
| 13 | + url: "http://localhost:4000", |
| 14 | + query: {}, |
| 15 | + headers: {}, |
| 16 | + body: { |
| 17 | + image: "http://localhost:4000/api/internal", |
| 18 | + }, |
| 19 | + cookies: {}, |
| 20 | + routeParams: {}, |
| 21 | + source: "express", |
| 22 | + route: "/posts/:id", |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +t.test("it works", async () => { |
| 27 | + const agent = createTestAgent({}); |
| 28 | + agent.start([]); |
| 29 | + |
| 30 | + // No unsafe input |
| 31 | + runWithContext(createContext(), () => { |
| 32 | + const context = getContext(); |
| 33 | + |
| 34 | + if (!context) { |
| 35 | + throw new Error("Context is not defined"); |
| 36 | + } |
| 37 | + |
| 38 | + const result = checkContextForSqlInjection({ |
| 39 | + sql: 'SELECT * FROM "users" WHERE id = 1', |
| 40 | + operation: "pg.query", |
| 41 | + dialect: new SQLDialectPostgres(), |
| 42 | + context: context, |
| 43 | + }); |
| 44 | + t.same(result, undefined); |
| 45 | + }); |
| 46 | + |
| 47 | + // Unsafe string |
| 48 | + runWithContext(createContext(), () => { |
| 49 | + markUnsafe("id = 1"); |
| 50 | + |
| 51 | + const context = getContext(); |
| 52 | + |
| 53 | + if (!context) { |
| 54 | + throw new Error("Context is not defined"); |
| 55 | + } |
| 56 | + |
| 57 | + const result = checkContextForSqlInjection({ |
| 58 | + sql: 'SELECT * FROM "users" WHERE id = 1', |
| 59 | + operation: "pg.query", |
| 60 | + dialect: new SQLDialectPostgres(), |
| 61 | + context: context, |
| 62 | + }); |
| 63 | + t.same(result, { |
| 64 | + kind: "sql_injection", |
| 65 | + operation: "pg.query", |
| 66 | + source: "markUnsafe", |
| 67 | + metadata: { |
| 68 | + sql: 'SELECT * FROM "users" WHERE id = 1', |
| 69 | + }, |
| 70 | + payload: "id = 1", |
| 71 | + pathsToPayload: [".[0]"], |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + // Unsafe object |
| 76 | + runWithContext(createContext(), () => { |
| 77 | + markUnsafe({ somePropertyThatContainsSQL: "id = 1" }); |
| 78 | + |
| 79 | + const context = getContext(); |
| 80 | + |
| 81 | + if (!context) { |
| 82 | + throw new Error("Context is not defined"); |
| 83 | + } |
| 84 | + |
| 85 | + const result = checkContextForSqlInjection({ |
| 86 | + sql: 'SELECT * FROM "users" WHERE id = 1', |
| 87 | + operation: "pg.query", |
| 88 | + dialect: new SQLDialectPostgres(), |
| 89 | + context: context, |
| 90 | + }); |
| 91 | + t.same(result, { |
| 92 | + kind: "sql_injection", |
| 93 | + operation: "pg.query", |
| 94 | + source: "markUnsafe", |
| 95 | + metadata: { |
| 96 | + sql: 'SELECT * FROM "users" WHERE id = 1', |
| 97 | + }, |
| 98 | + payload: "id = 1", |
| 99 | + pathsToPayload: [".[0].somePropertyThatContainsSQL"], |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + // Test markUnsafe called without context |
| 104 | + const logs: string[] = []; |
| 105 | + wrap(console, "warn", function warn() { |
| 106 | + return function warn(message: string) { |
| 107 | + logs.push(message); |
| 108 | + }; |
| 109 | + }); |
| 110 | + markUnsafe("id = 1"); |
| 111 | + t.same(logs, [ |
| 112 | + "markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.", |
| 113 | + ]); |
| 114 | + |
| 115 | + // Warning logged only once |
| 116 | + markUnsafe("id = 1"); |
| 117 | + t.same(logs.length, 1); |
| 118 | + |
| 119 | + // Test if serialize fails |
| 120 | + runWithContext(createContext(), () => { |
| 121 | + // Define an object with a circular reference |
| 122 | + const obj: Record<string, any> = {}; |
| 123 | + obj.self = obj; |
| 124 | + markUnsafe(obj); |
| 125 | + }); |
| 126 | + t.same(logs, [ |
| 127 | + "markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.", |
| 128 | + "markUnsafe(...) failed to serialize the data", |
| 129 | + ]); |
| 130 | + |
| 131 | + runWithContext(createContext(), () => { |
| 132 | + markUnsafe(); |
| 133 | + }); |
| 134 | + t.same(logs, [ |
| 135 | + "markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.", |
| 136 | + "markUnsafe(...) failed to serialize the data", |
| 137 | + "markUnsafe(...) was called without any data.", |
| 138 | + ]); |
| 139 | + |
| 140 | + runWithContext(createContext(), () => { |
| 141 | + markUnsafe(1, true, null, undefined, () => {}, Symbol("test")); |
| 142 | + }); |
| 143 | + t.same(logs, [ |
| 144 | + "markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.", |
| 145 | + "markUnsafe(...) failed to serialize the data", |
| 146 | + "markUnsafe(...) was called without any data.", |
| 147 | + "markUnsafe(...) expects an object, array, or string. Received: number", |
| 148 | + "markUnsafe(...) expects an object, array, or string. Received: boolean", |
| 149 | + "markUnsafe(...) expects an object, array, or string. Received: null", |
| 150 | + "markUnsafe(...) expects an object, array, or string. Received: undefined", |
| 151 | + "markUnsafe(...) expects an object, array, or string. Received: function", |
| 152 | + "markUnsafe(...) expects an object, array, or string. Received: symbol", |
| 153 | + ]); |
| 154 | +}); |
0 commit comments