Skip to content

Add support for libsql #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ jobs:
MONGODB_REPLICA_SET_KEY: replicasetkey123
ports:
- "27020:27017"
libsql:
image: ghcr.io/tursodatabase/libsql-server:v0.24.30
ports:
- "27021:8080"
strategy:
fail-fast: false
matrix:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Zen for Node.js 16+ is compatible with:
* ✅ [`postgres`](https://www.npmjs.com/package/postgres) 3.x
* ✅ [`@clickhouse/client`](https://www.npmjs.com/package/@clickhouse/client) 1.x
* ✅ [`@prisma/client`](https://www.npmjs.com/package/@prisma/client) 5.x
* ✅ [`@libsql/client`](https://www.npmjs.com/package/@libsql/client) ^0.10.x
* ✅ [`libsql`](https://www.npmjs.com/package/libsql) ^0.4.x

### Cloud providers

Expand Down
109 changes: 109 additions & 0 deletions end2end/tests/hapi-libsql.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const t = require("tap");
const { spawn } = require("child_process");
const { resolve } = require("path");
const timeout = require("../timeout");

const pathToApp = resolve(__dirname, "../../sample-apps/hapi-libsql", "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://127.0.0.1:4000/?petname=${encodeURIComponent("Njuska'); DELETE FROM cats;-- H")}`,
{
signal: AbortSignal.timeout(5000),
}
),
fetch("http://127.0.0.1:4000/?petname=Njuska", {
signal: AbortSignal.timeout(5000),
}),
]);
})
.then(async ([noSQLInjection, normalSearch]) => {
t.equal(noSQLInjection.status, 500);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.match(await noSQLInjection.text(), /Zen 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://127.0.0.1:4001/?petname=${encodeURIComponent("Njuska'); DELETE FROM cats;-- H")}`,
{
signal: AbortSignal.timeout(5000),
}
),
fetch("http://127.0.0.1:4001/?petname=Njuska", {
signal: AbortSignal.timeout(5000),
}),
])
)
.then(async ([noSQLInjection, normalSearch]) => {
t.equal(noSQLInjection.status, 200);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.notMatch(
await noSQLInjection.text(),
/Zen has blocked an SQL injection/
);
})
.catch((error) => {
t.fail(error.message);
})
.finally(() => {
server.kill();
});
});
4 changes: 4 additions & 0 deletions library/agent/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import { Koa } from "../sources/Koa";
import { ClickHouse } from "../sinks/ClickHouse";
import { Prisma } from "../sinks/Prisma";
import { Function } from "../sinks/Function";
import { LibSQL } from "../sinks/LibSQL";
import { LibSQLClient } from "../sinks/LibSQLClient";

function getLogger(): Logger {
if (isDebugging()) {
Expand Down Expand Up @@ -140,6 +142,8 @@ export function getWrappers() {
new ClickHouse(),
new Prisma(),
new Function(),
new LibSQL(),
new LibSQLClient(),
];
}

Expand Down
Loading
Loading