-
Notifications
You must be signed in to change notification settings - Fork 0
fix(docs): prevent path traversal in /docs static handler #80
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { resolve } from "node:path"; | ||
| import { resolvePathWithinBase } from "./safePath"; | ||
|
|
||
| describe("resolvePathWithinBase", () => { | ||
| const baseDir = resolve("/tmp/docs-base"); | ||
|
|
||
| test("resolves nested relative paths inside the base directory", () => { | ||
| expect(resolvePathWithinBase(baseDir, "/assets/app.js")).toBe( | ||
| resolve(baseDir, "assets/app.js"), | ||
| ); | ||
| }); | ||
|
|
||
| test("keeps base directory requests inside the base directory", () => { | ||
| expect(resolvePathWithinBase(baseDir, "/")).toBe(baseDir); | ||
| }); | ||
|
|
||
| test("rejects traversal outside the base directory", () => { | ||
| expect(resolvePathWithinBase(baseDir, "/../../etc/passwd")).toBeNull(); | ||
| }); | ||
|
|
||
| test("contains repeated leading slashes within the base directory", () => { | ||
| expect(resolvePathWithinBase(baseDir, "//etc/passwd")).toBe( | ||
| resolve(baseDir, "etc/passwd"), | ||
| ); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { resolve } from "node:path"; | ||
|
|
||
| export function resolvePathWithinBase( | ||
| baseDir: string, | ||
| requestPath: string, | ||
| ): string | null { | ||
| const normalizedRequestPath = requestPath.replaceAll("\\", "/"); | ||
| const pathSegments = normalizedRequestPath | ||
| .replace(/^\/+/, "") | ||
| .split("/") | ||
| .filter(Boolean); | ||
|
|
||
| if (pathSegments.includes("..")) { | ||
| return null; | ||
| } | ||
|
|
||
| return resolve(baseDir, ...pathSegments); | ||
| } | ||
|
Comment on lines
+1
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation is a bit too restrictive as it rejects any path containing import { isAbsolute, relative, resolve } from "node:path";
export function resolvePathWithinBase(
baseDir: string,
requestPath: string,
): string | null {
// Prevent null byte injection.
if (requestPath.includes("\u0000")) {
return null;
}
// Resolve the path. This will process '..', '.', etc.
// We must treat requestPath as relative to baseDir, so we remove leading slashes.
const resolvedPath = resolve(baseDir, requestPath.replace(/^\/+/, ""));
// Check if the resolved path is a sub-path of baseDir.
const relation = relative(baseDir, resolvedPath);
// If `relation` is empty, it's the base directory itself.
// If it starts with '..', it has escaped the base directory.
// An absolute path as `relation` is also an escape.
if (relation.startsWith("..") || isAbsolute(relation)) {
return null;
}
return resolvedPath;
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To complement the suggested change in
safePath.ts, it would be beneficial to add a test case that verifies that valid paths containing..which resolve within the base directory are handled correctly.