|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as Layer from "effect/Layer"; |
| 4 | +import { |
| 5 | + FetchHttpClient, |
| 6 | + HttpClient, |
| 7 | + HttpClientResponse, |
| 8 | + HttpServerResponse, |
| 9 | +} from "effect/unstable/http"; |
| 10 | +import { ChildProcessSpawner } from "effect/unstable/process"; |
| 11 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 12 | +import type { GitLabUploadReference } from "@t3tools/contracts"; |
| 13 | +import * as GitLabUploadMedia from "./GitLabUploadMedia.ts"; |
| 14 | +import * as GitLabCli from "../sourceControl/GitLabCli.ts"; |
| 15 | +import * as ServerConfig from "../config.ts"; |
| 16 | + |
| 17 | +const reference: GitLabUploadReference = { |
| 18 | + origin: "http://gl.here", |
| 19 | + project: "team/project", |
| 20 | + secret: "66dbcd21ec5d24ed6ea225176098d52b", |
| 21 | + fileName: "my clip.mp4", |
| 22 | +}; |
| 23 | +const output = (stdout: string) => ({ |
| 24 | + stdout, |
| 25 | + stderr: "", |
| 26 | + stdoutTruncated: false, |
| 27 | + stderrTruncated: false, |
| 28 | + exitCode: ChildProcessSpawner.ExitCode(0), |
| 29 | +}); |
| 30 | + |
| 31 | +function fixture( |
| 32 | + options: { |
| 33 | + protocol?: string; |
| 34 | + endpoint?: string; |
| 35 | + token?: () => string; |
| 36 | + response?: ( |
| 37 | + request: { url: string; method: string; headers: Readonly<Record<string, string>> }, |
| 38 | + index: number, |
| 39 | + ) => Response; |
| 40 | + } = {}, |
| 41 | +) { |
| 42 | + const commands: ReadonlyArray<string>[] = []; |
| 43 | + const requests: { url: string; method: string; headers: Readonly<Record<string, string>> }[] = []; |
| 44 | + const client = HttpClient.make((request) => { |
| 45 | + requests.push(request); |
| 46 | + return Effect.succeed( |
| 47 | + HttpClientResponse.fromWeb( |
| 48 | + request, |
| 49 | + options.response?.(request, requests.length - 1) ?? |
| 50 | + new Response("video-bytes", { |
| 51 | + headers: { |
| 52 | + "content-type": "application/octet-stream", |
| 53 | + "content-length": "11", |
| 54 | + "accept-ranges": "bytes", |
| 55 | + }, |
| 56 | + }), |
| 57 | + ), |
| 58 | + ); |
| 59 | + }); |
| 60 | + const layer = GitLabUploadMedia.layer.pipe( |
| 61 | + Layer.provide( |
| 62 | + Layer.mock(GitLabCli.GitLabCli)({ |
| 63 | + execute: ({ args }) => { |
| 64 | + commands.push(args); |
| 65 | + return Effect.succeed( |
| 66 | + output( |
| 67 | + args[0] === "config" |
| 68 | + ? (options.protocol ?? "https") |
| 69 | + : `REST API Endpoint: ${options.endpoint ?? "https://api.example/api/v4/"}\nToken found in config file: ${options.token?.() ?? "private-credential"}`, |
| 70 | + ), |
| 71 | + ); |
| 72 | + }, |
| 73 | + }), |
| 74 | + ), |
| 75 | + Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), |
| 76 | + Layer.provide( |
| 77 | + ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "t3-gitlab-media-test-" }), |
| 78 | + ), |
| 79 | + Layer.provide(NodeServices.layer), |
| 80 | + ); |
| 81 | + return { layer, commands, requests }; |
| 82 | +} |
| 83 | + |
| 84 | +describe("GitLabUploadMedia", () => { |
| 85 | + it.effect("streams decoded upload names through the HTTPS API and reuses the credential", () => { |
| 86 | + const f = fixture(); |
| 87 | + return Effect.gen(function* () { |
| 88 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 89 | + const response = HttpServerResponse.toWeb(yield* service.respond(reference, {}, "GET")); |
| 90 | + expect(yield* Effect.promise(() => response.text())).toBe("video-bytes"); |
| 91 | + expect(response.headers.get("content-type")).toBe("video/mp4"); |
| 92 | + expect(response.headers.get("cache-control")).toBe("private, no-store"); |
| 93 | + yield* service.respond(reference, {}, "GET"); |
| 94 | + expect(f.commands).toHaveLength(2); |
| 95 | + expect(f.commands[1]).toEqual(["auth", "status", "--hostname", "gl.here", "--show-token"]); |
| 96 | + expect(f.requests[0]?.url).toBe( |
| 97 | + `https://api.example/api/v4/projects/team%2Fproject/uploads/${reference.secret}/my%20clip.mp4`, |
| 98 | + ); |
| 99 | + expect(f.requests[0]?.headers["private-token"]).toBe("private-credential"); |
| 100 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 101 | + }); |
| 102 | + |
| 103 | + it.effect("rejects plaintext configuration before auth status can transmit a credential", () => { |
| 104 | + const f = fixture({ protocol: "http" }); |
| 105 | + return Effect.gen(function* () { |
| 106 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 107 | + expect((yield* service.respond(reference, {}, "GET")).status).toBe(502); |
| 108 | + expect(f.commands).toHaveLength(1); |
| 109 | + expect(f.requests).toHaveLength(0); |
| 110 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 111 | + }); |
| 112 | + |
| 113 | + it.effect("rejects a plaintext API endpoint even when the configured protocol is HTTPS", () => { |
| 114 | + const f = fixture({ endpoint: "http://api.example/api/v4/" }); |
| 115 | + return Effect.gen(function* () { |
| 116 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 117 | + expect((yield* service.respond(reference, {}, "GET")).status).toBe(502); |
| 118 | + expect(f.requests).toHaveLength(0); |
| 119 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 120 | + }); |
| 121 | + |
| 122 | + it.effect("does not cache a missing credential after login", () => { |
| 123 | + let token = ""; |
| 124 | + const f = fixture({ token: () => token }); |
| 125 | + return Effect.gen(function* () { |
| 126 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 127 | + expect((yield* service.respond(reference, {}, "GET")).status).toBe(502); |
| 128 | + token = "signed-in"; |
| 129 | + expect((yield* service.respond(reference, {}, "GET")).status).toBe(200); |
| 130 | + expect(f.commands).toHaveLength(4); |
| 131 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 132 | + }); |
| 133 | + |
| 134 | + it.effect("keeps credentials off storage redirects, including a later hop back", () => { |
| 135 | + const f = fixture({ |
| 136 | + response: (_, index) => |
| 137 | + index < 2 |
| 138 | + ? new Response(null, { |
| 139 | + status: 302, |
| 140 | + headers: { |
| 141 | + location: |
| 142 | + index === 0 ? "https://storage.example/signed" : "https://api.example/file", |
| 143 | + }, |
| 144 | + }) |
| 145 | + : new Response("png", { headers: { "content-type": "image/png" } }), |
| 146 | + }); |
| 147 | + return Effect.gen(function* () { |
| 148 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 149 | + expect((yield* service.respond(reference, {}, "GET")).status).toBe(200); |
| 150 | + expect(f.requests.map((r) => r.headers["private-token"])).toEqual([ |
| 151 | + "private-credential", |
| 152 | + undefined, |
| 153 | + undefined, |
| 154 | + ]); |
| 155 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 156 | + }); |
| 157 | + |
| 158 | + it.effect("forwards ranges and if-range and serves HEAD without a body", () => { |
| 159 | + const f = fixture({ |
| 160 | + response: ({ method }) => |
| 161 | + new Response(method === "HEAD" ? null : "part", { |
| 162 | + status: method === "HEAD" ? 200 : 206, |
| 163 | + headers: { |
| 164 | + "content-type": "video/mp4", |
| 165 | + "content-range": "bytes 2-5/11", |
| 166 | + "content-length": "4", |
| 167 | + }, |
| 168 | + }), |
| 169 | + }); |
| 170 | + return Effect.gen(function* () { |
| 171 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 172 | + const response = HttpServerResponse.toWeb( |
| 173 | + yield* service.respond(reference, { range: "bytes=2-5", "if-range": '"etag"' }, "GET"), |
| 174 | + ); |
| 175 | + expect(response.status).toBe(206); |
| 176 | + expect(response.headers.get("content-range")).toBe("bytes 2-5/11"); |
| 177 | + expect(response.headers.get("cache-control")).toBe("private, no-store"); |
| 178 | + expect(yield* Effect.promise(() => response.text())).toBe("part"); |
| 179 | + expect(f.requests[0]?.headers.range).toBe("bytes=2-5"); |
| 180 | + expect(f.requests[0]?.headers["if-range"]).toBe('"etag"'); |
| 181 | + const head = HttpServerResponse.toWeb(yield* service.respond(reference, {}, "HEAD")); |
| 182 | + expect(f.requests[1]?.method).toBe("HEAD"); |
| 183 | + expect(yield* Effect.promise(() => head.text())).toBe(""); |
| 184 | + expect(head.headers.get("cache-control")).toBe("private, no-store"); |
| 185 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 186 | + }); |
| 187 | + |
| 188 | + it.effect("uses project IDs verbatim and removes the configured installation subfolder", () => { |
| 189 | + const f = fixture({ endpoint: "https://api.example/gitlab/api/v4/" }); |
| 190 | + return Effect.gen(function* () { |
| 191 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 192 | + yield* service.respond({ ...reference, project: "gitlab/team/project" }, {}, "GET"); |
| 193 | + yield* service.respond({ ...reference, project: "42" }, {}, "GET"); |
| 194 | + expect(f.requests[0]?.url).toContain("/gitlab/api/v4/projects/team%2Fproject/uploads/"); |
| 195 | + expect(f.requests[1]?.url).toContain("/gitlab/api/v4/projects/42/uploads/"); |
| 196 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 197 | + }); |
| 198 | + |
| 199 | + it.effect("omits unsafe lengths and protects SVG responses", () => { |
| 200 | + const f = fixture({ |
| 201 | + response: () => |
| 202 | + new Response("<svg/>", { |
| 203 | + headers: { "content-type": "image/svg+xml", "content-length": "9007199254740992" }, |
| 204 | + }), |
| 205 | + }); |
| 206 | + return Effect.gen(function* () { |
| 207 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 208 | + const response = yield* service.respond(reference, {}, "GET"); |
| 209 | + expect(response.headers["content-length"]).toBeUndefined(); |
| 210 | + expect(response.headers["content-security-policy"]).toContain("sandbox"); |
| 211 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 212 | + }); |
| 213 | + |
| 214 | + it.effect("rejects redirect loops, insecure targets and non-media responses", () => { |
| 215 | + return Effect.forEach( |
| 216 | + [ |
| 217 | + { status: 302, headers: { location: "http://api.example/file" } }, |
| 218 | + { status: 302, headers: { location: "https://user:[email protected]/file" } }, |
| 219 | + { status: 302, headers: { location: "https://api.example/loop" } }, |
| 220 | + { status: 302, headers: { location: "https://[" } }, |
| 221 | + { status: 200, headers: { "content-type": "text/html" } }, |
| 222 | + { status: 206, headers: { "content-type": "video/mp4" } }, |
| 223 | + { |
| 224 | + status: 206, |
| 225 | + headers: { |
| 226 | + "content-type": "video/mp4", |
| 227 | + "content-range": "bytes 0-2/3", |
| 228 | + "content-encoding": "gzip", |
| 229 | + }, |
| 230 | + }, |
| 231 | + ], |
| 232 | + (response) => { |
| 233 | + const f = fixture({ response: () => new Response("bad", response) }); |
| 234 | + return Effect.gen(function* () { |
| 235 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 236 | + const result = yield* service.respond(reference, {}, "GET"); |
| 237 | + expect(result.status).toBe(502); |
| 238 | + expect(result.headers["cache-control"]).toBe("private, no-store"); |
| 239 | + expect(f.requests.length).toBeLessThanOrEqual(5); |
| 240 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 241 | + }, |
| 242 | + ); |
| 243 | + }); |
| 244 | + |
| 245 | + it.effect("preserves unsatisfiable ranges without forwarding error bodies", () => { |
| 246 | + const f = fixture({ |
| 247 | + response: () => |
| 248 | + new Response("private upstream error", { |
| 249 | + status: 416, |
| 250 | + headers: { "content-range": "bytes */11", "content-length": "22" }, |
| 251 | + }), |
| 252 | + }); |
| 253 | + return Effect.gen(function* () { |
| 254 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 255 | + const response = HttpServerResponse.toWeb( |
| 256 | + yield* service.respond(reference, { range: "bytes=100-" }, "GET"), |
| 257 | + ); |
| 258 | + expect(response.status).toBe(416); |
| 259 | + expect(response.headers.get("content-range")).toBe("bytes */11"); |
| 260 | + expect(yield* Effect.promise(() => response.text())).toBe(""); |
| 261 | + }).pipe(Effect.provide(f.layer), Effect.scoped); |
| 262 | + }); |
| 263 | + |
| 264 | + it.effect("cancels the upstream transfer when the caller closes its scope", () => { |
| 265 | + let signal: AbortSignal | null | undefined; |
| 266 | + return Effect.gen(function* () { |
| 267 | + yield* Effect.gen(function* () { |
| 268 | + const service = yield* GitLabUploadMedia.GitLabUploadMedia; |
| 269 | + yield* service.respond(reference, {}, "GET"); |
| 270 | + expect(signal?.aborted).toBe(false); |
| 271 | + }).pipe(Effect.scoped); |
| 272 | + expect(signal?.aborted).toBe(true); |
| 273 | + }).pipe( |
| 274 | + Effect.provide( |
| 275 | + GitLabUploadMedia.layer.pipe( |
| 276 | + Layer.provide( |
| 277 | + Layer.mock(GitLabCli.GitLabCli)({ |
| 278 | + execute: ({ args }) => |
| 279 | + Effect.succeed( |
| 280 | + output( |
| 281 | + args[0] === "config" |
| 282 | + ? "https" |
| 283 | + : "REST API Endpoint: https://api.example/api/v4/\nToken found in config file: token", |
| 284 | + ), |
| 285 | + ), |
| 286 | + }), |
| 287 | + ), |
| 288 | + Layer.provide(FetchHttpClient.layer), |
| 289 | + Layer.provide( |
| 290 | + Layer.succeed(FetchHttpClient.Fetch, (_url, init) => { |
| 291 | + signal = init?.signal; |
| 292 | + return Promise.resolve( |
| 293 | + new Response("bytes", { headers: { "content-type": "video/mp4" } }), |
| 294 | + ); |
| 295 | + }), |
| 296 | + ), |
| 297 | + Layer.provide( |
| 298 | + ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "t3-gitlab-cancel-" }), |
| 299 | + ), |
| 300 | + Layer.provide(NodeServices.layer), |
| 301 | + ), |
| 302 | + ), |
| 303 | + ); |
| 304 | + }); |
| 305 | +}); |
0 commit comments