@zereight/mcp-gitlab 2.1.70 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/README.ko.md +3 -1
  2. package/README.md +6 -2
  3. package/README.pt-BR.md +1 -1
  4. package/README.zh-CN.md +3 -1
  5. package/build/config.js +12 -4
  6. package/build/deprecated-env.js +47 -0
  7. package/build/index.js +369 -184
  8. package/build/oauth-device-flow.js +80 -0
  9. package/build/oauth.js +135 -18
  10. package/build/schemas.js +1 -1
  11. package/build/server/remote-http-stack.js +39 -0
  12. package/build/tools/discover-activation.js +38 -0
  13. package/build/tools/exposed-tools.js +35 -0
  14. package/build/tools/listed-tools.js +76 -0
  15. package/build/tools/registry.js +226 -57
  16. package/build/tools/tool-profile.js +66 -0
  17. package/build/utils/compact-tool-allowlist.js +14 -0
  18. package/build/utils/logger.js +34 -12
  19. package/package.json +3 -3
  20. package/build/scripts/check-skill-sync.js +0 -143
  21. package/build/scripts/generate-server-card.js +0 -39
  22. package/build/scripts/generate-tool-coverage.js +0 -58
  23. package/build/scripts/generate-tool-docs.js +0 -430
  24. package/build/scripts/tool-coverage/coverage.js +0 -341
  25. package/build/scripts/tool-coverage/coverage.test.js +0 -142
  26. package/build/test/callback-proxy-tests.js +0 -321
  27. package/build/test/cli/args.test.js +0 -175
  28. package/build/test/cli/boolean-flags.test.js +0 -106
  29. package/build/test/cli/compact-mcp-result.test.js +0 -93
  30. package/build/test/cli/credentials.test.js +0 -153
  31. package/build/test/cli/exposure.test.js +0 -104
  32. package/build/test/cli/load-config-snapshot.js +0 -5
  33. package/build/test/cli/output.test.js +0 -74
  34. package/build/test/cli/replay-command.test.js +0 -37
  35. package/build/test/cli/router.test.js +0 -183
  36. package/build/test/client-pool-test.js +0 -109
  37. package/build/test/clients/client.js +0 -34
  38. package/build/test/clients/custom-header-client.js +0 -122
  39. package/build/test/clients/sse-client.js +0 -95
  40. package/build/test/clients/stdio-client.js +0 -113
  41. package/build/test/clients/streamable-http-client.js +0 -95
  42. package/build/test/config-allowed-groups.test.js +0 -97
  43. package/build/test/create-commit-actions.test.js +0 -102
  44. package/build/test/draft-note-schema.test.js +0 -44
  45. package/build/test/dynamic-api-url-allowlist.test.js +0 -121
  46. package/build/test/dynamic-api-url-test.js +0 -304
  47. package/build/test/dynamic-routing-tests.js +0 -495
  48. package/build/test/get-merge-request-discussion-schema.test.js +0 -30
  49. package/build/test/gitlab-api-tool-errors.test.js +0 -112
  50. package/build/test/gitlab-artifact-entry-schema.test.js +0 -44
  51. package/build/test/gitlab-diff-schema.test.js +0 -36
  52. package/build/test/gitlab-pipeline-schedule-schema.test.js +0 -32
  53. package/build/test/gitlab-tag-schema.test.js +0 -51
  54. package/build/test/group-milestone-schema.test.js +0 -45
  55. package/build/test/mcp-oauth-tests.js +0 -905
  56. package/build/test/mcp-server-name.test.js +0 -87
  57. package/build/test/merge-merge-request-schema.test.js +0 -75
  58. package/build/test/multi-server-test.js +0 -190
  59. package/build/test/no-proxy-integration-test.js +0 -183
  60. package/build/test/no-proxy-test.js +0 -149
  61. package/build/test/nullable-gitlab-response-fields.test.js +0 -37
  62. package/build/test/nullish-tool-arguments-schema.test.js +0 -118
  63. package/build/test/oauth-device-flow-tests.js +0 -450
  64. package/build/test/oauth-download-url.test.js +0 -157
  65. package/build/test/oauth-startup.test.js +0 -204
  66. package/build/test/oauth-tests.js +0 -428
  67. package/build/test/path-segment-encoding.test.js +0 -98
  68. package/build/test/remote-auth-simple-test.js +0 -277
  69. package/build/test/remote-auth-tests.js +0 -314
  70. package/build/test/response-masking.test.js +0 -445
  71. package/build/test/schema-tests.js +0 -1679
  72. package/build/test/server/metrics.test.js +0 -47
  73. package/build/test/server/request-helpers.test.js +0 -48
  74. package/build/test/server/version.test.js +0 -13
  75. package/build/test/sse-auth-guard.test.js +0 -96
  76. package/build/test/sse-session-limits.test.js +0 -273
  77. package/build/test/stateless/callback-proxy.test.js +0 -510
  78. package/build/test/stateless/client-id.test.js +0 -177
  79. package/build/test/stateless/codec.test.js +0 -328
  80. package/build/test/stateless/config-ttl.test.js +0 -149
  81. package/build/test/stateless/consumed-proxy-code-cache.test.js +0 -150
  82. package/build/test/stateless/session-id-integration.test.js +0 -675
  83. package/build/test/stateless/session-id.test.js +0 -157
  84. package/build/test/streamable-http-concurrent-session.test.js +0 -171
  85. package/build/test/streamable-http-dns-rebinding.test.js +0 -185
  86. package/build/test/streamable-http-sse-stream.test.js +0 -350
  87. package/build/test/streamable-http-static-token-auth.test.js +0 -157
  88. package/build/test/streamable-http-unauthenticated-discovery.test.js +0 -207
  89. package/build/test/test-all-transport-server.js +0 -255
  90. package/build/test/test-auth-retry.js +0 -195
  91. package/build/test/test-blank-filters.js +0 -156
  92. package/build/test/test-ci-catalog.js +0 -177
  93. package/build/test/test-ci-lint.js +0 -191
  94. package/build/test/test-ci-variables.js +0 -309
  95. package/build/test/test-create-repository.js +0 -120
  96. package/build/test/test-dependency-proxy.js +0 -195
  97. package/build/test/test-deployment-tools.js +0 -463
  98. package/build/test/test-download-attachment.js +0 -190
  99. package/build/test/test-dynamic-project-scope.js +0 -524
  100. package/build/test/test-empty-toolsets.js +0 -535
  101. package/build/test/test-get-file-blame.js +0 -145
  102. package/build/test/test-get-file-contents.js +0 -128
  103. package/build/test/test-geteffectiveprojectid.js +0 -804
  104. package/build/test/test-health-check.js +0 -115
  105. package/build/test/test-issue-description-patch.js +0 -256
  106. package/build/test/test-job-artifacts.js +0 -275
  107. package/build/test/test-json-schema.js +0 -166
  108. package/build/test/test-list-group-members.js +0 -150
  109. package/build/test/test-list-group-merge-requests.js +0 -165
  110. package/build/test/test-list-issues.js +0 -170
  111. package/build/test/test-list-merge-requests.js +0 -201
  112. package/build/test/test-list-milestones.js +0 -145
  113. package/build/test/test-list-project-members.js +0 -132
  114. package/build/test/test-merge-request-approval-state-tools.js +0 -213
  115. package/build/test/test-merge-request-approvals.js +0 -187
  116. package/build/test/test-merge-request-pipelines.js +0 -106
  117. package/build/test/test-mr-diffs-filter.js +0 -132
  118. package/build/test/test-mr-file-diffs.js +0 -251
  119. package/build/test/test-oauth-proxy-rate-limit.js +0 -133
  120. package/build/test/test-orbit.js +0 -155
  121. package/build/test/test-permission-mode.js +0 -424
  122. package/build/test/test-pipeline-schedules.js +0 -484
  123. package/build/test/test-protected-branches.js +0 -174
  124. package/build/test/test-remote-downloads.js +0 -510
  125. package/build/test/test-search-code.js +0 -275
  126. package/build/test/test-snippets.js +0 -735
  127. package/build/test/test-tags.js +0 -220
  128. package/build/test/test-todos.js +0 -195
  129. package/build/test/test-token-optimizations.js +0 -691
  130. package/build/test/test-toolset-filtering.js +0 -499
  131. package/build/test/test-update-issue-slim.js +0 -141
  132. package/build/test/test-update-project.js +0 -112
  133. package/build/test/test-upload-markdown.js +0 -206
  134. package/build/test/test-vulnerabilities.js +0 -399
  135. package/build/test/test-webhooks.js +0 -218
  136. package/build/test/tool-description-quality.test.js +0 -50
  137. package/build/test/utils/bulk-publish-options.test.js +0 -32
  138. package/build/test/utils/download-token.test.js +0 -35
  139. package/build/test/utils/forwarded-public-base-url.test.js +0 -46
  140. package/build/test/utils/gitlab-instance-version.test.js +0 -32
  141. package/build/test/utils/gitlab-version-gate.test.js +0 -74
  142. package/build/test/utils/graphql-query.test.js +0 -168
  143. package/build/test/utils/helpers-path.test.js +0 -104
  144. package/build/test/utils/jmespath-tool-result.test.js +0 -91
  145. package/build/test/utils/masking.test.js +0 -384
  146. package/build/test/utils/merge-request-position.test.js +0 -57
  147. package/build/test/utils/mock-gitlab-server.js +0 -614
  148. package/build/test/utils/proxy-client-ip.test.js +0 -28
  149. package/build/test/utils/redact-sensitive.test.js +0 -32
  150. package/build/test/utils/safe-redirect-fetch.test.js +0 -463
  151. package/build/test/utils/server-launcher.js +0 -231
  152. package/build/test/utils/tool-args.test.js +0 -210
  153. package/build/test/utils/url.test.js +0 -141
  154. package/build/test/utils/version-check.test.js +0 -239
  155. package/build/test/utils/wiki-title.test.js +0 -21
  156. package/build/test-note.js +0 -54
  157. package/build/test-resolve-issue-note.js +0 -127
