|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { isPathInScope } from "../../src/runtime/internal/route-rules-utils"; |
| 2 | +import { |
| 3 | + isPathInScope, |
| 4 | + isTargetInScope, |
| 5 | +} from "../../src/runtime/internal/route-rules-utils"; |
3 | 6 |
|
4 | 7 | // Regression for GHSA-5w89-w975-hf9q: an encoded traversal like `..%2f` must |
5 | 8 | // not let a request escape a `/**` proxy scope once the upstream decodes it. |
@@ -49,3 +52,92 @@ describe("isPathInScope", () => { |
49 | 52 | expect(isPathInScope("/anything/here", "")).toBe(true); |
50 | 53 | }); |
51 | 54 | }); |
| 55 | + |
| 56 | +// Empty `//` segments and prefix re-entry must not bypass the scope check. |
| 57 | +describe("isPathInScope (empty segments)", () => { |
| 58 | + it("rejects `..` absorbed by an empty segment", () => { |
| 59 | + expect(isPathInScope("/api//..%2fadmin", "/api")).toBe(false); |
| 60 | + expect(isPathInScope("/api//%2e%2e%2fadmin", "/api")).toBe(false); |
| 61 | + expect( |
| 62 | + isPathInScope("/api/orders//..%2f//..%2f//..%2froot.json", "/api/orders") |
| 63 | + ).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it("rejects nested %25 encodings", () => { |
| 67 | + expect(isPathInScope("/api/..%252fadmin", "/api")).toBe(false); |
| 68 | + expect(isPathInScope("/api/%252e%252e%252fadmin", "/api")).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it("allows legitimate empty segments in the tail", () => { |
| 72 | + expect(isPathInScope("/api//foo", "/api")).toBe(true); |
| 73 | + expect(isPathInScope("/api/foo//bar", "/api")).toBe(true); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("isTargetInScope", () => { |
| 78 | + it("allows in-scope targets", () => { |
| 79 | + expect(isTargetInScope("http://up/safe/hello", "http://up/safe")).toBe( |
| 80 | + true |
| 81 | + ); |
| 82 | + expect(isTargetInScope("http://up/safe", "http://up/safe")).toBe(true); |
| 83 | + expect(isTargetInScope("http://up/safe/a?x=..%2f", "http://up/safe")).toBe( |
| 84 | + true |
| 85 | + ); |
| 86 | + expect(isTargetInScope("/safe/a", "/safe")).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it("rejects targets that escape the target base", () => { |
| 90 | + expect(isTargetInScope("http://up/safe/..%2fadmin", "http://up/safe")).toBe( |
| 91 | + false |
| 92 | + ); |
| 93 | + expect( |
| 94 | + isTargetInScope("http://up/safe/..%2Fproxy%2Fadmin", "http://up/safe") |
| 95 | + ).toBe(false); |
| 96 | + expect( |
| 97 | + isTargetInScope( |
| 98 | + "http://up/orders/..%2f//..%2f//..%2froot.json", |
| 99 | + "http://up/orders" |
| 100 | + ) |
| 101 | + ).toBe(false); |
| 102 | + expect(isTargetInScope("/safe/..%2fadmin", "/safe")).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it("rejects raw dot segments combined with encoded separators", () => { |
| 106 | + // fetch/browsers resolve raw `..` with `%2F` kept opaque |
| 107 | + expect( |
| 108 | + isTargetInScope("http://up/safe/a%2fb/../../admin", "http://up/safe") |
| 109 | + ).toBe(false); |
| 110 | + expect(isPathInScope("/api/a%2fb/../../admin", "/api")).toBe(false); |
| 111 | + expect(isPathInScope("/api/x%2fy%2fz/../../../admin", "/api")).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + it("rejects double-encoded traversal without literal %25%25", () => { |
| 115 | + expect(isPathInScope("/api/..%25%32%66..%25%32%66admin", "/api")).toBe( |
| 116 | + false |
| 117 | + ); |
| 118 | + expect(isPathInScope("/api/..%2%66admin", "/api")).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it("supports encoded or non-canonical target bases", () => { |
| 122 | + const gitlab = "https://gitlab.com/api/v4/projects/grp%2Fproj/repository"; |
| 123 | + expect(isTargetInScope(`${gitlab}/files/a.txt`, gitlab)).toBe(true); |
| 124 | + expect(isTargetInScope(`${gitlab}/..%2f..%2fother`, gitlab)).toBe(false); |
| 125 | + expect(isTargetInScope("http://up/100%25/x", "http://up/100%25")).toBe( |
| 126 | + true |
| 127 | + ); |
| 128 | + expect(isTargetInScope("http://up/v1%2E0/x", "http://up/v1%2E0")).toBe( |
| 129 | + true |
| 130 | + ); |
| 131 | + expect(isTargetInScope("http://up/a//b/x", "http://up/a//b")).toBe(true); |
| 132 | + expect(isTargetInScope("http://up/a/./b/x", "http://up/a/./b")).toBe(true); |
| 133 | + // decoded base must not let the WHATWG reading escape |
| 134 | + expect(isTargetInScope("http://up/a%2Fb/../b/x", "http://up/a%2Fb")).toBe( |
| 135 | + false |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it("allows anything for a root target base", () => { |
| 140 | + expect(isTargetInScope("http://up/a/b", "http://up")).toBe(true); |
| 141 | + expect(isTargetInScope("http://up/a/b", "http://up/")).toBe(true); |
| 142 | + }); |
| 143 | +}); |
0 commit comments