|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const { mockedAxiosGet } = vi.hoisted(() => ({ |
| 4 | + mockedAxiosGet: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock("axios", () => ({ |
| 8 | + default: { |
| 9 | + get: mockedAxiosGet, |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +interface MockResponseState { |
| 14 | + headers: Record<string, string | number>; |
| 15 | + jsonBody: unknown; |
| 16 | + sentBody: unknown; |
| 17 | + statusCode: number; |
| 18 | +} |
| 19 | + |
| 20 | +const originalEnv = { ...process.env }; |
| 21 | +const baseEnv = Object.fromEntries( |
| 22 | + Object.entries(originalEnv).filter( |
| 23 | + ([key]) => !key.startsWith("GITHUB_PAT") && !key.startsWith("VITE_GITHUB_PAT"), |
| 24 | + ), |
| 25 | +); |
| 26 | + |
| 27 | +const createMockRes = (): { |
| 28 | + res: { |
| 29 | + status: (code: number) => unknown; |
| 30 | + json: (data: unknown) => unknown; |
| 31 | + send: (data: unknown) => unknown; |
| 32 | + setHeader: (name: string, value: string | number) => unknown; |
| 33 | + }; |
| 34 | + state: MockResponseState; |
| 35 | +} => { |
| 36 | + const state: MockResponseState = { |
| 37 | + headers: {}, |
| 38 | + jsonBody: null, |
| 39 | + sentBody: null, |
| 40 | + statusCode: 200, |
| 41 | + }; |
| 42 | + |
| 43 | + const res = { |
| 44 | + status(code: number) { |
| 45 | + state.statusCode = code; |
| 46 | + return res; |
| 47 | + }, |
| 48 | + json(data: unknown) { |
| 49 | + state.jsonBody = data; |
| 50 | + return res; |
| 51 | + }, |
| 52 | + send(data: unknown) { |
| 53 | + state.sentBody = data; |
| 54 | + return res; |
| 55 | + }, |
| 56 | + setHeader(name: string, value: string | number) { |
| 57 | + state.headers[name] = value; |
| 58 | + return res; |
| 59 | + }, |
| 60 | + }; |
| 61 | + |
| 62 | + return { res, state }; |
| 63 | +}; |
| 64 | + |
| 65 | +const loadHandler = async (): Promise<(req: unknown, res: unknown) => Promise<void>> => { |
| 66 | + vi.resetModules(); |
| 67 | + const mod = await import("./github"); |
| 68 | + return mod.default as (req: unknown, res: unknown) => Promise<void>; |
| 69 | +}; |
| 70 | + |
| 71 | +describe("api/github handler security hardening", () => { |
| 72 | + beforeEach(() => { |
| 73 | + mockedAxiosGet.mockReset(); |
| 74 | + process.env = { |
| 75 | + ...baseEnv, |
| 76 | + GITHUB_REPO_OWNER: "test-owner", |
| 77 | + GITHUB_REPO_NAME: "test-repo", |
| 78 | + GITHUB_REPO_BRANCH: "main", |
| 79 | + GITHUB_PAT1: "", |
| 80 | + }; |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + process.env = { ...originalEnv }; |
| 85 | + }); |
| 86 | + |
| 87 | + it("rejects deprecated getFileContent url parameter", async () => { |
| 88 | + const handler = await loadHandler(); |
| 89 | + const { res, state } = createMockRes(); |
| 90 | + |
| 91 | + await handler( |
| 92 | + { |
| 93 | + query: { |
| 94 | + action: "getFileContent", |
| 95 | + url: "https://example.com/test.txt", |
| 96 | + }, |
| 97 | + }, |
| 98 | + res, |
| 99 | + ); |
| 100 | + |
| 101 | + expect(state.statusCode).toBe(400); |
| 102 | + expect(state.jsonBody).toEqual({ |
| 103 | + error: "The url parameter is deprecated. Use path and optional branch instead.", |
| 104 | + }); |
| 105 | + expect(mockedAxiosGet).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("rejects getFileContent without path", async () => { |
| 109 | + const handler = await loadHandler(); |
| 110 | + const { res, state } = createMockRes(); |
| 111 | + |
| 112 | + await handler( |
| 113 | + { |
| 114 | + query: { |
| 115 | + action: "getFileContent", |
| 116 | + }, |
| 117 | + }, |
| 118 | + res, |
| 119 | + ); |
| 120 | + |
| 121 | + expect(state.statusCode).toBe(400); |
| 122 | + expect(state.jsonBody).toEqual({ error: "Missing path parameter" }); |
| 123 | + expect(mockedAxiosGet).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("fetches repo files with composed raw URL and auth header", async () => { |
| 127 | + process.env.GITHUB_PAT1 = "secret-token"; |
| 128 | + const handler = await loadHandler(); |
| 129 | + const { res, state } = createMockRes(); |
| 130 | + |
| 131 | + mockedAxiosGet.mockResolvedValueOnce({ |
| 132 | + data: new Uint8Array([65, 66, 67]).buffer, |
| 133 | + headers: { |
| 134 | + "content-type": "text/plain; charset=utf-8", |
| 135 | + }, |
| 136 | + } as never); |
| 137 | + |
| 138 | + await handler( |
| 139 | + { |
| 140 | + query: { |
| 141 | + action: "getFileContent", |
| 142 | + path: "docs/readme.md", |
| 143 | + branch: "main", |
| 144 | + }, |
| 145 | + }, |
| 146 | + res, |
| 147 | + ); |
| 148 | + |
| 149 | + expect(mockedAxiosGet).toHaveBeenCalledTimes(1); |
| 150 | + const [calledUrl, calledConfig] = mockedAxiosGet.mock.calls[0] ?? []; |
| 151 | + expect(calledUrl).toBe( |
| 152 | + "https://raw.githubusercontent.com/test-owner/test-repo/main/docs/readme.md", |
| 153 | + ); |
| 154 | + expect(calledConfig?.maxRedirects).toBe(0); |
| 155 | + expect(calledConfig?.headers?.Authorization).toBe("token secret-token"); |
| 156 | + expect(state.statusCode).toBe(200); |
| 157 | + expect(Buffer.isBuffer(state.sentBody)).toBe(true); |
| 158 | + }); |
| 159 | + |
| 160 | + it("rejects getGitHubAsset non-https url", async () => { |
| 161 | + const handler = await loadHandler(); |
| 162 | + const { res, state } = createMockRes(); |
| 163 | + |
| 164 | + await handler( |
| 165 | + { |
| 166 | + query: { |
| 167 | + action: "getGitHubAsset", |
| 168 | + url: "http://raw.githubusercontent.com/test-owner/test-repo/main/a.md", |
| 169 | + }, |
| 170 | + }, |
| 171 | + res, |
| 172 | + ); |
| 173 | + |
| 174 | + expect(state.statusCode).toBe(400); |
| 175 | + expect(state.jsonBody).toEqual({ error: "Only https protocol is allowed" }); |
| 176 | + expect(mockedAxiosGet).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | + |
| 179 | + it("rejects getGitHubAsset non-allowlisted host", async () => { |
| 180 | + const handler = await loadHandler(); |
| 181 | + const { res, state } = createMockRes(); |
| 182 | + |
| 183 | + await handler( |
| 184 | + { |
| 185 | + query: { |
| 186 | + action: "getGitHubAsset", |
| 187 | + url: "https://example.com/assets/a.png", |
| 188 | + }, |
| 189 | + }, |
| 190 | + res, |
| 191 | + ); |
| 192 | + |
| 193 | + expect(state.statusCode).toBe(400); |
| 194 | + expect(state.jsonBody).toEqual({ error: "Host is not allowed" }); |
| 195 | + expect(mockedAxiosGet).not.toHaveBeenCalled(); |
| 196 | + }); |
| 197 | + |
| 198 | + it("fetches allowlisted GitHub assets without Authorization", async () => { |
| 199 | + process.env.GITHUB_PAT1 = "secret-token"; |
| 200 | + const handler = await loadHandler(); |
| 201 | + const { res, state } = createMockRes(); |
| 202 | + |
| 203 | + mockedAxiosGet.mockResolvedValueOnce({ |
| 204 | + data: new Uint8Array([1, 2, 3]).buffer, |
| 205 | + headers: { |
| 206 | + "content-type": "image/png", |
| 207 | + }, |
| 208 | + } as never); |
| 209 | + |
| 210 | + await handler( |
| 211 | + { |
| 212 | + query: { |
| 213 | + action: "getGitHubAsset", |
| 214 | + url: "https://user-images.githubusercontent.com/123/abc.png", |
| 215 | + }, |
| 216 | + }, |
| 217 | + res, |
| 218 | + ); |
| 219 | + |
| 220 | + expect(mockedAxiosGet).toHaveBeenCalledTimes(1); |
| 221 | + const [, calledConfig] = mockedAxiosGet.mock.calls[0] ?? []; |
| 222 | + expect(calledConfig?.maxRedirects).toBe(0); |
| 223 | + expect(calledConfig?.headers?.Authorization).toBeUndefined(); |
| 224 | + expect(state.statusCode).toBe(200); |
| 225 | + }); |
| 226 | + |
| 227 | + it("does not follow getGitHubAsset redirects", async () => { |
| 228 | + const handler = await loadHandler(); |
| 229 | + const { res, state } = createMockRes(); |
| 230 | + |
| 231 | + mockedAxiosGet.mockRejectedValueOnce({ |
| 232 | + response: { |
| 233 | + status: 302, |
| 234 | + }, |
| 235 | + message: "Found", |
| 236 | + }); |
| 237 | + |
| 238 | + await handler( |
| 239 | + { |
| 240 | + query: { |
| 241 | + action: "getGitHubAsset", |
| 242 | + url: "https://raw.githubusercontent.com/test-owner/test-repo/main/file.png", |
| 243 | + }, |
| 244 | + }, |
| 245 | + res, |
| 246 | + ); |
| 247 | + |
| 248 | + expect(state.statusCode).toBe(302); |
| 249 | + expect(state.jsonBody).toEqual({ error: "Failed to fetch GitHub asset" }); |
| 250 | + }); |
| 251 | +}); |
0 commit comments