|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | 2 | import { pollCheckRuns } from "../../packages/gittensory-miner/lib/ci-poller.js"; |
3 | 3 |
|
4 | | -const API = "https://api.test"; |
| 4 | +const API = "https://api.github.com"; |
5 | 5 |
|
6 | 6 | function jsonResponse(body: unknown, init: ResponseInit = {}) { |
7 | 7 | return Response.json(body, init); |
@@ -68,6 +68,40 @@ describe("miner CI check-run poller (#2323)", () => { |
68 | 68 | ).toBe(true); |
69 | 69 | }); |
70 | 70 |
|
| 71 | + it("rejects untrusted apiBaseUrl values before any token-bearing request", async () => { |
| 72 | + const fetchFn = vi.fn(); |
| 73 | + for (const apiBaseUrl of [ |
| 74 | + "http://api.github.com", |
| 75 | + "https://evil.example", |
| 76 | + "https://api.github.com.evil.example", |
| 77 | + "not a url", |
| 78 | + ]) { |
| 79 | + await expect( |
| 80 | + pollCheckRuns("acme/widgets", 42, { |
| 81 | + apiBaseUrl, |
| 82 | + githubToken: "github-token", |
| 83 | + fetchFn, |
| 84 | + }), |
| 85 | + ).rejects.toThrow("invalid_api_base_url"); |
| 86 | + } |
| 87 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("uses the default GitHub API base URL when apiBaseUrl is omitted", async () => { |
| 91 | + const fetchFn = vi.fn(async (input: RequestInfo | URL) => { |
| 92 | + const url = String(input); |
| 93 | + if (url === "https://api.github.com/repos/acme/widgets/pulls/42") return prResponse("head-sha"); |
| 94 | + if (url === "https://api.github.com/repos/acme/widgets/commits/head-sha/check-runs?per_page=100&page=1") { |
| 95 | + return checksResponse([checkRun("validate", "completed", "success")]); |
| 96 | + } |
| 97 | + return jsonResponse({}, { status: 404 }); |
| 98 | + }); |
| 99 | + |
| 100 | + await expect(pollCheckRuns("acme/widgets", 42, { fetchFn })).resolves.toMatchObject({ |
| 101 | + conclusion: "success", |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
71 | 105 | it("follows paginated check-run responses before aggregating failures (regression for #2621)", async () => { |
72 | 106 | const pageOneChecks = Array.from({ length: 100 }, (_, index) => |
73 | 107 | checkRun(`success-${index}`, "completed", "success"), |
|
0 commit comments