diff --git a/server/middlewares/authentication.ts b/server/middlewares/authentication.ts index 32f8efc518..21f259f848 100644 --- a/server/middlewares/authentication.ts +++ b/server/middlewares/authentication.ts @@ -22,6 +22,8 @@ type BaseAuthenticationOptions = { role?: UserRole; /** Type of authentication required to access the route. */ type?: AuthenticationType | AuthenticationType[]; + /** Transports the token may be received from, defaults to all. */ + transports?: AuthTransport[]; }; type AuthenticationOptions = @@ -101,10 +103,18 @@ export default function auth(options: AuthenticationOptions = {}) { * Parses the authentication token from the request context. * * @param ctx The application context containing the request information. + * @param transports The transports to read the token from, defaults to all. * @returns An object containing the token and its transport method. */ -export function parseAuthentication(ctx: AppContext): AuthInput { - const authorizationHeader = ctx.request.get("authorization"); +export function parseAuthentication( + ctx: AppContext, + transports?: AuthTransport[] +): AuthInput { + const isAllowed = (transport: AuthTransport) => + !transports || transports.includes(transport); + const authorizationHeader = isAllowed("header") + ? ctx.request.get("authorization") + : undefined; if (authorizationHeader) { const parts = authorizationHeader.split(" "); @@ -125,6 +135,7 @@ export function parseAuthentication(ctx: AppContext): AuthInput { ); } } else if ( + isAllowed("body") && ctx.request.body && typeof ctx.request.body === "object" && "token" in ctx.request.body @@ -133,12 +144,12 @@ export function parseAuthentication(ctx: AppContext): AuthInput { token: String(ctx.request.body.token), transport: "body", }; - } else if (ctx.request.query?.token) { + } else if (isAllowed("query") && ctx.request.query?.token) { return { token: String(ctx.request.query.token), transport: "query", }; - } else { + } else if (isAllowed("cookie")) { const accessToken = ctx.cookies.get("accessToken"); if (accessToken) { return { @@ -164,7 +175,7 @@ async function validateAuthentication( service?: string; scope?: string[]; }> { - const { token, transport } = parseAuthentication(ctx); + const { token, transport } = parseAuthentication(ctx, options.transports); if (!token) { throw AuthenticationError("Authentication required"); diff --git a/server/routes/auth/index.test.ts b/server/routes/auth/index.test.ts index 349fc46bcc..adade4cd10 100644 --- a/server/routes/auth/index.test.ts +++ b/server/routes/auth/index.test.ts @@ -1,5 +1,10 @@ import { addMonths } from "date-fns"; -import { buildUser, buildCollection } from "@server/test/factories"; +import { + buildAdmin, + buildApiKey, + buildUser, + buildCollection, +} from "@server/test/factories"; import { getTestServer, setCloudHosted, @@ -97,4 +102,36 @@ describe("auth provider routes", () => { const state = verifyOAuthState(location.searchParams.get("state") ?? ""); expect(state.actorId).toEqual(user.id); }); + + it("should not use an API key in the query as the actor", async () => { + const admin = await buildAdmin(); + const apiKey = await buildApiKey({ userId: admin.id }); + const res = await server.get(`/auth/google?token=${apiKey.value}`, { + redirect: "manual", + }); + expect(res.status).toEqual(302); + + const location = new URL(res.headers.get("location") ?? ""); + const state = verifyOAuthState(location.searchParams.get("state") ?? ""); + expect(state.actorId).toBeUndefined(); + }); + + it("should prefer the session cookie over a token in the query", async () => { + const admin = await buildAdmin(); + const victim = await buildUser(); + const res = await server.get( + `/auth/google?token=${admin.getTransferToken()}`, + { + redirect: "manual", + headers: { + Cookie: `accessToken=${victim.getSessionToken()}`, + }, + } + ); + expect(res.status).toEqual(302); + + const location = new URL(res.headers.get("location") ?? ""); + const state = verifyOAuthState(location.searchParams.get("state") ?? ""); + expect(state.actorId).toEqual(victim.id); + }); }); diff --git a/server/routes/auth/index.ts b/server/routes/auth/index.ts index 8b37ca9059..1012eb59d6 100644 --- a/server/routes/auth/index.ts +++ b/server/routes/auth/index.ts @@ -26,7 +26,8 @@ void (async () => { ) ); - router.use(authMiddleware({ optional: true })); + // The OAuth actor must come from the browser session, not a link. + router.use(authMiddleware({ optional: true, transports: ["cookie"] })); for (const resolvedRouter of resolvedRouters) { if (resolvedRouter) { router.use("/", resolvedRouter.routes());