|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { isPathInScope } from "../../src/runtime/internal/route-rules-utils"; |
| 3 | + |
| 4 | +// Regression for GHSA-5w89-w975-hf9q: an encoded traversal like `..%2f` must |
| 5 | +// not let a request escape a `/**` proxy scope once the upstream decodes it. |
| 6 | +describe("isPathInScope", () => { |
| 7 | + it("accepts in-scope paths", () => { |
| 8 | + expect(isPathInScope("/api/orders/list.json", "/api/orders")).toBe(true); |
| 9 | + expect(isPathInScope("/api/orders/", "/api/orders")).toBe(true); |
| 10 | + expect(isPathInScope("/api/orders", "/api/orders")).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it("rejects encoded slash traversal (%2f)", () => { |
| 14 | + expect( |
| 15 | + isPathInScope("/api/orders/..%2fadmin%2fconfig.json", "/api/orders") |
| 16 | + ).toBe(false); |
| 17 | + expect(isPathInScope("/api/orders/..%2Fadmin", "/api/orders")).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + it("rejects encoded backslash traversal (%5c)", () => { |
| 21 | + expect(isPathInScope("/api/orders/..%5cadmin", "/api/orders")).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it("rejects double-encoded dot-segments (%2E%2E)", () => { |
| 25 | + expect(isPathInScope("/api/orders/%2E%2E%2Fadmin", "/api/orders")).toBe( |
| 26 | + false |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("rejects literal traversal above scope", () => { |
| 31 | + expect(isPathInScope("/api/orders/../admin", "/api/orders")).toBe(false); |
| 32 | + expect(isPathInScope("/api/orders/../../etc/passwd", "/api/orders")).toBe( |
| 33 | + false |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("keeps traversal confined within scope", () => { |
| 38 | + expect(isPathInScope("/api/orders/foo/../bar", "/api/orders")).toBe(true); |
| 39 | + expect(isPathInScope("/api/orders/foo%2f..%2fbar", "/api/orders")).toBe( |
| 40 | + true |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it("does not confuse sibling prefix with scope", () => { |
| 45 | + expect(isPathInScope("/api/ordersX/list.json", "/api/orders")).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it("allows anything for an empty base (catch-all /**)", () => { |
| 49 | + expect(isPathInScope("/anything/here", "")).toBe(true); |
| 50 | + }); |
| 51 | +}); |
0 commit comments