|
| 1 | +import { createHmac } from "crypto"; |
| 2 | +import githubWebhookAuthenticator from "../../build/utils/auth-github-webhook.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Compute the SHA-1 HMAC signature GitHub sends in X-Hub-Signature. |
| 6 | + * @param {Buffer} body Raw request body |
| 7 | + * @param {string} secret Shared webhook secret |
| 8 | + * @returns {string} e.g. "sha1=abc123..." |
| 9 | + */ |
| 10 | +function sign(body, secret) { |
| 11 | + const hash = createHmac("sha1", secret).update(body).digest("hex"); |
| 12 | + return `sha1=${hash}`; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Create a minimal Express-like request object. |
| 17 | + * @param {object} options |
| 18 | + * @param {Buffer} options.body Raw body buffer |
| 19 | + * @param {Record<string, string>} [options.headers] Request headers |
| 20 | + * @returns {object} Mock request |
| 21 | + */ |
| 22 | +function mockReq({ body, headers = {} }) { |
| 23 | + const lowerHeaders = {}; |
| 24 | + for (const [k, v] of Object.entries(headers)) { |
| 25 | + lowerHeaders[k.toLowerCase()] = v; |
| 26 | + } |
| 27 | + return { |
| 28 | + body, |
| 29 | + headers: lowerHeaders, |
| 30 | + get(name) { |
| 31 | + return this.headers[name.toLowerCase()]; |
| 32 | + }, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Create a minimal Express-like response object. |
| 38 | + * @returns {{ statusCode: number|null, body: string|null, status: Function, send: Function }} |
| 39 | + */ |
| 40 | +function mockRes() { |
| 41 | + const res = { |
| 42 | + statusCode: null, |
| 43 | + body: null, |
| 44 | + status(code) { |
| 45 | + res.statusCode = code; |
| 46 | + return res; |
| 47 | + }, |
| 48 | + send(data) { |
| 49 | + res.body = data; |
| 50 | + return res; |
| 51 | + }, |
| 52 | + }; |
| 53 | + return res; |
| 54 | +} |
| 55 | + |
| 56 | +describe("utils/auth-github-webhook", () => { |
| 57 | + const SECRET = "test-webhook-secret-1234"; |
| 58 | + |
| 59 | + it("returns an array of [rawBodyParser, verifier]", () => { |
| 60 | + const middleware = githubWebhookAuthenticator(SECRET); |
| 61 | + expect(Array.isArray(middleware)).toBeTrue(); |
| 62 | + expect(middleware).toHaveSize(2); |
| 63 | + // First element is express.raw() middleware, second is the verifier fn |
| 64 | + expect(typeof middleware[0]).toBe("function"); |
| 65 | + expect(typeof middleware[1]).toBe("function"); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("signature verification (verifier middleware)", () => { |
| 69 | + // We test the second element of the array (the verifier function) |
| 70 | + // directly, since express.raw() is Express's own middleware. |
| 71 | + let verifier; |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + const middleware = githubWebhookAuthenticator(SECRET); |
| 75 | + verifier = middleware[1]; |
| 76 | + }); |
| 77 | + |
| 78 | + it("calls next() when the HMAC signature is valid", () => { |
| 79 | + const payload = { action: "opened", number: 42 }; |
| 80 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 81 | + const signature = sign(rawBody, SECRET); |
| 82 | + |
| 83 | + const req = mockReq({ |
| 84 | + body: rawBody, |
| 85 | + headers: { "X-Hub-Signature": signature }, |
| 86 | + }); |
| 87 | + const res = mockRes(); |
| 88 | + let nextCalled = false; |
| 89 | + const next = () => { |
| 90 | + nextCalled = true; |
| 91 | + }; |
| 92 | + |
| 93 | + verifier(req, res, next); |
| 94 | + |
| 95 | + expect(nextCalled).toBeTrue(); |
| 96 | + // Body should be parsed as JSON after verification |
| 97 | + expect(req.body).toEqual(payload); |
| 98 | + }); |
| 99 | + |
| 100 | + it("parses the raw body as JSON after successful verification", () => { |
| 101 | + const payload = { ref: "refs/heads/main", commits: [{ id: "abc" }] }; |
| 102 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 103 | + const signature = sign(rawBody, SECRET); |
| 104 | + |
| 105 | + const req = mockReq({ |
| 106 | + body: rawBody, |
| 107 | + headers: { "X-Hub-Signature": signature }, |
| 108 | + }); |
| 109 | + const res = mockRes(); |
| 110 | + verifier(req, res, () => {}); |
| 111 | + |
| 112 | + expect(req.body).toEqual(payload); |
| 113 | + expect(typeof req.body).toBe("object"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("responds with 'pong' for GitHub ping events (zen field present)", () => { |
| 117 | + const payload = { zen: "Keep it logically awesome.", hook_id: 1 }; |
| 118 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 119 | + const signature = sign(rawBody, SECRET); |
| 120 | + |
| 121 | + const req = mockReq({ |
| 122 | + body: rawBody, |
| 123 | + headers: { "X-Hub-Signature": signature }, |
| 124 | + }); |
| 125 | + const res = mockRes(); |
| 126 | + let nextCalled = false; |
| 127 | + |
| 128 | + verifier(req, res, () => { |
| 129 | + nextCalled = true; |
| 130 | + }); |
| 131 | + |
| 132 | + expect(nextCalled).toBeFalse(); |
| 133 | + expect(res.body).toBe("pong"); |
| 134 | + }); |
| 135 | + |
| 136 | + it("returns 401 when the signature is invalid", () => { |
| 137 | + const payload = { action: "opened" }; |
| 138 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 139 | + const badSignature = "sha1=0000000000000000000000000000000000000000"; |
| 140 | + |
| 141 | + const req = mockReq({ |
| 142 | + body: rawBody, |
| 143 | + headers: { "X-Hub-Signature": badSignature }, |
| 144 | + }); |
| 145 | + const res = mockRes(); |
| 146 | + let nextCalled = false; |
| 147 | + |
| 148 | + verifier(req, res, () => { |
| 149 | + nextCalled = true; |
| 150 | + }); |
| 151 | + |
| 152 | + expect(nextCalled).toBeFalse(); |
| 153 | + expect(res.statusCode).toBe(401); |
| 154 | + expect(res.body).toContain("Failed to authenticate"); |
| 155 | + }); |
| 156 | + |
| 157 | + it("returns 401 when X-Hub-Signature header is missing", () => { |
| 158 | + const payload = { action: "opened" }; |
| 159 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 160 | + |
| 161 | + const req = mockReq({ body: rawBody, headers: {} }); |
| 162 | + const res = mockRes(); |
| 163 | + let nextCalled = false; |
| 164 | + |
| 165 | + verifier(req, res, () => { |
| 166 | + nextCalled = true; |
| 167 | + }); |
| 168 | + |
| 169 | + expect(nextCalled).toBeFalse(); |
| 170 | + expect(res.statusCode).toBe(401); |
| 171 | + }); |
| 172 | + |
| 173 | + it("returns 401 when the body has been tampered with", () => { |
| 174 | + const original = { action: "opened", number: 42 }; |
| 175 | + const rawOriginal = Buffer.from(JSON.stringify(original)); |
| 176 | + const signature = sign(rawOriginal, SECRET); |
| 177 | + |
| 178 | + // Tamper with the body after signing |
| 179 | + const tampered = { action: "opened", number: 99 }; |
| 180 | + const rawTampered = Buffer.from(JSON.stringify(tampered)); |
| 181 | + |
| 182 | + const req = mockReq({ |
| 183 | + body: rawTampered, |
| 184 | + headers: { "X-Hub-Signature": signature }, |
| 185 | + }); |
| 186 | + const res = mockRes(); |
| 187 | + let nextCalled = false; |
| 188 | + |
| 189 | + verifier(req, res, () => { |
| 190 | + nextCalled = true; |
| 191 | + }); |
| 192 | + |
| 193 | + expect(nextCalled).toBeFalse(); |
| 194 | + expect(res.statusCode).toBe(401); |
| 195 | + }); |
| 196 | + |
| 197 | + it("returns 401 when the secret is wrong", () => { |
| 198 | + const payload = { action: "opened" }; |
| 199 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 200 | + // Sign with a different secret |
| 201 | + const signature = sign(rawBody, "wrong-secret"); |
| 202 | + |
| 203 | + const req = mockReq({ |
| 204 | + body: rawBody, |
| 205 | + headers: { "X-Hub-Signature": signature }, |
| 206 | + }); |
| 207 | + const res = mockRes(); |
| 208 | + let nextCalled = false; |
| 209 | + |
| 210 | + verifier(req, res, () => { |
| 211 | + nextCalled = true; |
| 212 | + }); |
| 213 | + |
| 214 | + expect(nextCalled).toBeFalse(); |
| 215 | + expect(res.statusCode).toBe(401); |
| 216 | + }); |
| 217 | + |
| 218 | + it("rejects an empty signature header value", () => { |
| 219 | + const payload = { action: "opened" }; |
| 220 | + const rawBody = Buffer.from(JSON.stringify(payload)); |
| 221 | + |
| 222 | + const req = mockReq({ |
| 223 | + body: rawBody, |
| 224 | + headers: { "X-Hub-Signature": "" }, |
| 225 | + }); |
| 226 | + const res = mockRes(); |
| 227 | + let nextCalled = false; |
| 228 | + |
| 229 | + verifier(req, res, () => { |
| 230 | + nextCalled = true; |
| 231 | + }); |
| 232 | + |
| 233 | + expect(nextCalled).toBeFalse(); |
| 234 | + expect(res.statusCode).toBe(401); |
| 235 | + }); |
| 236 | + }); |
| 237 | +}); |
0 commit comments