|
| 1 | +// NTFS alternate-data-stream paths are refused on Windows (#5375), checked on |
| 2 | +// any host. As in win32.test.ts, the `path` module is replaced by `path.win32` |
| 3 | +// and `process.platform` reads "win32", so path-validation.ts takes its |
| 4 | +// Windows branch here on macOS and Linux CI too. The refusal happens before |
| 5 | +// any file system call, so a server built for a Windows drive path can be |
| 6 | +// driven over the protocol without that drive existing. |
| 7 | + |
| 8 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 9 | + |
| 10 | +vi.mock("path", async () => { |
| 11 | + const actual = await vi.importActual<typeof import("path")>("path"); |
| 12 | + return { ...actual.win32, default: actual.win32 }; |
| 13 | +}); |
| 14 | +// version.ts finds package.json by walking up a POSIX path, which path.win32 |
| 15 | +// cannot do; the version is irrelevant to these tests. |
| 16 | +vi.mock("../version.js", () => ({ SERVER_VERSION: "0.0.0-test" })); |
| 17 | + |
| 18 | +const { isPathWithinAllowedDirectories, namesAlternateDataStream } = |
| 19 | + await import("../path-validation.js"); |
| 20 | +const { validatePath } = await import("../lib.js"); |
| 21 | +const { call, connect, quietStderr, textOf } = await import("./helpers.js"); |
| 22 | + |
| 23 | +const realPlatform = process.platform; |
| 24 | + |
| 25 | +beforeAll(() => { |
| 26 | + Object.defineProperty(process, "platform", { value: "win32" }); |
| 27 | +}); |
| 28 | + |
| 29 | +afterAll(() => { |
| 30 | + Object.defineProperty(process, "platform", { value: realPlatform }); |
| 31 | +}); |
| 32 | + |
| 33 | +const REFUSAL = /NTFS alternate data stream paths are not supported/; |
| 34 | + |
| 35 | +describe("namesAlternateDataStream on win32", () => { |
| 36 | + it.each([ |
| 37 | + "C:\\work\\file.txt", |
| 38 | + "C:\\", |
| 39 | + "\\\\server\\share\\file.txt", |
| 40 | + "\\\\?\\C:\\work\\file.txt", |
| 41 | + "\\\\.\\C:\\work\\file.txt", |
| 42 | + "\\\\?\\UNC\\server\\share\\file.txt", |
| 43 | + ])("accepts %s, whose only colon is the drive's", (p) => { |
| 44 | + expect(namesAlternateDataStream(p)).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it.each([ |
| 48 | + "C:\\work\\file.txt:stream", |
| 49 | + "C:\\work\\file.txt::$DATA", |
| 50 | + "C:\\work\\file.txt:stream:$DATA", |
| 51 | + "C:\\work:stream", |
| 52 | + "\\\\server\\share\\file.txt:stream", |
| 53 | + "\\\\?\\C:\\work\\file.txt:stream", |
| 54 | + "\\\\?\\UNC\\server\\share\\file.txt:stream", |
| 55 | + "work\\file.txt:stream", |
| 56 | + "AB:\\work", |
| 57 | + ])("refuses %s, which has a colon past the drive prefix", (p) => { |
| 58 | + expect(namesAlternateDataStream(p)).toBe(true); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("isPathWithinAllowedDirectories on win32 refuses alternate streams", () => { |
| 63 | + it("throws for a stream on a file inside an allowed directory", () => { |
| 64 | + expect(() => |
| 65 | + isPathWithinAllowedDirectories("C:\\work\\file.txt:hidden", ["C:\\work"]), |
| 66 | + ).toThrow(REFUSAL); |
| 67 | + }); |
| 68 | + |
| 69 | + it("throws for the default stream spelled out (::$DATA)", () => { |
| 70 | + expect(() => |
| 71 | + isPathWithinAllowedDirectories("C:\\work\\file.txt::$DATA", ["C:\\work"]), |
| 72 | + ).toThrow(REFUSAL); |
| 73 | + }); |
| 74 | + |
| 75 | + // The issue noted this one already failed containment; it stays refused. |
| 76 | + it("throws for a stream on the allowed directory itself", () => { |
| 77 | + expect(() => |
| 78 | + isPathWithinAllowedDirectories("C:\\work:stream", ["C:\\work"]), |
| 79 | + ).toThrow(REFUSAL); |
| 80 | + }); |
| 81 | + |
| 82 | + it("throws for a stream under a drive-root allowed directory", () => { |
| 83 | + expect(() => |
| 84 | + isPathWithinAllowedDirectories("C:\\file.txt:s", ["C:\\"]), |
| 85 | + ).toThrow(REFUSAL); |
| 86 | + }); |
| 87 | + |
| 88 | + it("throws for a stream on a UNC share path", () => { |
| 89 | + expect(() => |
| 90 | + isPathWithinAllowedDirectories("\\\\server\\share\\f.txt:s", [ |
| 91 | + "\\\\server\\share", |
| 92 | + ]), |
| 93 | + ).toThrow(REFUSAL); |
| 94 | + }); |
| 95 | + |
| 96 | + it("throws for a stream behind the \\\\?\\ long-path prefix", () => { |
| 97 | + expect(() => |
| 98 | + isPathWithinAllowedDirectories("\\\\?\\C:\\work\\f.txt:s", [ |
| 99 | + "\\\\?\\C:\\work", |
| 100 | + ]), |
| 101 | + ).toThrow(REFUSAL); |
| 102 | + }); |
| 103 | + |
| 104 | + it("still accepts a \\\\?\\ long path under a directory spelled the same way", () => { |
| 105 | + expect( |
| 106 | + isPathWithinAllowedDirectories("\\\\?\\C:\\work\\f.txt", [ |
| 107 | + "\\\\?\\C:\\work", |
| 108 | + ]), |
| 109 | + ).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + // A drive-relative path resolves against that drive's current directory, |
| 113 | + // so its own colon is the drive's and only a further one is a stream. |
| 114 | + it("accepts a drive-relative path and refuses a stream on one", () => { |
| 115 | + expect(isPathWithinAllowedDirectories("C:file.txt", ["C:\\"])).toBe(true); |
| 116 | + expect(() => |
| 117 | + isPathWithinAllowedDirectories("C:file.txt:s", ["C:\\"]), |
| 118 | + ).toThrow(REFUSAL); |
| 119 | + }); |
| 120 | + |
| 121 | + it("names the requested path in the error", () => { |
| 122 | + expect(() => |
| 123 | + isPathWithinAllowedDirectories("C:\\work\\a.txt:s", ["C:\\work"]), |
| 124 | + ).toThrow("C:\\work\\a.txt:s"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("leaves ordinary paths to the containment check", () => { |
| 128 | + expect( |
| 129 | + isPathWithinAllowedDirectories("C:\\work\\a.txt", ["C:\\work"]), |
| 130 | + ).toBe(true); |
| 131 | + expect( |
| 132 | + isPathWithinAllowedDirectories("D:\\other\\a.txt", ["C:\\work"]), |
| 133 | + ).toBe(false); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +describe("validatePath on win32 refuses alternate streams", () => { |
| 138 | + it("reports the stream refusal, not 'outside allowed directories'", async () => { |
| 139 | + await expect( |
| 140 | + validatePath("C:\\work\\file.txt:hidden", ["C:\\work"]), |
| 141 | + ).rejects.toThrow(REFUSAL); |
| 142 | + }); |
| 143 | + |
| 144 | + it("refuses a relative path that names a stream", async () => { |
| 145 | + await expect(validatePath("file.txt:hidden", ["C:\\work"])).rejects.toThrow( |
| 146 | + REFUSAL, |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +describe("the tools on win32 refuse alternate streams", () => { |
| 152 | + it("returns the refusal as a tool error for a read and a write", async () => { |
| 153 | + quietStderr(); |
| 154 | + const conn = await connect(["C:\\work"]); |
| 155 | + try { |
| 156 | + const read = await call(conn.client, "read_text_file", { |
| 157 | + path: "C:\\work\\notes.txt:hidden", |
| 158 | + }); |
| 159 | + expect(read.isError).toBe(true); |
| 160 | + expect(textOf(read)).toMatch(REFUSAL); |
| 161 | + |
| 162 | + const write = await call(conn.client, "write_file", { |
| 163 | + path: "C:\\work\\notes.txt::$DATA", |
| 164 | + content: "payload", |
| 165 | + }); |
| 166 | + expect(write.isError).toBe(true); |
| 167 | + expect(textOf(write)).toMatch(REFUSAL); |
| 168 | + } finally { |
| 169 | + await conn.close(); |
| 170 | + vi.restoreAllMocks(); |
| 171 | + } |
| 172 | + }); |
| 173 | +}); |
0 commit comments