|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import fs from "node:fs" |
| 3 | +import os from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import { afterEach, beforeEach, describe, it } from "node:test" |
| 6 | + |
| 7 | +import { integerFlag, parseFlagArgs, resolveServedFile, validateRelativeDir, writeTaskAtomically } from "../lib.mjs" |
| 8 | + |
| 9 | +let tmp |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), "gray-screen-test-")) |
| 13 | +}) |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + fs.rmSync(tmp, { recursive: true, force: true }) |
| 17 | +}) |
| 18 | + |
| 19 | +describe("parseFlagArgs / integerFlag", () => { |
| 20 | + it("parses flag/value pairs", () => { |
| 21 | + assert.deepEqual(parseFlagArgs(["--messages", "10", "--two-byte", "true"]), { messages: "10", "two-byte": "true" }) |
| 22 | + }) |
| 23 | + |
| 24 | + it("rejects a flag with a missing operand", () => { |
| 25 | + assert.throws(() => parseFlagArgs(["--messages", "--text-bytes", "5"]), /Missing value for --messages/) |
| 26 | + assert.throws(() => parseFlagArgs(["--messages"]), /Missing value for --messages/) |
| 27 | + }) |
| 28 | + |
| 29 | + it("accepts integers at or above the minimum and rejects everything else", () => { |
| 30 | + assert.equal(integerFlag("messages", "5000", 1), 5000) |
| 31 | + assert.equal(integerFlag("image-every", "0", 0), 0) |
| 32 | + for (const bad of ["0", "-3", "1.5", "abc", "", "NaN"]) { |
| 33 | + assert.throws(() => integerFlag("messages", bad, 1), /--messages must be an integer >= 1/) |
| 34 | + } |
| 35 | + }) |
| 36 | +}) |
| 37 | + |
| 38 | +describe("validateRelativeDir", () => { |
| 39 | + it("accepts plain relative paths", () => { |
| 40 | + for (const ok of [".mock-session", "a", "src/mock_1", "a.b/c-d"]) assert.equal(validateRelativeDir(ok), ok) |
| 41 | + }) |
| 42 | + |
| 43 | + it("rejects shell metacharacters, absolute paths, traversal, dot and option-like segments", () => { |
| 44 | + const bad = ['safe"; touch /tmp/pwned; #', "a b", "$(id)", "/abs", "../x", "a/../b", ".", "a/./b", "-rf", "a/-rf", "", "a//b"] |
| 45 | + for (const dir of bad) assert.throws(() => validateRelativeDir(dir), /Invalid --dir/, JSON.stringify(dir)) |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +describe("resolveServedFile", () => { |
| 50 | + beforeEach(() => { |
| 51 | + fs.mkdirSync(path.join(tmp, "build", "assets"), { recursive: true }) |
| 52 | + fs.mkdirSync(path.join(tmp, "build-secret")) |
| 53 | + fs.writeFileSync(path.join(tmp, "build", "index.html"), "index") |
| 54 | + fs.writeFileSync(path.join(tmp, "build", "assets", "app.js"), "js") |
| 55 | + fs.writeFileSync(path.join(tmp, "build-secret", "credentials.json"), "secret") |
| 56 | + fs.symlinkSync(path.join(tmp, "build-secret"), path.join(tmp, "build", "link")) |
| 57 | + }) |
| 58 | + |
| 59 | + const root = () => path.join(tmp, "build") |
| 60 | + |
| 61 | + it("serves files inside the build directory, with / mapping to index.html", () => { |
| 62 | + assert.equal(resolveServedFile(root(), "/"), fs.realpathSync(path.join(root(), "index.html"))) |
| 63 | + assert.equal(resolveServedFile(root(), "/assets/app.js"), fs.realpathSync(path.join(root(), "assets", "app.js"))) |
| 64 | + }) |
| 65 | + |
| 66 | + it("returns undefined for traversal (plain, encoded and sibling-prefix), symlink escapes, directories and bad input", () => { |
| 67 | + for (const p of [ |
| 68 | + "/../build-secret/credentials.json", |
| 69 | + "/%2e%2e%2fbuild-secret%2fcredentials.json", |
| 70 | + "/link/credentials.json", |
| 71 | + "/assets", |
| 72 | + "/missing.js", |
| 73 | + "/%E0%A4%A", |
| 74 | + "/a\0b", |
| 75 | + ]) { |
| 76 | + assert.equal(resolveServedFile(root(), p), undefined, p) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + it("returns undefined when the build directory does not exist", () => { |
| 81 | + assert.equal(resolveServedFile(path.join(tmp, "nope"), "/"), undefined) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +describe("writeTaskAtomically", () => { |
| 86 | + it("writes every file into tasks/<id> and leaves no staging directory", () => { |
| 87 | + const taskDir = writeTaskAtomically(tmp, "t1", { "a.json": { a: 1 }, "b.json": [2] }) |
| 88 | + |
| 89 | + assert.equal(taskDir, path.join(tmp, "tasks", "t1")) |
| 90 | + assert.deepEqual(JSON.parse(fs.readFileSync(path.join(taskDir, "a.json"), "utf8")), { a: 1 }) |
| 91 | + assert.deepEqual(JSON.parse(fs.readFileSync(path.join(taskDir, "b.json"), "utf8")), [2]) |
| 92 | + assert.deepEqual(fs.readdirSync(tmp).sort(), ["tasks"]) |
| 93 | + }) |
| 94 | + |
| 95 | + it("leaves neither a task directory nor staging files when a write fails", () => { |
| 96 | + const circular = {} |
| 97 | + circular.self = circular |
| 98 | + |
| 99 | + assert.throws(() => writeTaskAtomically(tmp, "t2", { "ok.json": { ok: true }, "bad.json": circular }), /circular/i) |
| 100 | + |
| 101 | + assert.equal(fs.existsSync(path.join(tmp, "tasks", "t2")), false) |
| 102 | + assert.deepEqual(fs.readdirSync(tmp), []) |
| 103 | + }) |
| 104 | +}) |
0 commit comments