Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verify with fallback #134

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [Methods](#methods)
- [`sign()`](#sign)
- [`verify()`](#verify)
- [`verifyWithFallback()`](#verifyWithFallback)
- [Contributing](#contributing)
- [License](#license)

Expand All @@ -40,6 +41,7 @@ Load `@octokit/webhooks-methods` directly from [cdn.skypack.dev](https://cdn.sky
import {
sign,
verify,
verifyWithFallback,
} from "https://cdn.skypack.dev/@octokit/webhooks-methods";
</script>
```
Expand All @@ -54,7 +56,11 @@ Node
Install with `npm install @octokit/core @octokit/webhooks-methods`

```js
const { sign, verify } = require("@octokit/webhooks-methods");
const {
sign,
verify,
verifyWithFallback,
} = require("@octokit/webhooks-methods");
```

</td></tr>
Expand All @@ -70,6 +76,9 @@ await sign({ secret: "mysecret", algorithm: "sha1" }, eventPayloadString);

await verify("mysecret", eventPayloadString, "sha256=486d27...");
// resolves with true or false

await verifyWithFallback("mysecret", eventPayloadString, "sha256=486d27...", ["oldsecret", ...]);
// resolves with true or false
```

## Methods
Expand Down Expand Up @@ -184,6 +193,78 @@ await verify(secret, eventPayloadString, signature);

Resolves with `true` or `false`. Throws error if an argument is missing.

### `verifyWithFallback()`

```js
await verifyWithFallback(
secret,
eventPayloadString,
signature,
additionalSecrets
);
```

<table width="100%">
<tr>
<td>
<code>
secret
</code>
<em>(String)</em>
</td>
<td>
<strong>Required.</strong>
Secret as configured in GitHub Settings.
</td>
</tr>
<tr>
<td>
<code>
eventPayloadString
</code>
<em>
(String)
</em>
</td>
<td>
<strong>Required.</strong>
Webhook request payload as received from GitHub.<br>
<br>
If you have only access to an already parsed object, stringify it with <code>JSON.stringify(payload)</code>
</td>
</tr>
<tr>
<td>
<code>
signature
</code>
<em>
(String)
</em>
</td>
<td>
<strong>Required.</strong>
Signature string as calculated by <code><a href="../sign">sign()</a></code>.
</td>
</tr>
<tr>
<td>
<code>
additionalSecrets
</code>
<em>
(Array of String)
</em>
</td>
<td>
If given, each additional secret will be tried in turn.
</td>
</tr>
</table>

This is a thin wrapper around [`verify()`](#verify) that is intended to ease callers' support for key rotation.
Resolves with `true` or `false`. Throws error if a required argument is missing.

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
27 changes: 26 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
export { sign } from "./node/sign";
export { verify } from "./node/verify";
import { verify } from "./node/verify";
export { verify };

export async function verifyWithFallback(
secret: string,
payload: string,
signature: string,
additionalSecrets: undefined | string[]
): Promise<any> {
const firstPass = await verify(secret, payload, signature);

if (firstPass) {
return true;
}

if (additionalSecrets !== undefined) {
for (const s of additionalSecrets) {
const v: boolean = await verify(s, payload, signature);
if (v) {
return v;
}
}
}

return false;
}
40 changes: 38 additions & 2 deletions test/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { verify } from "../src";
import { verify, verifyWithFallback } from "../src";

function toNormalizedJsonString(payload: object) {
// GitHub sends its JSON with an indentation of 2 spaces and a line break at the end
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("verify", () => {
expect(signatureMatches).toBe(false);
});

test("verify(secret, eventPayload, signatureSHA256) returns false for correct secret", async () => {
test("verify(secret, eventPayload, signatureSHA256) returns false for incorrect secret", async () => {
const signatureMatches = await verify("foo", eventPayload, signatureSHA256);
expect(signatureMatches).toBe(false);
});
Expand Down Expand Up @@ -141,3 +141,39 @@ describe("verify", () => {
expect(signatureMatchesEscapedSequence).toBe(true);
});
});

describe("verifyWithFallback", () => {
it("is a function", () => {
expect(verifyWithFallback).toBeInstanceOf(Function);
});

test("verifyWithFallback(secret, eventPayload, signatureSHA256, [bogus]) returns true", async () => {
const signatureMatches = await verifyWithFallback(
secret,
eventPayload,
signatureSHA256,
["foo"]

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "foo" is used as [key](1).
);
expect(signatureMatches).toBe(true);
});

test("verifyWithFallback(bogus, eventPayload, signatureSHA256, [secret]) returns true", async () => {
const signatureMatches = await verifyWithFallback(
"foo",

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "foo" is used as [key](1).
eventPayload,
signatureSHA256,
[secret]
);
expect(signatureMatches).toBe(true);
});

test("verify(bogus, eventPayload, signatureSHA256, [bogus]) returns false", async () => {
const signatureMatches = await verifyWithFallback(
"foo",

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "foo" is used as [key](1).
eventPayload,
signatureSHA256,
["foo"]

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "foo" is used as [key](1).
);
expect(signatureMatches).toBe(false);
});
});