|
| 1 | +import assert from "node:assert"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { describe, it } from "node:test"; |
| 4 | +import { safePath, validateName, validatePathSegment, validateSessionId } from "./path-safety.js"; |
| 5 | + |
| 6 | +describe("validatePathSegment", () => { |
| 7 | + it("accepts normal strings", () => { |
| 8 | + assert.strictEqual(validatePathSegment("hello", "test"), "hello"); |
| 9 | + assert.strictEqual(validatePathSegment("my-file", "test"), "my-file"); |
| 10 | + assert.strictEqual(validatePathSegment("abc123", "test"), "abc123"); |
| 11 | + }); |
| 12 | + |
| 13 | + it("returns the segment on success", () => { |
| 14 | + const result = validatePathSegment("valid-segment", "label"); |
| 15 | + assert.strictEqual(result, "valid-segment"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("rejects empty string", () => { |
| 19 | + assert.throws(() => validatePathSegment("", "test"), /Invalid test/); |
| 20 | + }); |
| 21 | + |
| 22 | + it("rejects strings containing '..'", () => { |
| 23 | + assert.throws(() => validatePathSegment("..", "test"), /Invalid test/); |
| 24 | + assert.throws(() => validatePathSegment("foo/../bar", "test"), /Invalid test/); |
| 25 | + }); |
| 26 | + |
| 27 | + it("rejects strings containing '/'", () => { |
| 28 | + assert.throws(() => validatePathSegment("foo/bar", "test"), /Invalid test/); |
| 29 | + assert.throws(() => validatePathSegment("/absolute", "test"), /Invalid test/); |
| 30 | + }); |
| 31 | + |
| 32 | + it("rejects strings containing '\\'", () => { |
| 33 | + assert.throws(() => validatePathSegment("foo\\bar", "test"), /Invalid test/); |
| 34 | + }); |
| 35 | + |
| 36 | + it("rejects strings containing null byte", () => { |
| 37 | + assert.throws(() => validatePathSegment("foo\0bar", "test"), /Invalid test/); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("validateSessionId", () => { |
| 42 | + it("accepts valid UUID v4", () => { |
| 43 | + const uuid = "550e8400-e29b-41d4-a716-446655440000"; |
| 44 | + assert.strictEqual(validateSessionId(uuid), uuid); |
| 45 | + }); |
| 46 | + |
| 47 | + it("accepts uppercase UUID", () => { |
| 48 | + const uuid = "550E8400-E29B-41D4-A716-446655440000"; |
| 49 | + assert.strictEqual(validateSessionId(uuid), uuid); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns the id on success", () => { |
| 53 | + const uuid = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"; |
| 54 | + assert.strictEqual(validateSessionId(uuid), uuid); |
| 55 | + }); |
| 56 | + |
| 57 | + it("rejects empty string", () => { |
| 58 | + assert.throws(() => validateSessionId(""), /Invalid session ID/); |
| 59 | + }); |
| 60 | + |
| 61 | + it("rejects non-UUID strings", () => { |
| 62 | + assert.throws(() => validateSessionId("not-a-uuid"), /Invalid session ID/); |
| 63 | + assert.throws(() => validateSessionId("hello"), /Invalid session ID/); |
| 64 | + assert.throws(() => validateSessionId("../etc"), /Invalid session ID/); |
| 65 | + }); |
| 66 | + |
| 67 | + it("rejects partial UUIDs", () => { |
| 68 | + assert.throws(() => validateSessionId("550e8400-e29b-41d4"), /Invalid session ID/); |
| 69 | + assert.throws(() => validateSessionId("550e8400"), /Invalid session ID/); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("validateName", () => { |
| 74 | + it("accepts valid names", () => { |
| 75 | + assert.strictEqual(validateName("cofounder", "agent"), "cofounder"); |
| 76 | + assert.strictEqual(validateName("my-agent", "agent"), "my-agent"); |
| 77 | + assert.strictEqual(validateName("agent_1", "agent"), "agent_1"); |
| 78 | + assert.strictEqual(validateName("ab", "agent"), "ab"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("returns the name on success", () => { |
| 82 | + assert.strictEqual(validateName("researcher", "agent"), "researcher"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("rejects single character", () => { |
| 86 | + assert.throws(() => validateName("a", "agent"), /Invalid agent/); |
| 87 | + }); |
| 88 | + |
| 89 | + it("rejects names with dots", () => { |
| 90 | + assert.throws(() => validateName("my.agent", "agent"), /Invalid agent/); |
| 91 | + }); |
| 92 | + |
| 93 | + it("rejects names with slashes", () => { |
| 94 | + assert.throws(() => validateName("my/agent", "agent"), /Invalid agent/); |
| 95 | + }); |
| 96 | + |
| 97 | + it("rejects names starting with hyphen", () => { |
| 98 | + assert.throws(() => validateName("-agent", "agent"), /Invalid agent/); |
| 99 | + }); |
| 100 | + |
| 101 | + it("rejects names longer than 64 characters", () => { |
| 102 | + const longName = "a".repeat(65); |
| 103 | + assert.throws(() => validateName(longName, "agent"), /Invalid agent/); |
| 104 | + }); |
| 105 | + |
| 106 | + it("rejects empty string", () => { |
| 107 | + assert.throws(() => validateName("", "agent"), /Invalid agent/); |
| 108 | + }); |
| 109 | + |
| 110 | + it("rejects path traversal", () => { |
| 111 | + assert.throws(() => validateName("../etc", "agent"), /Invalid agent/); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("safePath", () => { |
| 116 | + it("returns resolved path for valid segments", () => { |
| 117 | + const result = safePath("/tmp/base", "sub", "file.txt"); |
| 118 | + assert.strictEqual(result, resolve("/tmp/base", "sub", "file.txt")); |
| 119 | + }); |
| 120 | + |
| 121 | + it("throws on traversal attempt", () => { |
| 122 | + assert.throws(() => safePath("/tmp/base", "..", "etc", "passwd"), /Path traversal detected/); |
| 123 | + }); |
| 124 | + |
| 125 | + it("throws on absolute path injection", () => { |
| 126 | + assert.throws(() => safePath("/tmp/base", "/etc/passwd"), /Path traversal detected/); |
| 127 | + }); |
| 128 | + |
| 129 | + it("accepts base directory itself (no segments)", () => { |
| 130 | + const result = safePath("/tmp/base"); |
| 131 | + assert.strictEqual(result, resolve("/tmp/base")); |
| 132 | + }); |
| 133 | + |
| 134 | + it("works with nested valid segments", () => { |
| 135 | + const result = safePath("/tmp/base", "level1", "level2", "level3"); |
| 136 | + assert.strictEqual(result, resolve("/tmp/base", "level1", "level2", "level3")); |
| 137 | + }); |
| 138 | +}); |
0 commit comments