Commit cf9f646c authored by Anthony Jacob's avatar Anthony Jacob
Browse files

add env var on build and add extra check on url builder

parent beca8727
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ node_modules
.git
.env
.env.*
!.env.production
*.log
Dockerfile
.dockerignore
+13 −4
Original line number Diff line number Diff line
@@ -4,13 +4,22 @@ function getForwardedValue(value: string | null): string | null {
    return value?.split(',')[0]?.trim() || null;
}

function isInvalidHost(value: string | null): boolean {
    if (!value) return true;
    const normalized = value.trim().toLowerCase();
    return normalized === 'null' || normalized === '(null)' || normalized === 'undefined';
}

export function buildExternalUrl(req: NextRequest, path: string): URL {
    const forwardedProto = getForwardedValue(req.headers.get('x-forwarded-proto'));
    const forwardedHost = getForwardedValue(req.headers.get('x-forwarded-host'));
    const host = forwardedHost ?? getForwardedValue(req.headers.get('host'));
    const forwardedHostRaw = getForwardedValue(req.headers.get('x-forwarded-host'));
    const hostRaw = getForwardedValue(req.headers.get('host'));
    const forwardedHost = isInvalidHost(forwardedHostRaw) ? null : forwardedHostRaw;
    const host = isInvalidHost(hostRaw) ? null : hostRaw;
    const resolvedHost = forwardedHost ?? host;

    if (forwardedProto && host) {
        return new URL(path, `${forwardedProto}://${host}`);
    if (forwardedProto && resolvedHost) {
        return new URL(path, `${forwardedProto}://${resolvedHost}`);
    }

    return new URL(path, req.url);