Skip to content
Draft
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
22 changes: 19 additions & 3 deletions packages/documents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,28 @@ export class LocalJsonStore<T> {

private toPath(key: string) {
const relative = key
.split("/")
.filter(Boolean)
.split(/[\\/]/)
.filter((segment) => {
const s = segment.trim();
return s && s !== "." && s !== "..";
})
.map(sanitizeSegment)
.join(path.sep);

return path.join(this.baseDirectory, `${relative}.json`);
const absoluteBase = path.resolve(this.baseDirectory);
const absoluteResolved = path.resolve(absoluteBase, `${relative}.json`);

// Ensure the resolved path stays within the base directory.
// Use path.sep to prevent prefix bypass (e.g. /data vs /data-secrets)
const baseWithTrailingSep = absoluteBase.endsWith(path.sep)
? absoluteBase
: `${absoluteBase}${path.sep}`;

if (!absoluteResolved.startsWith(baseWithTrailingSep) && absoluteResolved !== absoluteBase) {
throw new Error(`Path traversal detected: "${key}" resolves outside the base directory.`);
}

return absoluteResolved;
}

private readPath(filePath: string) {
Expand Down
63 changes: 63 additions & 0 deletions packages/documents/src/security.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { LocalJsonStore } from "./index.js";
import path from "node:path";
import fs from "node:fs";
import os from "node:os";

describe("LocalJsonStore Security", () => {
let baseDir: string;
let store: LocalJsonStore<any>;

beforeEach(() => {
baseDir = fs.mkdtempSync(path.join(os.tmpdir(), "jeanbot-test-store-"));
store = new LocalJsonStore(baseDir);
});

afterEach(() => {
fs.rmSync(baseDir, { recursive: true, force: true });
});

it("should prevent path traversal when reading", () => {
const secretFile = path.resolve(baseDir, "..", "jeanbot-secret.json");
fs.writeFileSync(secretFile, JSON.stringify({ secret: "stolen" }));

try {
const data = store.read("../jeanbot-secret");
expect(data).toBeUndefined();
} finally {
if (fs.existsSync(secretFile)) {
fs.unlinkSync(secretFile);
}
}
});

it("should prevent path traversal when writing", () => {
const maliciousKey = "../malicious";
const data = { evil: true };

store.write(maliciousKey, data);

const maliciousPath = path.resolve(baseDir, "..", "malicious.json");
const exists = fs.existsSync(maliciousPath);

if (exists) {
fs.unlinkSync(maliciousPath);
}

expect(exists).toBe(false);
});

it("should prevent path traversal when deleting", () => {
const victimFile = path.resolve(baseDir, "..", "victim.json");
fs.writeFileSync(victimFile, JSON.stringify({ data: "stay" }));

try {
store.delete("../victim");
expect(fs.existsSync(victimFile)).toBe(true);
} finally {
if (fs.existsSync(victimFile)) {
fs.unlinkSync(victimFile);
}
}
});
});
Loading