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:
7 files changed
+140
-28
No files matched your search
+36
-1
@@ -9,7 +9,7 @@ import {
|
||||
revokeApiToken,
|
||||
} from '../src/database/repositories/web-session-repository.js';
|
||||
import { authorize } from '../src/web/auth/guard.js';
|
||||
import { resolveAccessLevel } from '../src/web/auth/discord-oauth.js';
|
||||
import { redirectUri, requestOrigin, resolveAccessLevel } from '../src/web/auth/discord-oauth.js';
|
||||
import {
|
||||
assertSameOrigin,
|
||||
authenticate,
|
||||
@@ -227,3 +227,38 @@ describe('oauth access level', () => {
|
||||
assert.equal(resolveAccessLevel({ member: null, userId: '2', guildOwnerId: '1', roles }), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oauth redirect', () => {
|
||||
it('follows the address the visitor actually used', () => {
|
||||
// A hosting panel can publish the site on another port than the one
|
||||
// announced by `WEB_PUBLIC_URL`; the browser's address is what Discord
|
||||
// compares against.
|
||||
const request = fakeRequest({ host: 'bot.example:25571' });
|
||||
assert.equal(requestOrigin(request), 'http://bot.example:25571');
|
||||
assert.equal(redirectUri(request), 'http://bot.example:25571/auth/callback');
|
||||
});
|
||||
|
||||
it('honours a reverse proxy', () => {
|
||||
const request = fakeRequest({
|
||||
host: '127.0.0.1:3080',
|
||||
'x-forwarded-host': 'bot.example',
|
||||
'x-forwarded-proto': 'https',
|
||||
});
|
||||
assert.equal(redirectUri(request), 'https://bot.example/auth/callback');
|
||||
});
|
||||
|
||||
it('keeps only the left-most forwarded value', () => {
|
||||
const request = fakeRequest({
|
||||
host: 'interne:3080',
|
||||
'x-forwarded-host': 'bot.example, interne',
|
||||
'x-forwarded-proto': 'https, http',
|
||||
});
|
||||
assert.equal(requestOrigin(request), 'https://bot.example');
|
||||
});
|
||||
|
||||
it('falls back to the configured URL when there is no request', () => {
|
||||
// `WEB_PUBLIC_URL` is unset in the tests, so nothing can be built.
|
||||
assert.equal(requestOrigin(undefined), '');
|
||||
assert.equal(redirectUri(undefined), '');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user