Refactor of OAuth account linking flows (#12246)

* Refactor of OAuth account linking flows

* PR feedback
This commit is contained in:
Tom Moor authored and GitHub committed 2026-05-02 18:54:38 -04:00
1 parent 8c716b173a
commit f50bb00b29
30 files changed
+590 -84

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
import { buildUser } from "@server/test/factories";
import { getTestServer } from "@server/test/support";
import { SetupAction } from "./schema";
const server = getTestServer();
describe("#github.callback", () => {
it("should reject callback when state nonce does not match cookie", async () => {
const user = await buildUser();
const state = JSON.stringify({
teamId: user.teamId,
nonce: "attacker-nonce",
});
const res = await server.get(
`/api/github.callback?state=${encodeURIComponent(
state
)}&code=123&setup_action=${SetupAction.install}&installation_id=1&token=${user.getJwtToken()}`,
{ redirect: "manual" }
);
const body = await res.json();
expect(res.status).toEqual(400);
expect(body.error).toEqual("state_mismatch");
});
it("should reject callback when nonce is missing from state", async () => {
const user = await buildUser();
const state = JSON.stringify({ teamId: user.teamId });
const res = await server.get(
`/api/github.callback?state=${encodeURIComponent(
state
)}&code=123&setup_action=${SetupAction.install}&installation_id=1&token=${user.getJwtToken()}`,
{ redirect: "manual" }
);
expect(res.status).toEqual(400);
});
it("should fail when state is not valid JSON", async () => {
const user = await buildUser();
const res = await server.get(
`/api/github.callback?state=bad&code=123&setup_action=${SetupAction.install}&installation_id=1&token=${user.getJwtToken()}`,
{ redirect: "manual" }
);
expect(res.status).toEqual(400);
});
});
+13 -4
View File
@@ -2,6 +2,7 @@ import Router from "koa-router";
import find from "lodash/find";
import { IntegrationService, IntegrationType } from "@shared/types";
import { createContext } from "@server/context";
import { ValidationError } from "@server/errors";
import apexAuthRedirect from "@server/middlewares/apexAuthRedirect";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
@@ -9,7 +10,8 @@ import validate from "@server/middlewares/validate";
import validateWebhook from "@server/middlewares/validateWebhook";
import { IntegrationAuthentication, Integration } from "@server/models";
import type { APIContext } from "@server/types";
import { GitHubUtils } from "../../shared/GitHubUtils";
import { verifyOAuthStateNonce } from "@server/utils/oauth";
import { GitHubOAuthNonceCookie, GitHubUtils } from "../../shared/GitHubUtils";
import env from "../env";
import { GitHub } from "../github";
import GitHubWebhookTask from "../tasks/GitHubWebhookTask";
@@ -22,7 +24,7 @@ router.get(
auth({ optional: true }),
validate(T.GitHubCallbackSchema),
apexAuthRedirect<T.GitHubCallbackReq>({
getTeamId: (ctx) => ctx.input.query.state,
getTeamId: (ctx) => GitHubUtils.parseState(ctx.input.query.state)?.teamId,
getRedirectPath: (ctx, team) =>
GitHubUtils.callbackUrl({
baseUrl: team.url,
@@ -34,7 +36,7 @@ router.get(
async (ctx: APIContext<T.GitHubCallbackReq>) => {
const {
code,
state: teamId,
state,
error,
installation_id: installationId,
setup_action: setupAction,
@@ -52,7 +54,14 @@ router.get(
return;
}
const client = await GitHub.authenticateAsUser(code!, teamId);
const parsedState = GitHubUtils.parseState(state);
if (!parsedState) {
throw ValidationError("Invalid state");
}
verifyOAuthStateNonce(ctx, GitHubOAuthNonceCookie, parsedState.nonce);
const client = await GitHub.authenticateAsUser(code!, state);
const installationsByUser = await client.requestAppInstallations();
const installation = find(
installationsByUser,
+1 -1
View File
@@ -12,7 +12,7 @@ export const GitHubCallbackSchema = BaseSchema.extend({
query: z
.object({
code: z.string().nullish(),
state: z.uuid().nullish(),
state: z.string(),
error: z.string().nullish(),
installation_id: z.coerce.number().optional(),
setup_action: z.enum(SetupAction),