@@ -1,463 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import http from "node:http";
3
- import { after, before, describe, test } from "node:test";
4
- import { Agent, MockAgent } from "undici";
5
- import { fetchWithValidatedRedirects, isNonPublicAddress, UnsafeRedirectError, } from "../../utils/safe-redirect-fetch.js";
6
- const dispatcher = new Agent();
7
- /** A public IPv4 literal, so hop checks never depend on DNS resolution. */
8
- const PUBLIC_HOST = "93.184.216.34";
9
- let server;
10
- let origin;
11
- let serverPort;
12
- let secondServer;
13
- let secondOrigin;
14
- let secondPort;
15
- let lastSecondHeaders = {};
16
- /** Hosts the trusted-host predicate has been called with. */
17
- const trustedHostArguments = [];
18
- function listen(target) {
19
- return new Promise(resolve => {
20
- target.listen(0, "127.0.0.1", () => {
21
- const address = target.address();
22
- if (!address || typeof address === "string")
23
- throw new Error("no port");
24
- resolve(address.port);
25
- });
26
- });
27
- }
28
- /**
29
- * A fetch stub that answers the first request with a redirect and every later one
30
- * with a body, recording the headers used on each hop. Injected so hop policy can
31
- * be asserted without reaching the network.
32
- */
33
- function stubRedirectFetch(location, calls) {
34
- const impl = async (input, init) => {
35
- calls.push({ url: String(input), headers: { ...(init?.headers ?? {}) } });
36
- if (calls.length === 1 && location) {
37
- return new Response(null, { status: 302, headers: { location } });
38
- }
39
- return new Response("payload", { status: 200 });
40
- };
41
- return impl;
42
- }
43
- /**
44
- * A fetch stub that walks a list of redirect locations: call N answers with
45
- * `locations[N - 1]` and the call after the last location returns a body. Used to
46
- * assert the header policy on chains longer than one hop.
47
- */
48
- function stubRedirectChain(locations, calls) {
49
- const impl = async (input, init) => {
50
- calls.push({ url: String(input), headers: { ...(init?.headers ?? {}) } });
51
- const location = locations[calls.length - 1];
52
- if (location) {
53
- return new Response(null, { status: 302, headers: { location } });
54
- }
55
- return new Response("payload", { status: 200 });
56
- };
57
- return impl;
58
- }
59
- /**
60
- * Pass one injected client for both the hops that carry the credentials and the ones the
61
- * helper strips. The stripped hops default to plain `undici`, so injecting only
62
- * `fetchImpl` in a test would send the hop it wants to inspect to the network.
63
- */
64
- function sameClientForEveryHop(stub) {
65
- return { fetchImpl: stub, unauthenticatedFetchImpl: stub };
66
- }
67
- describe("safe redirect fetch", () => {
68
- before(async () => {
69
- server = http.createServer((req, res) => {
70
- const url = new URL(req.url ?? "/", "http://127.0.0.1");
71
- switch (url.pathname) {
72
- case "/file":
73
- res.writeHead(200, { "content-type": "application/octet-stream" });
74
- res.end("payload");
75
- return;
76
- case "/same-origin-redirect":
77
- res.writeHead(302, { location: "/file" });
78
- res.end();
79
- return;
80
- case "/cross-origin-redirect":
81
- res.writeHead(302, { location: `${secondOrigin}/file` });
82
- res.end();
83
- return;
84
- case "/loopback-redirect":
85
- res.writeHead(302, { location: "http://127.0.0.1:1/file" });
86
- res.end();
87
- return;
88
- case "/metadata-redirect":
89
- res.writeHead(302, { location: "http://169.254.169.254/latest/meta-data/" });
90
- res.end();
91
- return;
92
- case "/mapped-metadata-redirect":
93
- res.writeHead(302, { location: "http://[::ffff:169.254.169.254]/latest/meta-data/" });
94
- res.end();
95
- return;
96
- case "/mapped-loopback-redirect":
97
- res.writeHead(302, { location: "http://[::ffff:7f00:1]:1/file" });
98
- res.end();
99
- return;
100
- case "/dotted-mapped-loopback-redirect":
101
- res.writeHead(302, { location: "http://[::ffff:127.0.0.1]:1/file" });
102
- res.end();
103
- return;
104
- case "/nat64-metadata-redirect":
105
- res.writeHead(302, { location: "http://[64:ff9b::a9fe:a9fe]/latest/meta-data/" });
106
- res.end();
107
- return;
108
- case "/file-protocol-redirect":
109
- res.writeHead(302, { location: "file:///etc/passwd" });
110
- res.end();
111
- return;
112
- case "/loop":
113
- res.writeHead(302, { location: "/loop" });
114
- res.end();
115
- return;
116
- default:
117
- res.writeHead(404);
118
- res.end();
119
- }
120
- });
121
- secondServer = http.createServer((req, res) => {
122
- lastSecondHeaders = req.headers;
123
- res.writeHead(200).end("second");
124
- });
125
- serverPort = await listen(server);
126
- secondPort = await listen(secondServer);
127
- origin = `http://127.0.0.1:${serverPort}`;
128
- secondOrigin = `http://127.0.0.1:${secondPort}`;
129
- });
130
- after(async () => {
131
- await Promise.all([
132
- new Promise(resolve => server.close(resolve)),
133
- new Promise(resolve => secondServer.close(resolve)),
134
- dispatcher.close(),
135
- ]);
136
- });
137
- const rejectsWith = (promise, fragment) => assert.rejects(promise, (error) => error instanceof UnsafeRedirectError && error.message.includes(fragment));
138
- test("follows a same-origin redirect", async () => {
139
- const response = await fetchWithValidatedRedirects(`${origin}/same-origin-redirect`, {
140
- headers: {},
141
- dispatcher,
142
- });
143
- assert.equal(response.status, 200);
144
- assert.equal(await response.text(), "payload");
145
- });
146
- test("refuses a redirect to a loopback address on another origin", async () => {
147
- await rejectsWith(() => fetchWithValidatedRedirects(`${origin}/loopback-redirect`, { headers: {}, dispatcher }), "non-public address");
148
- });
149
- test("refuses a redirect to the cloud instance metadata address", async () => {
150
- await rejectsWith(() => fetchWithValidatedRedirects(`${origin}/metadata-redirect`, { headers: {}, dispatcher }), "169.254.169.254");
151
- });
152
- // The URL parser canonicalizes IPv4-mapped hosts to hex (`::ffff:169.254.169.254`
153
- // becomes `::ffff:a9fe:a9fe`), so the check has to classify parsed bits. These
154
- // routes exercise the production path: `new URL(location, currentUrl)`.
155
- for (const [route, address] of [
156
- ["/mapped-metadata-redirect", "::ffff:a9fe:a9fe"],
157
- ["/mapped-loopback-redirect", "::ffff:7f00:1"],
158
- ["/dotted-mapped-loopback-redirect", "::ffff:7f00:1"],
159
- ["/nat64-metadata-redirect", "64:ff9b::a9fe:a9fe"],
160
- ]) {
161
- test(`refuses a redirect to the IPv6 form ${route} (${address})`, async () => {
162
- await rejectsWith(() => fetchWithValidatedRedirects(`${origin}${route}`, { headers: {}, dispatcher }), "non-public address");
163
- });
164
- }
165
- test("refuses a redirect to a non-http protocol", async () => {
166
- await rejectsWith(() => fetchWithValidatedRedirects(`${origin}/file-protocol-redirect`, {
167
- headers: {},
168
- dispatcher,
169
- }), "unsupported protocol");
170
- });
171
- test("refuses to follow more redirects than the limit allows", async () => {
172
- await rejectsWith(() => fetchWithValidatedRedirects(`${origin}/loop`, {
173
- headers: {},
174
- dispatcher,
175
- maxRedirects: 2,
176
- }), "Too many redirects");
177
- });
178
- test("follows a non-public redirect target when the host:port is trusted", async () => {
179
- trustedHostArguments.length = 0;
180
- const response = await fetchWithValidatedRedirects(`${origin}/cross-origin-redirect`, {
181
- headers: {},
182
- dispatcher,
183
- isTrustedRedirectHost: host => {
184
- trustedHostArguments.push(host);
185
- return host === `127.0.0.1:${secondPort}`;
186
- },
187
- });
188
- assert.equal(response.status, 200);
189
- assert.equal(await response.text(), "second");
190
- // The allowlist is keyed by `URL.host`, so the predicate must receive the port
191
- // with the host: matching on a bare hostname would never find `:8443` entries.
192
- assert.deepEqual(trustedHostArguments, [`127.0.0.1:${secondPort}`]);
193
- });
194
- test("the URL parser canonicalizes IPv4-mapped literals before the check runs", () => {
195
- assert.equal(new URL("http://[::ffff:169.254.169.254]/").hostname, "[::ffff:a9fe:a9fe]");
196
- assert.equal(new URL("http://[::ffff:7f00:1]/").hostname, "[::ffff:7f00:1]");
197
- assert.equal(new URL("http://[::ffff:127.0.0.1]:1/").hostname, "[::ffff:7f00:1]");
198
- assert.equal(new URL("http://[64:ff9b::a9fe:a9fe]/").hostname, "[64:ff9b::a9fe:a9fe]");
199
- });
200
- test("classifies address ranges", () => {
201
- for (const address of [
202
- // IPv4
203
- "127.0.0.1",
204
- "10.1.2.3",
205
- "172.16.0.1",
206
- "192.168.1.1",
207
- "169.254.169.254",
208
- "100.64.0.1",
209
- "0.0.0.0",
210
- // IPv6
211
- "::",
212
- "::1",
213
- "fd00::1",
214
- "fe80::1",
215
- "fea0::1",
216
- "febf::1",
217
- "fec0::1",
218
- // IPv6 carrying a non-public IPv4 address
219
- "::ffff:127.0.0.1",
220
- "::ffff:a9fe:a9fe",
221
- "::ffff:7f00:1",
222
- "::ffff:0:127.0.0.1",
223
- "::7f00:1",
224
- "64:ff9b::a9fe:a9fe",
225
- "64:ff9b:1::a9fe:a9fe",
226
- "2002:a9fe:a9fe::",
227
- // Not a literal address this helper understands — fail closed
228
- "[::ffff:a9fe:a9fe]",
229
- "not-an-address",
230
- ]) {
231
- assert.equal(isNonPublicAddress(address), true, `${address} should be non-public`);
232
- }
233
- for (const address of [
234
- "8.8.8.8",
235
- "1.1.1.1",
236
- "2606:4700::1111",
237
- // IPv6 carrying a public IPv4 address
238
- "::ffff:8.8.8.8",
239
- "::ffff:808:808",
240
- "64:ff9b::808:808",
241
- "2002:0808:0808::",
242
- ]) {
243
- assert.equal(isNonPublicAddress(address), false, `${address} should be public`);
244
- }
245
- });
246
- });
247
- describe("credential headers across redirect hops", () => {
248
- const credentials = {
249
- Accept: "application/octet-stream",
250
- Authorization: "Bearer secret",
251
- "Private-Token": "secret",
252
- };
253
- test("keeps credentials on a same-origin hop", async () => {
254
- const calls = [];
255
- const response = await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
256
- headers: credentials,
257
- fetchImpl: stubRedirectFetch("/file", calls),
258
- });
259
- assert.equal(response.status, 200);
260
- assert.deepEqual(calls.map(call => call.url), ["http://127.0.0.1:1/start", "http://127.0.0.1:1/file"]);
261
- assert.equal(calls[1].headers.Authorization, "Bearer secret");
262
- assert.equal(calls[1].headers["Private-Token"], "secret");
263
- });
264
- test("drops credential headers on a cross-origin hop to a public host", async () => {
265
- const calls = [];
266
- const response = await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
267
- headers: credentials,
268
- ...sameClientForEveryHop(stubRedirectFetch(`http://${PUBLIC_HOST}/file`, calls)),
269
- });
270
- assert.equal(response.status, 200);
271
- assert.equal(calls[0].headers["Private-Token"], "secret");
272
- assert.equal(calls[1].headers.Authorization, undefined);
273
- assert.equal(calls[1].headers["Private-Token"], undefined);
274
- // Non-credential headers still travel with the request.
275
- assert.equal(calls[1].headers.Accept, "application/octet-stream");
276
- });
277
- test("drops additional header names passed as credentialHeaders", async () => {
278
- const calls = [];
279
- await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
280
- headers: { ...credentials, "X-Gitlab-Session": "secret" },
281
- credentialHeaders: ["authorization", "private-token", "x-gitlab-session"],
282
- ...sameClientForEveryHop(stubRedirectFetch(`http://${PUBLIC_HOST}/file`, calls)),
283
- });
284
- assert.equal(calls[1].headers["X-Gitlab-Session"], undefined);
285
- assert.equal(calls[1].headers.Accept, "application/octet-stream");
286
- });
287
- test("keeps credentials on a hop to a trusted host", async () => {
288
- const calls = [];
289
- const seen = [];
290
- await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
291
- headers: credentials,
292
- isTrustedRedirectHost: host => {
293
- seen.push(host);
294
- return host === PUBLIC_HOST;
295
- },
296
- fetchImpl: stubRedirectFetch(`http://${PUBLIC_HOST}/file`, calls),
297
- });
298
- assert.deepEqual(seen, [PUBLIC_HOST]);
299
- assert.equal(calls[1].headers.Authorization, "Bearer secret");
300
- assert.equal(calls[1].headers["Private-Token"], "secret");
301
- });
302
- test("still refuses an untrusted non-public target even without credentials", async () => {
303
- const calls = [];
304
- await assert.rejects(() => fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
305
- headers: credentials,
306
- fetchImpl: stubRedirectFetch("http://169.254.169.254/latest/meta-data/", calls),
307
- }), (error) => error instanceof UnsafeRedirectError && error.message.includes("non-public address"));
308
- assert.equal(calls.length, 1);
309
- });
310
- const rejectsWithCleartext = (promise) => assert.rejects(promise, (error) => error instanceof UnsafeRedirectError && error.message.includes("cleartext"));
311
- test("refuses an https-to-http downgrade even for a trusted host", async () => {
312
- const calls = [];
313
- await rejectsWithCleartext(() => fetchWithValidatedRedirects(`https://${PUBLIC_HOST}/start`, {
314
- headers: credentials,
315
- // Trust must not re-enable the credentials on a cleartext hop.
316
- isTrustedRedirectHost: () => true,
317
- fetchImpl: stubRedirectFetch(`http://${PUBLIC_HOST}/file`, calls),
318
- }));
319
- assert.deepEqual(calls.map(call => call.url), [`https://${PUBLIC_HOST}/start`]);
320
- });
321
- test("refuses a downgrade that happens after an earlier https hop", async () => {
322
- const calls = [];
323
- await rejectsWithCleartext(() => fetchWithValidatedRedirects(`https://${PUBLIC_HOST}/start`, {
324
- headers: credentials,
325
- fetchImpl: stubRedirectChain([`https://${PUBLIC_HOST}/mid`, `http://${PUBLIC_HOST}/file`], calls),
326
- }));
327
- assert.deepEqual(calls.map(call => call.url), [`https://${PUBLIC_HOST}/start`, `https://${PUBLIC_HOST}/mid`]);
328
- });
329
- test("follows an http-to-https upgrade and keeps credentials on a trusted host", async () => {
330
- const calls = [];
331
- const response = await fetchWithValidatedRedirects(`http://${PUBLIC_HOST}/start`, {
332
- headers: credentials,
333
- isTrustedRedirectHost: host => host === PUBLIC_HOST,
334
- fetchImpl: stubRedirectFetch(`https://${PUBLIC_HOST}/file`, calls),
335
- });
336
- assert.equal(response.status, 200);
337
- assert.equal(calls[1].headers.Authorization, "Bearer secret");
338
- assert.equal(calls[1].headers["Private-Token"], "secret");
339
- });
340
- test("treats a scheme change to an untrusted host as cross-origin", async () => {
341
- const calls = [];
342
- await fetchWithValidatedRedirects(`http://${PUBLIC_HOST}/start`, {
343
- headers: credentials,
344
- ...sameClientForEveryHop(stubRedirectFetch(`https://${PUBLIC_HOST}/file`, calls)),
345
- });
346
- assert.equal(calls[1].headers.Authorization, undefined);
347
- assert.equal(calls[1].headers["Private-Token"], undefined);
348
- assert.equal(calls[1].headers.Accept, "application/octet-stream");
349
- });
350
- test("keeps the credentials off a hop that returns to the initial origin", async () => {
351
- const calls = [];
352
- const response = await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
353
- headers: credentials,
354
- ...sameClientForEveryHop(stubRedirectChain([`http://${PUBLIC_HOST}/mid`, "http://127.0.0.1:1/file"], calls)),
355
- });
356
- assert.equal(response.status, 200);
357
- assert.deepEqual(calls.map(call => call.url), ["http://127.0.0.1:1/start", `http://${PUBLIC_HOST}/mid`, "http://127.0.0.1:1/file"]);
358
- // The untrusted hop keeps the token out of the request...
359
- assert.equal(calls[1].headers["Private-Token"], undefined);
360
- // ...and a target cannot get it back by bouncing the request to the origin, whose
361
- // response the caller of this helper reads as the downloaded body.
362
- assert.equal(calls[2].headers.Authorization, undefined);
363
- assert.equal(calls[2].headers["Private-Token"], undefined);
364
- });
365
- test("restores credentials on a return to the origin only after a trusted hop", async () => {
366
- const calls = [];
367
- await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
368
- headers: credentials,
369
- isTrustedRedirectHost: host => host === PUBLIC_HOST,
370
- fetchImpl: stubRedirectChain([`http://${PUBLIC_HOST}/mid`, "http://127.0.0.1:1/file"], calls),
371
- });
372
- // The trusted hop already received the token, so the return hop may carry it too.
373
- assert.equal(calls[1].headers["Private-Token"], "secret");
374
- assert.equal(calls[2].headers["Private-Token"], "secret");
375
- });
376
- test("stays without credentials across untrusted hops and returns without them", async () => {
377
- const calls = [];
378
- await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
379
- headers: credentials,
380
- ...sameClientForEveryHop(stubRedirectChain([`http://${PUBLIC_HOST}/mid`, `http://${PUBLIC_HOST}/other`, "http://127.0.0.1:1/file"], calls)),
381
- });
382
- assert.equal(calls[1].headers["Private-Token"], undefined);
383
- assert.equal(calls[2].headers["Private-Token"], undefined);
384
- assert.equal(calls[3].headers["Private-Token"], undefined);
385
- });
386
- test("a trusted hop after an untrusted one stays without credentials", async () => {
387
- const calls = [];
388
- await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
389
- headers: credentials,
390
- isTrustedRedirectHost: host => host === "93.184.216.35",
391
- ...sameClientForEveryHop(stubRedirectChain([`http://${PUBLIC_HOST}/mid`, "http://93.184.216.35/storage", "http://127.0.0.1:1/file"], calls)),
392
- });
393
- // The untrusted hop chose the trusted URL, so trust must not re-attach the token.
394
- assert.equal(calls[1].headers["Private-Token"], undefined);
395
- assert.equal(calls[2].headers.Authorization, undefined);
396
- assert.equal(calls[2].headers["Private-Token"], undefined);
397
- assert.equal(calls[3].headers["Private-Token"], undefined);
398
- });
399
- test("an untrusted hop cannot re-attach credentials via a second trusted host", async () => {
400
- const calls = [];
401
- await fetchWithValidatedRedirects("https://gitlab.a/start", {
402
- headers: credentials,
403
- isTrustedRedirectHost: host => host === "gitlab.a" || host === "gitlab.b",
404
- ...sameClientForEveryHop(stubRedirectChain([`https://${PUBLIC_HOST}/evil`, "https://gitlab.b/api/v4/user"], calls)),
405
- });
406
- assert.equal(calls[0].headers["Private-Token"], "secret");
407
- assert.equal(calls[1].headers["Private-Token"], undefined);
408
- assert.equal(calls[2].headers.Authorization, undefined);
409
- assert.equal(calls[2].headers["Private-Token"], undefined);
410
- });
411
- test("an untrusted hop cannot re-attach credentials via a scheme change on the origin host", async () => {
412
- const calls = [];
413
- await fetchWithValidatedRedirects("http://gitlab.a/start", {
414
- headers: credentials,
415
- isTrustedRedirectHost: host => host === "gitlab.a",
416
- ...sameClientForEveryHop(stubRedirectChain([`http://${PUBLIC_HOST}/evil`, "https://gitlab.a/api/v4/user"], calls)),
417
- });
418
- assert.equal(calls[1].headers["Private-Token"], undefined);
419
- assert.equal(calls[2].headers.Authorization, undefined);
420
- assert.equal(calls[2].headers["Private-Token"], undefined);
421
- });
422
- test("uses the unauthenticated client once the credentials are withheld", async () => {
423
- const authenticatedCalls = [];
424
- const unauthenticatedCalls = [];
425
- await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
426
- headers: credentials,
427
- // A client that can add credentials by itself (cookie jar, OAuth 401 retry) must
428
- // not see a hop the helper stripped.
429
- fetchImpl: stubRedirectChain([`http://${PUBLIC_HOST}/mid`, "http://127.0.0.1:1/file"], authenticatedCalls),
430
- // This stub answers from its own first call on, which is the untrusted hop.
431
- unauthenticatedFetchImpl: stubRedirectChain(["http://127.0.0.1:1/file"], unauthenticatedCalls),
432
- });
433
- assert.deepEqual(authenticatedCalls.map(call => call.url), ["http://127.0.0.1:1/start"]);
434
- assert.deepEqual(unauthenticatedCalls.map(call => call.url), [`http://${PUBLIC_HOST}/mid`, "http://127.0.0.1:1/file"]);
435
- assert.equal(unauthenticatedCalls[0].headers["Private-Token"], undefined);
436
- // ...including the hop back to the origin, which the caller reads as the download.
437
- assert.equal(unauthenticatedCalls[1].headers["Private-Token"], undefined);
438
- });
439
- test("falls back to the plain client for stripped hops when only fetchImpl is given", async () => {
440
- const calls = [];
441
- // `undici`'s own client is observable through the dispatcher, so this asserts the
442
- // default rather than trusting it: were the stripped hop routed to `fetchImpl`, the
443
- // stub would answer and the mock pool would stay untouched.
444
- const mockAgent = new MockAgent();
445
- mockAgent.disableNetConnect();
446
- mockAgent
447
- .get(`http://${PUBLIC_HOST}`)
448
- .intercept({ path: "/file", method: "GET" })
449
- .reply(200, "from-plain-undici");
450
- try {
451
- const response = await fetchWithValidatedRedirects("http://127.0.0.1:1/start", {
452
- headers: credentials,
453
- dispatcher: mockAgent,
454
- fetchImpl: stubRedirectFetch(`http://${PUBLIC_HOST}/file`, calls),
455
- });
456
- assert.equal(await response.text(), "from-plain-undici");
457
- assert.deepEqual(calls.map(call => call.url), ["http://127.0.0.1:1/start"]);
458
- }
459
- finally {
460
- await mockAgent.close();
461
- }
462
- });
463
- });