Skip to content

feat(typescript): compile-time missing secret error #461

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

Merged
merged 3 commits into from
Mar 30, 2021
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
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class Webhooks<TTransformed = unknown> {
next?: (err?: any) => void
) => void | Promise<void>;

constructor(options: Options<TTransformed>) {
constructor(options: Options<TTransformed> & { secret: string }) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State = {
const state: State & { secret: string } = {
eventHandler: createEventHandler(options),
path: options.path || "/",
secret: options.secret,
Expand Down Expand Up @@ -88,7 +88,9 @@ class Webhooks<TTransformed = unknown> {
}

/** @deprecated `createWebhooksApi()` is deprecated and will be removed in a future release of `@octokit/webhooks`, please use the `Webhooks` class instead */
const createWebhooksApi = <TTransformed>(options: Options<TTransformed>) => {
const createWebhooksApi = <TTransformed>(
options: Options<TTransformed> & { secret: string }
) => {
const log = createLogger(options.log);
log.warn(
"[@octokit/webhooks] `createWebhooksApi()` is deprecated and will be removed in a future release of `@octokit/webhooks`, please use the `Webhooks` class instead"
Expand Down
4 changes: 2 additions & 2 deletions src/middleware-legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { createEventHandler } from "../event-handler/index";
import { middleware } from "./middleware";
import { Options, State } from "../types";

export function createMiddleware(options: Options) {
export function createMiddleware(options: Options & { secret: string }) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State = {
const state: State & { secret: string } = {
eventHandler: createEventHandler(options),
path: options.path || "/",
secret: options.secret,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware-legacy/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IncomingMessage, ServerResponse } from "http";
import { State, WebhookEventHandlerError } from "../types";

export function middleware(
state: State,
state: State & { secret: string },
request: IncomingMessage,
response: ServerResponse,
next?: Function
Expand Down
8 changes: 2 additions & 6 deletions src/middleware-legacy/verify-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { EmitterWebhookEvent, State } from "../types";
import { verify } from "../verify/index";

export function verifyAndReceive(
state: State,
state: State & { secret: string },
event: EmitterWebhookEvent & { signature: string }
): any {
// verify will validate that the secret is not undefined
const matchesSignature = verify(
state.secret!,
event.payload,
event.signature
);
const matchesSignature = verify(state.secret, event.payload, event.signature);

if (!matchesSignature) {
const error = new Error(
Expand Down
1 change: 1 addition & 0 deletions test/unit/middleware-constructor-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createMiddleware as Middleware } from "../../src/middleware-legacy";

test("options: none", () => {
// @ts-expect-error
expect(() => Middleware({})).toThrow();
});
5 changes: 3 additions & 2 deletions test/unit/middleware-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test("next() callback", () => {

middleware(
{
secret: "mysecret",
hooks: {},
log: console,
},
Expand All @@ -45,7 +46,7 @@ describe("when does a timeout on retrieving the payload", () => {
mockVerifyAndReceive.mockResolvedValueOnce(undefined);

const promiseMiddleware = middleware(
{ hooks: {}, path: "/foo", log: console },
{ secret: "mysecret", hooks: {}, path: "/foo", log: console },
({
method: "POST",
url: "/foo",
Expand Down Expand Up @@ -78,7 +79,7 @@ describe("when does a timeout on retrieving the payload", () => {
mockVerifyAndReceive.mockRejectedValueOnce(new Error("random error"));

const promiseMiddleware = middleware(
{ hooks: {}, path: "/foo", log: console },
{ secret: "mysecret", hooks: {}, path: "/foo", log: console },
({
method: "POST",
url: "/foo",
Expand Down