fix: Remove trust of state.host in auth error redirect

This commit is contained in:
Tom Moor
2024-06-05 06:45:23 -04:00
parent 1d97a6c10b
commit c9d5ff7ca5

View File

@@ -3,7 +3,7 @@ import { Context } from "koa";
import { InternalOAuthError } from "passport-oauth2";
import { Client } from "@shared/types";
import env from "@server/env";
import { AuthenticationError } from "@server/errors";
import { AuthenticationError, OAuthStateMismatchError } from "@server/errors";
import Logger from "@server/logging/Logger";
import { AuthenticationResult } from "@server/types";
import { signIn } from "@server/utils/authentication";
@@ -39,7 +39,12 @@ export default function createMiddleware(providerName: string) {
// form a URL object with the err.redirectPath and replace the host
const reqProtocol =
state?.client === Client.Desktop ? "outline" : ctx.protocol;
const requestHost = state?.host ?? ctx.hostname;
// `state.host` cannot be trusted if the error is a state mismatch, use `ctx.hostname`
const requestHost =
err instanceof OAuthStateMismatchError
? ctx.hostname
: state?.host ?? ctx.hostname;
const url = new URL(
env.isCloudHosted
? `${reqProtocol}://${requestHost}${redirectPath}`