-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add e2e tests and improve unit tests
- Loading branch information
1 parent
8d1793a
commit 7e2f2b6
Showing
14 changed files
with
487 additions
and
9 deletions.
There are no files selected for viewing
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
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
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,127 @@ | ||
const t = require("tap"); | ||
const { spawn } = require("child_process"); | ||
const { resolve, join } = require("path"); | ||
const timeout = require("../timeout"); | ||
const { promisify } = require("util"); | ||
const { exec: execCb } = require("child_process"); | ||
|
||
const execAsync = promisify(execCb); | ||
|
||
const appDir = resolve(__dirname, "../../sample-apps/hono-prisma"); | ||
const pathToApp = join(appDir, "app.js"); | ||
|
||
t.before(async (t) => { | ||
// Generate prismajs client | ||
const { stdout, stderr } = await execAsync( | ||
"npx prisma migrate reset --force", // Generate prisma client, reset db and apply migrations | ||
{ | ||
cwd: appDir, | ||
} | ||
); | ||
|
||
if (stderr) { | ||
t.fail(stderr); | ||
} | ||
}); | ||
|
||
t.test("it blocks in blocking mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4002"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCKING: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
server.on("error", (err) => { | ||
t.fail(err.message); | ||
}); | ||
|
||
let stdout = ""; | ||
server.stdout.on("data", (data) => { | ||
stdout += data.toString(); | ||
}); | ||
|
||
let stderr = ""; | ||
server.stderr.on("data", (data) => { | ||
stderr += data.toString(); | ||
}); | ||
|
||
// Wait for the server to start | ||
timeout(2000) | ||
.then(() => { | ||
return Promise.all([ | ||
fetch('http://127.0.0.1:4002/posts/Test" OR 1=1 -- C', { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
fetch("http://127.0.0.1:4002/posts/Happy", { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
]); | ||
}) | ||
.then(([sqlInjection, normalAdd]) => { | ||
t.equal(sqlInjection.status, 500); | ||
t.equal(normalAdd.status, 200); | ||
t.match(stdout, /Starting agent/); | ||
t.match(stderr, /Zen has blocked an SQL injection/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error.message); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); | ||
|
||
t.test("it does not block in non-blocking mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4002"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
server.on("error", (err) => { | ||
t.fail(err.message); | ||
}); | ||
|
||
let stdout = ""; | ||
server.stdout.on("data", (data) => { | ||
stdout += data.toString(); | ||
}); | ||
|
||
let stderr = ""; | ||
server.stderr.on("data", (data) => { | ||
stderr += data.toString(); | ||
}); | ||
|
||
// Wait for the server to start | ||
timeout(2000) | ||
.then(() => { | ||
return Promise.all([ | ||
fetch('http://127.0.0.1:4002/posts/Test" OR 1=1 -- C', { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
fetch("http://127.0.0.1:4002/posts/Happy", { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
]); | ||
}) | ||
.then(([sqlInjection, normalAdd]) => { | ||
t.equal(sqlInjection.status, 200); | ||
t.equal(normalAdd.status, 200); | ||
t.match(stdout, /Starting agent/); | ||
t.notMatch(stderr, /Zen has blocked an SQL injection/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error.message); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); |
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
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
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
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,3 @@ | ||
node_modules | ||
*.db | ||
*.db-journal |
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,3 @@ | ||
# hono-prisma | ||
|
||
WARNING: This application contains security issues and should not be used in production (or taken as an example of how to write secure code). |
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,49 @@ | ||
const Zen = require("@aikidosec/firewall"); | ||
|
||
const { PrismaClient } = require("@prisma/client"); | ||
const { serve } = require("@hono/node-server"); | ||
const { Hono } = require("hono"); | ||
|
||
function getPort() { | ||
const port = parseInt(process.argv[2], 10) || 4000; | ||
|
||
if (isNaN(port)) { | ||
console.error("Invalid port"); | ||
process.exit(1); | ||
} | ||
|
||
return port; | ||
} | ||
|
||
async function main() { | ||
const port = getPort(); | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
const app = new Hono(); | ||
|
||
app.get("/", async (c) => { | ||
return c.text("Hello, world!"); | ||
}); | ||
|
||
app.get("/posts/:title", async (c) => { | ||
// Insecure, do not use in production | ||
const posts = await prisma.$queryRawUnsafe( | ||
'SELECT * FROM Post WHERE `title` = "' + c.req.param().title + '"' | ||
); | ||
return c.json(posts); | ||
}); | ||
|
||
serve({ | ||
fetch: app.fetch, | ||
port: port, | ||
}).on("listening", () => { | ||
console.log(`Server is running on port ${port}`); | ||
}); | ||
} | ||
|
||
main().catch(async (e) => { | ||
console.error(e); | ||
await prisma.$disconnect(); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.