Skip to content

Commit 30b2044

Browse files
committed
feat: remove SHA1 support
BREAKING CHANGE: remove SHA1 support
1 parent 815352d commit 30b2044

File tree

7 files changed

+6
-65
lines changed

7 files changed

+6
-65
lines changed

src/node/sign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function sign(
3131

3232
if (!Object.values(Algorithm).includes(algorithm as Algorithm)) {
3333
throw new TypeError(
34-
`[@octokit/webhooks] Algorithm ${algorithm} is not supported. Must be 'sha1' or 'sha256'`,
34+
`[@octokit/webhooks] Algorithm ${algorithm} is not supported. Must be 'sha256'`,
3535
);
3636
}
3737

src/node/verify.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Buffer } from "node:buffer";
33

44
import { sign } from "./sign.js";
55
import { VERSION } from "../version.js";
6-
import { getAlgorithm } from "../utils.js";
76

87
export async function verify(
98
secret: string,
@@ -23,7 +22,7 @@ export async function verify(
2322
}
2423

2524
const signatureBuffer = Buffer.from(signature);
26-
const algorithm = getAlgorithm(signature);
25+
const algorithm = "sha256";
2726

2827
const verificationBuffer = Buffer.from(
2928
await sign({ secret, algorithm }, eventPayload),

src/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
export enum Algorithm {
2-
SHA1 = "sha1",
32
SHA256 = "sha256",
43
}
54

6-
export type AlgorithmLike = Algorithm | "sha1" | "sha256";
5+
export type AlgorithmLike = Algorithm | "sha256";
76

87
export type SignOptions = {
98
secret: string;

src/utils.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/web.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Algorithm, type AlgorithmLike, type SignOptions } from "./types.js";
2-
import { getAlgorithm } from "./utils.js";
32

43
const enc = new TextEncoder();
54

@@ -24,7 +23,6 @@ function UInt8ArrayToHex(signature: ArrayBuffer) {
2423
function getHMACHashName(algorithm: AlgorithmLike) {
2524
return (
2625
{
27-
[Algorithm.SHA1]: "SHA-1",
2826
[Algorithm.SHA256]: "SHA-256",
2927
} as { [key in Algorithm]: string }
3028
)[algorithm];
@@ -71,7 +69,7 @@ export async function sign(options: SignOptions | string, payload: string) {
7169

7270
if (!Object.values(Algorithm).includes(algorithm as Algorithm)) {
7371
throw new TypeError(
74-
`[@octokit/webhooks] Algorithm ${algorithm} is not supported. Must be 'sha1' or 'sha256'`,
72+
`[@octokit/webhooks] Algorithm ${algorithm} is not supported. Must be 'sha256'`,
7573
);
7674
}
7775

@@ -101,7 +99,7 @@ export async function verify(
10199
);
102100
}
103101

104-
const algorithm = getAlgorithm(signature);
102+
const algorithm = "sha256";
105103
return await crypto.subtle.verify(
106104
"HMAC",
107105
await importKey(secret, algorithm),

test/sign.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("sign", () => {
4040
// @ts-expect-error
4141
sign({ secret, algorithm: "sha2" }, JSON.stringify(eventPayload)),
4242
).rejects.toThrow(
43-
"[@octokit/webhooks] Algorithm sha2 is not supported. Must be 'sha1' or 'sha256'",
43+
"[@octokit/webhooks] Algorithm sha2 is not supported. Must be 'sha256'",
4444
);
4545
});
4646

@@ -59,14 +59,6 @@ describe("sign", () => {
5959
"sha256=4864d2759938a15468b5df9ade20bf161da9b4f737ea61794142f3484236bda3",
6060
);
6161
});
62-
63-
test("sign({secret, algorithm: 'sha1'}, eventPayload)", async () => {
64-
const signature = await sign(
65-
{ secret, algorithm: "sha1" },
66-
JSON.stringify(eventPayload),
67-
);
68-
expect(signature).toBe("sha1=d03207e4b030cf234e3447bac4d93add4c6643d8");
69-
});
7062
});
7163

7264
describe("returns expected sha256 signature", () => {

test/verify.test.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ function toNormalizedJsonString(payload: object) {
1111
const JSONeventPayload = { foo: "bar" };
1212
const eventPayload = toNormalizedJsonString(JSONeventPayload);
1313
const secret = "mysecret";
14-
const signatureSHA1 = "sha1=640c0ea7402a3f74e1767338fa2dba243b1f2d9c";
1514
const signatureSHA256 =
1615
"sha256=e3eccac34c43c7dc1cbb905488b1b81347fcc700a7b025697a9d07862256023f";
1716

@@ -52,49 +51,6 @@ describe("verify", () => {
5251
);
5352
});
5453

55-
test("verify(secret, eventPayload, signatureSHA1) returns true for correct signature", async () => {
56-
const signatureMatches = await verify(secret, eventPayload, signatureSHA1);
57-
expect(signatureMatches).toBe(true);
58-
});
59-
60-
test("verify(secret, eventPayload, signatureSHA1) returns false for incorrect signature", async () => {
61-
const signatureMatches = await verify(secret, eventPayload, "foo");
62-
expect(signatureMatches).toBe(false);
63-
});
64-
65-
test("verify(secret, eventPayload, signatureSHA1) returns false for correct secret", async () => {
66-
const signatureMatches = await verify("foo", eventPayload, signatureSHA1);
67-
expect(signatureMatches).toBe(false);
68-
});
69-
70-
test("verify(secret, eventPayload, signatureSHA1) returns true if eventPayload contains special characters (#71)", async () => {
71-
// https://github.com/octokit/webhooks.js/issues/71
72-
const signatureMatchesLowerCaseSequence = await verify(
73-
"development",
74-
toNormalizedJsonString({
75-
foo: "Foo\n\u001b[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001b[0m\u001b[2K",
76-
}),
77-
"sha1=82a91c5aacc9cdc2eea893bc828bd03d218df79c",
78-
);
79-
expect(signatureMatchesLowerCaseSequence).toBe(true);
80-
const signatureMatchesUpperCaseSequence = await verify(
81-
"development",
82-
toNormalizedJsonString({
83-
foo: "Foo\n\u001B[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001B[0m\u001B[2K",
84-
}),
85-
"sha1=82a91c5aacc9cdc2eea893bc828bd03d218df79c",
86-
);
87-
expect(signatureMatchesUpperCaseSequence).toBe(true);
88-
const signatureMatchesEscapedSequence = await verify(
89-
"development",
90-
toNormalizedJsonString({
91-
foo: "\\u001b",
92-
}),
93-
"sha1=bdae4705bdd827d026bb227817ca025b5b3a6756",
94-
);
95-
expect(signatureMatchesEscapedSequence).toBe(true);
96-
});
97-
9854
test("verify(secret, eventPayload, signatureSHA256) returns true for correct signature", async () => {
9955
const signatureMatches = await verify(
10056
secret,

0 commit comments

Comments
 (0)