Skip to content

Commit 855cefa

Browse files
authored
feat: only accept string payloads for webhooks.sign() (#794)
* feat: only accept string payloads for `webhooks.sign()` BREAKING CHANGE: Only accept string payloads for `webhooks.verify()`
1 parent 7cc4068 commit 855cefa

File tree

6 files changed

+4
-46
lines changed

6 files changed

+4
-46
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ webhooks.sign(eventPayload);
162162
eventPayload
163163
</code>
164164
<em>
165-
(Object)
165+
(String)
166166
</em>
167167
</td>
168168
<td>

src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createLogger } from "./createLogger";
22
import { createEventHandler } from "./event-handler/index";
3-
import { sign } from "./sign";
4-
import { verify } from "@octokit/webhooks-methods";
3+
import { sign, verify } from "@octokit/webhooks-methods";
54
import { verifyAndReceive } from "./verify-and-receive";
65
import {
76
EmitterWebhookEvent,
@@ -20,7 +19,7 @@ export { emitterEventNames } from "./generated/webhook-names";
2019

2120
// U holds the return value of `transform` function in Options
2221
class Webhooks<TTransformed = unknown> {
23-
public sign: (payload: string | object) => Promise<string>;
22+
public sign: (payload: string) => Promise<string>;
2423
public verify: (eventPayload: string, signature: string) => Promise<boolean>;
2524
public on: <E extends EmitterWebhookEventName>(
2625
event: E | E[],

src/sign.ts

-13
This file was deleted.

src/to-normalized-json-string.ts

-9
This file was deleted.

test/integration/webhooks.test.ts

-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { readFileSync } from "fs";
33
import { sign } from "@octokit/webhooks-methods";
44

55
import { Webhooks } from "../../src";
6-
import { toNormalizedJsonString } from "../../src/to-normalized-json-string";
76

87
const pushEventPayloadString = readFileSync(
98
"test/fixtures/push-payload.json",
@@ -18,31 +17,13 @@ describe("Webhooks", () => {
1817
);
1918
});
2019

21-
test("webhooks.sign(payload) with object payload", async () => {
22-
const secret = "mysecret";
23-
const webhooks = new Webhooks({ secret });
24-
25-
await webhooks.sign(JSON.parse(pushEventPayloadString));
26-
});
27-
2820
test("webhooks.sign(payload) with string payload", async () => {
2921
const secret = "mysecret";
3022
const webhooks = new Webhooks({ secret });
3123

3224
await webhooks.sign(pushEventPayloadString);
3325
});
3426

35-
test("webhooks.verify(payload, signature) with string payload containing special characters", async () => {
36-
const secret = "mysecret";
37-
const webhooks = new Webhooks({ secret });
38-
39-
const payload = toNormalizedJsonString({
40-
foo: "Foo\n\u001b[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001b[0m\u001b[2K",
41-
});
42-
43-
await webhooks.verify(payload, await sign(secret, payload));
44-
});
45-
4627
test("webhooks.verify(payload, signature) with string payload", async () => {
4728
const secret = "mysecret";
4829
const webhooks = new Webhooks({ secret });

test/typescript-validate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default async function () {
8282

8383
webhooks.onAny(async ({ id, name, payload }) => {
8484
console.log(name, "event received", id);
85-
const sig = await webhooks.sign(payload);
85+
const sig = await webhooks.sign(JSON.stringify(payload));
8686
webhooks.verify(JSON.stringify(payload), sig);
8787
});
8888

0 commit comments

Comments
 (0)