-
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
6b81acd
commit 3d0108a
Showing
6 changed files
with
214 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const t = require("tap"); | ||
const { spawn } = require("child_process"); | ||
const { resolve } = require("path"); | ||
const timeout = require("../timeout"); | ||
|
||
const pathToApp = resolve(__dirname, "../../sample-apps/hono-xml", "app.js"); | ||
|
||
t.test("it blocks in blocking mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4000"], { | ||
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://localhost:4000/add", { | ||
method: "POST", | ||
body: "<cat><name>Njuska'); DELETE FROM cats;-- H</name></cat>", | ||
headers: { | ||
"Content-Type": "application/xml", | ||
}, | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
fetch("http://localhost:4000/add", { | ||
method: "POST", | ||
body: "<cat><name>Miau</name></cat>", | ||
headers: { | ||
"Content-Type": "application/xml", | ||
}, | ||
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, /Aikido firewall has blocked an SQL injection/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error.message); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); | ||
|
||
t.test("it does not block in dry mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4001"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
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(() => | ||
Promise.all([ | ||
fetch("http://localhost:4001/add", { | ||
method: "POST", | ||
body: "<cat><name>Njuska'); DELETE FROM cats;-- H</name></cat>", | ||
headers: { | ||
"Content-Type": "application/xml", | ||
}, | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
fetch("http://localhost:4001/add", { | ||
method: "POST", | ||
body: "<cat><name>Miau</name></cat>", | ||
headers: { | ||
"Content-Type": "application/xml", | ||
}, | ||
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, /Aikido firewall has blocked an SQL injection/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error.message); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
class Cats { | ||
constructor(db) { | ||
this.db = db; | ||
} | ||
|
||
async add(name) { | ||
// This is unsafe! This is for demo purposes only, you should use parameterized queries. | ||
await this.db.query(`INSERT INTO cats(petname) VALUES ('${name}');`); | ||
} | ||
|
||
async byName(name) { | ||
// This is unsafe! This is for demo purposes only, you should use parameterized queries. | ||
const [cats] = await this.db.query( | ||
`SELECT petname FROM cats WHERE petname = '${name}'` | ||
); | ||
|
||
return cats.map((row) => row.petname); | ||
} | ||
|
||
async getAll() { | ||
const [cats] = await this.db.execute("SELECT petname FROM `cats`;"); | ||
|
||
return cats.map((row) => row.petname); | ||
} | ||
} | ||
|
||
module.exports = Cats; |
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,25 @@ | ||
const mysql = require("mysql2/promise"); | ||
|
||
async function createConnection() { | ||
// Normally you'd use environment variables for this | ||
const connection = await mysql.createConnection({ | ||
host: "localhost", | ||
user: "root", | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical
The hard-coded value "root" is used as
user name Error loading related location Loading |
||
password: "mypassword", | ||
database: "catsdb", | ||
port: 27015, | ||
multipleStatements: true, | ||
}); | ||
|
||
await connection.execute(` | ||
CREATE TABLE IF NOT EXISTS cats ( | ||
petname varchar(255) | ||
); | ||
`); | ||
|
||
return connection; | ||
} | ||
|
||
module.exports = { | ||
createConnection, | ||
}; |
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