Files
outline/server/validation.test.ts
T
Tom MoorandClaude Opus 5 fc7f33577f fix: Sanitize user-provided name when building export storage keys (#13336)
* fix: Sanitize user-provided name when building export storage keys

Export keys interpolated a document title or collection name straight
into the storage path, so a name containing path separators could move
the archive out of the uploads bucket and into public, where files.get
serves content without authorization.

Sanitize the name down to a single path segment, dedupe the duplicated
key builder in collectionExporter, and reject traversal in LocalStorage
where resolve-path only guards the root, not movement between buckets.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore: Complete JSDoc on export key helpers

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 21:58:58 +00:00

90 lines
2.7 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { Buckets } from "./models/helpers/AttachmentHelper";
import { ValidateKey } from "./validation";
describe("#ValidateKey.isValid", () => {
it("should return false if number of key components is incorrect", () => {
expect(
ValidateKey.isValid(
`${Buckets.uploads}/${randomUUID()}/${randomUUID()}/foo/bar`
)
).toBe(false);
});
it("should return false if the first key component is not a valid bucket", () => {
expect(
ValidateKey.isValid(`foo/${randomUUID()}/${randomUUID()}/bar.png`)
).toBe(false);
});
it("should return false if second and third key components are not UUID", () => {
expect(
ValidateKey.isValid(`${Buckets.uploads}/foo/${randomUUID()}/bar.png`)
).toBe(false);
expect(
ValidateKey.isValid(`${Buckets.uploads}/${randomUUID()}/foo/bar.png`)
).toBe(false);
});
it("should return true successfully validating key", () => {
expect(
ValidateKey.isValid(
`${Buckets.public}/${randomUUID()}/${randomUUID()}/foo.png`
)
).toBe(true);
expect(
ValidateKey.isValid(
`${Buckets.uploads}/${randomUUID()}/${randomUUID()}/foo.png`
)
).toBe(true);
expect(
ValidateKey.isValid(`${Buckets.avatars}/${randomUUID()}/${randomUUID()}`)
).toBe(true);
});
});
describe("#ValidateKey.sanitize", () => {
it("should sanitize malicious looking keys", () => {
const uuid1 = randomUUID();
const uuid2 = randomUUID();
expect(
ValidateKey.sanitize(`public/${uuid1}/${uuid2}/~.\u0000malicious_key`)
).toEqual(`public/${uuid1}/${uuid2}/~.malicious_key`);
});
it("should remove potential path traversal", () => {
const uuid1 = randomUUID();
const uuid2 = randomUUID();
expect(
ValidateKey.sanitize(`public/${uuid1}/${uuid2}/../../malicious_key`)
).toEqual(`public/${uuid1}/${uuid2}/malicious_key`);
});
it("should remove problematic characters", () => {
const uuid1 = randomUUID();
const uuid2 = randomUUID();
expect(ValidateKey.sanitize(`public/${uuid1}/${uuid2}/test#:*?`)).toEqual(
`public/${uuid1}/${uuid2}/test`
);
});
});
describe("#ValidateKey.sanitizeSegment", () => {
it("should leave an ordinary name untouched", () => {
expect(ValidateKey.sanitizeSegment("My Document")).toEqual("My Document");
});
it("should remove path separators", () => {
expect(ValidateKey.sanitizeSegment("../../../public/foo")).not.toContain(
"/"
);
expect(ValidateKey.sanitizeSegment("..\\..\\public\\foo")).not.toContain(
"\\"
);
});
it("should remove problematic characters", () => {
expect(ValidateKey.sanitizeSegment("test#:*?")).toEqual("test");
});
});