Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions library/sinks/Postgresjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,66 @@ t.test("it inspects query method calls and blocks if needed", async (t) => {

await sql.unsafe("");
});

const numberOfQueries = 50;

const results = await Promise.allSettled(
Array.from({ length: numberOfQueries }, (_, index) => {
const contextKey = `myTitle${index}`;
const injection = `abc' OR 1=1; -- should be blocked ${index}`;

return runWithContext(
{
remoteAddress: "::1",
method: "POST",
url: "http://localhost:4000",
query: {},
headers: {},
body: {
[contextKey]: injection,
},
cookies: {},
routeParams: {},
source: "hono",
route: "/posts/:id",
},
async () => {
try {
await sql.unsafe(
`SELECT id FROM cats_2 WHERE petname = '${injection}'`
);
return {
index,
error: null,
};
} catch (error: any) {
return {
index,
error,
};
}
}
);
})
);

t.equal(results.length, numberOfQueries);

for (const result of results) {
t.equal(result.status, "fulfilled");

if (result.status === "fulfilled") {
const { index, error } = result.value;
t.ok(error instanceof Error);

if (error instanceof Error) {
t.same(
error.message,
`Zen has blocked an SQL injection: sql.unsafe(...) originating from body.myTitle${index}`
);
}
}
}
} catch (error: any) {
t.fail(error);
} finally {
Expand Down
Loading