diff --git a/changelog.d/1105.bugfix b/changelog.d/1105.bugfix new file mode 100644 index 00000000..7dffd47c --- /dev/null +++ b/changelog.d/1105.bugfix @@ -0,0 +1 @@ +Fix /oauth responding with 405 despite GitHub being configured. diff --git a/spec/github.spec.ts b/spec/github.spec.ts index 04347437..6fe18b3a 100644 --- a/spec/github.spec.ts +++ b/spec/github.spec.ts @@ -43,6 +43,11 @@ describe("GitHub", () => { webhook: { secret: randomUUID(), }, + oauth: { + client_id: "GITHUB_ID", + client_secret: "GITHUB_SECRET", + redirect_uri: "http://example.org/redirectme", + }, // So we can mock out the URL enterpriseUrl: `http://localhost:${githubPort}`, auth: { @@ -168,4 +173,14 @@ describe("GitHub", () => { expect(body).toContain("My test pull request"); }, ); + + test.each(["/oauth", "/github/oauth"])( + "should redirect invalid oauth requests to oauth.html", + async (path) => { + // This simply tests that oauth requests do not end up being ignored. + const req = await fetch(`http://localhost:${webhooksPort}${path}`); + expect(req.url.startsWith(`http://localhost:${webhooksPort}/oauth.html`)) + .to.be.true; + }, + ); }); diff --git a/src/Webhooks.ts b/src/Webhooks.ts index c42dfcdf..6f93d1a6 100644 --- a/src/Webhooks.ts +++ b/src/Webhooks.ts @@ -36,6 +36,11 @@ export class Webhooks extends EventEmitter { this.config.widgets?.parsedPublicUrl, ); this.expressRouter.use("/github", this.github.getRouter()); + // LEGACY PATH - Will be removed in a future release. + this.expressRouter.get( + "/oauth", + this.github.onGetOAuth.bind(this.github), + ); } if (this.config.gitlab) { @@ -94,9 +99,6 @@ export class Webhooks extends EventEmitter { // LEGACY PATHS. These will be removed in a future version. this.expressRouter.post("/", this.onPayload.bind(this)); - if (this.github) { - this.expressRouter.get("/oauth", this.github.onGetOAuth.bind(this)); - } } public stop() { diff --git a/src/github/Router.ts b/src/github/Router.ts index 77d2a2d0..fff52a2c 100644 --- a/src/github/Router.ts +++ b/src/github/Router.ts @@ -174,13 +174,13 @@ export class GitHubWebhooksRouter { const { setup_action: setupAction, state } = req.query; log.info("Got new oauth request", { state, setupAction }); + if (!this.config.oauth) { + throw new ApiError( + "Bridge is not configured with OAuth support", + ErrCode.DisabledFeature, + ); + } try { - if (!this.config.oauth) { - throw new ApiError( - "Bridge is not configured with OAuth support", - ErrCode.DisabledFeature, - ); - } if (req.query.error) { throw new ApiError( `GitHub Error: ${req.query.error} ${req.query.error_description}`, @@ -291,9 +291,9 @@ export class GitHubWebhooksRouter { public getRouter() { const router = Router(); + router.get("/oauth", this.onGetOAuth.bind(this)); router.use(json({ verify: this.verifyRequest.bind(this) })); router.post("/webhook", this.onWebhook.bind(this)); - router.get("/oauth", this.onGetOAuth.bind(this)); return router; } }