fix(web): derive the OAuth2 redirect from the address actually used

`WEB_PUBLIC_URL` was the only source of the `redirect_uri`, so a hosting
panel or a proxy publishing the dashboard on another port produced a link
Discord refuses, with nothing on screen pointing at the port mismatch.

The browser's own origin is what Discord compares against, so the redirect
now comes from the request, honouring `X-Forwarded-Host` and
`X-Forwarded-Proto` when a proxy sits in front. `WEB_PUBLIC_URL` stays as
the fallback for links built outside a request, and the sign-in page prints
the exact value to declare in the portal.

The `Secure` cookie flag follows the same rule: announcing HTTPS in the
configuration while serving plain HTTP made the browser drop the session.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-23 20:12:44 +02:00
1 parent e63d6aa18e
commit 20ff701430
7 files changed
+140 -28

No files matched your search

+30 -5
View File
@@ -30,7 +30,19 @@ const pepper = (() => {
const hash = (value) => createHmac('sha256', pepper).update(value).digest('hex');
export const secureCookies = () => env.web.publicUrl.startsWith('https://');
/**
* A `Secure` cookie is dropped by the browser over plain HTTP, so the flag has
* to follow the scheme the visitor actually used rather than what the
* configuration announces.
*/
export const secureCookies = (request) => {
const forwarded = String(request?.headers?.['x-forwarded-proto'] ?? '')
.split(',')[0]
.trim();
if (forwarded) return forwarded === 'https';
if (request?.socket?.encrypted) return true;
return env.web.publicUrl.startsWith('https://');
};
export function issueToken() {
return randomBytes(32).toString('base64url');
@@ -74,7 +86,7 @@ export function openSession(request, response, { user = {}, accessLevel, source
appendCookie(
response,
serializeCookie(SESSION_COOKIE, token, { maxAge: ttlSeconds, secure: secureCookies() }),
serializeCookie(SESSION_COOKIE, token, { maxAge: ttlSeconds, secure: secureCookies(request) }),
);
log.info('Dashboard session opened', { userId: session.user_id, source, accessLevel });
return session;
@@ -84,7 +96,7 @@ export function closeSession(request, response) {
const token = parseCookies(request.headers.cookie)[SESSION_COOKIE];
const session = token ? getLiveSessionByHash(hash(token)) : null;
if (session) deleteSession(session.id);
appendCookie(response, serializeCookie(SESSION_COOKIE, '', { maxAge: 0, secure: secureCookies() }));
appendCookie(response, serializeCookie(SESSION_COOKIE, '', { maxAge: 0, secure: secureCookies(request) }));
}
/**
@@ -146,8 +158,21 @@ export function assertSameOrigin(request, actor) {
const origin = request.headers.origin;
if (!origin) return;
const host = request.headers.host;
const allowed = new Set([env.web.publicUrl, `http://${host}`, `https://${host}`]);
// The host the visitor used is what the browser puts in `Origin`; behind a
// proxy that is the forwarded one, not the socket's.
const hosts = [request.headers['x-forwarded-host'], request.headers.host]
.map((value) =>
String(value ?? '')
.split(',')[0]
.trim(),
)
.filter(Boolean);
const allowed = new Set([
env.web.publicUrl,
...hosts.flatMap((host) => [`http://${host}`, `https://${host}`]),
]);
if (!allowed.has(origin.replace(/\/+$/, ''))) {
throw forbidden('Requête rejetée : origine inattendue');
}