A Claude skill that turns vibecoded, AI-looking UI into a designed interface
JavaScript
fix(story): block sibling-directory traversal in the story server (#32)
Dhruv Arorae10358f10c86
diff --git a/skills/unslop/scripts/story-path.mjs b/skills/unslop/scripts/story-path.mjsnew file mode 100644index 0000000..3e685d7--- /dev/null+++ b/skills/unslop/scripts/story-path.mjs@@ -0,0 +1,27 @@+import { existsSync, realpathSync } from "node:fs";+import { isAbsolute, join, normalize, relative, sep } from "node:path";++// Resolve a story-server request path to a file inside root, or null+// when the path does not name a file under it. The request path has+// already had its query string stripped and its escapes decoded.+//+// A bare `target.startsWith(root)` is not enough: a sibling directory+// whose name starts with the root's (`/a/site` vs `/a/site-private`)+// passes it, and so does a symlink inside root that points outside.+// Both sides are resolved to their real paths first, and the root+// itself is rejected too — it is a directory, not a file to serve.+export function resolveStoryFile(root, requestPath) {+ const target = normalize(join(root, requestPath.replace(/^\/__story\//, "/")));+ if (!existsSync(target)) return null;+ let realRoot;+ let realTarget;+ try {+ realRoot = realpathSync(root);+ realTarget = realpathSync(target);+ } catch {+ return null;+ }+ const rel = relative(realRoot, realTarget);+ if (rel === "" || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return null;+ return realTarget;+}diff --git a/skills/unslop/scripts/story.mjs b/skills/unslop/scripts/story.mjsindex a9565db..fd9bc1d 100644--- a/skills/unslop/scripts/story.mjs+++ b/skills/unslop/scripts/story.mjs@@ -2,7 +2,8 @@ import { createRequire } from "node:module";import { createServer } from "node:http";import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { execFileSync } from "node:child_process";-import { dirname, extname, join, normalize, resolve } from "node:path";+import { dirname, extname, join, resolve } from "node:path";+import { resolveStoryFile } from "./story-path.mjs";const usage = "usage: node story.mjs <story.html> <out.mp4> [--wide], or node story.mjs <story.html> --play to watch it live in a browser window";const args = process.argv.slice(2);@@ -384,8 +385,8 @@ const server = createServer((request, response) => {response.writeHead(200, { "content-type": "text/html" });return response.end(stage);}- const target = normalize(join(root, path.replace(/^\/__story\//, "/")));- if (!target.startsWith(root) || !existsSync(target)) {+ const target = resolveStoryFile(root, path);+ if (target === null) {response.writeHead(404);return response.end();}diff --git a/test/story-path.test.mjs b/test/story-path.test.mjsnew file mode 100644index 0000000..ac2c479--- /dev/null+++ b/test/story-path.test.mjs@@ -0,0 +1,68 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import { fileURLToPath } from "node:url";+import { resolveStoryFile } from "../skills/unslop/scripts/story-path.mjs";++// A root with a sibling whose name starts with the root's, the shape+// the prefix check gets wrong: /tmp/x/site vs /tmp/x/site-private.+const setup = () => {+ const base = mkdtempSync(join(tmpdir(), "unslop-story-path-"));+ const root = join(base, "site");+ const sibling = join(base, "site-private");+ mkdirSync(join(root, "sub"), { recursive: true });+ mkdirSync(sibling, { recursive: true });+ writeFileSync(join(root, "asset.txt"), "asset");+ writeFileSync(join(root, "sub", "inner.txt"), "inner");+ writeFileSync(join(sibling, "secret.txt"), "secret");+ symlinkSync(join(sibling, "secret.txt"), join(root, "link-out"));+ symlinkSync(join(root, "asset.txt"), join(root, "link-in"));+ return { root, sibling };+};++test("serves a file directly under root", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/asset.txt"), join(root, "asset.txt"));+});++test("serves a nested file under root", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/sub/inner.txt"), join(root, "sub", "inner.txt"));+});++test("serves a path that normalizes back inside root", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/sub/../asset.txt"), join(root, "asset.txt"));+});++test("rejects a missing file", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/nope.txt"), null);+});++test("rejects the root itself, which is a directory, not a file", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/"), null);+});++test("rejects a sibling directory whose name starts with the root's", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/../site-private/secret.txt"), null);+});++test("rejects a deeper escape through a nested path", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/sub/../../site-private/secret.txt"), null);+});++test("rejects a symlink inside root that points outside it", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/link-out"), null);+});++test("serves a symlink inside root that points inside it", () => {+ const { root } = setup();+ assert.equal(resolveStoryFile(root, "/__story/link-in"), join(root, "asset.txt"));+});