Skip to content

Commit e2bcb2c

Browse files
authored
feat: throw an error when passing an object payload to verify or sign (#235)
1 parent 44d625a commit e2bcb2c

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

src/node/sign.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export async function sign(
2020
);
2121
}
2222

23+
if (typeof payload !== "string") {
24+
throw new TypeError("[@octokit/webhooks-methods] payload must be a string");
25+
}
26+
2327
if (!Object.values(Algorithm).includes(algorithm as Algorithm)) {
2428
throw new TypeError(
2529
`[@octokit/webhooks] Algorithm ${algorithm} is not supported. Must be 'sha1' or 'sha256'`,

src/node/verify.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export async function verify(
1616
);
1717
}
1818

19+
if (typeof eventPayload !== "string") {
20+
throw new TypeError(
21+
"[@octokit/webhooks-methods] eventPayload must be a string",
22+
);
23+
}
24+
1925
const signatureBuffer = Buffer.from(signature);
2026
const algorithm = getAlgorithm(signature);
2127

src/web.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export async function sign(options: SignOptions | string, payload: string) {
6060
);
6161
}
6262

63+
if (typeof payload !== "string") {
64+
throw new TypeError("[@octokit/webhooks-methods] payload must be a string");
65+
}
66+
6367
if (!Object.values(Algorithm).includes(algorithm as Algorithm)) {
6468
throw new TypeError(
6569
`[@octokit/webhooks] Algorithm ${algorithm} is not supported. Must be 'sha1' or 'sha256'`,
@@ -86,6 +90,12 @@ export async function verify(
8690
);
8791
}
8892

93+
if (typeof eventPayload !== "string") {
94+
throw new TypeError(
95+
"[@octokit/webhooks-methods] eventPayload must be a string",
96+
);
97+
}
98+
8999
const algorithm = getAlgorithm(signature);
90100
return await crypto.subtle.verify(
91101
"HMAC",

test/sign.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("sign", () => {
3838
test("sign({secret, algorithm}) throws with invalid algorithm", async () => {
3939
await expect(() =>
4040
// @ts-expect-error
41-
sign({ secret, algorithm: "sha2" }, eventPayload),
41+
sign({ secret, algorithm: "sha2" }, JSON.stringify(eventPayload)),
4242
).rejects.toThrow(
4343
"[@octokit/webhooks] Algorithm sha2 is not supported. Must be 'sha1' or 'sha256'",
4444
);
@@ -81,4 +81,11 @@ describe("sign", () => {
8181
});
8282
});
8383
});
84+
85+
test("throws with eventPayload as object", () => {
86+
// @ts-expect-error
87+
expect(() => sign(secret, eventPayload)).rejects.toThrow(
88+
"[@octokit/webhooks-methods] payload must be a string",
89+
);
90+
});
8491
});

test/verify.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ function toNormalizedJsonString(payload: object) {
88
});
99
}
1010

11-
const eventPayload = toNormalizedJsonString({ foo: "bar" });
11+
const JSONeventPayload = { foo: "bar" };
12+
const eventPayload = toNormalizedJsonString(JSONeventPayload);
1213
const secret = "mysecret";
1314
const signatureSHA1 = "sha1=640c0ea7402a3f74e1767338fa2dba243b1f2d9c";
1415
const signatureSHA256 =
@@ -140,6 +141,15 @@ describe("verify", () => {
140141
);
141142
expect(signatureMatchesEscapedSequence).toBe(true);
142143
});
144+
145+
test("verify(secret, eventPayload, signatureSHA256) with JSON eventPayload", async () => {
146+
await expect(() =>
147+
// @ts-expect-error
148+
verify(secret, JSONeventPayload, signatureSHA256),
149+
).rejects.toThrow(
150+
"[@octokit/webhooks-methods] eventPayload must be a string",
151+
);
152+
});
143153
});
144154

145155
describe("verifyWithFallback", () => {

0 commit comments

Comments
 (0)