Skip to content

Commit 7733ec9

Browse files
chore(web): harden post-auth redirects and legacy URL rewrite (#1161)
* chore(web): harden post-auth redirects and legacy URL rewrite - Add explicit `redirect` callback to the NextAuth config that pins post-auth redirects to the same origin. This mirrors the documented Auth.js default but makes the protection visible in code rather than relying on upstream defaults. - Switch the legacy `/~/...` rewrite in `proxy.ts` from 308 to 301. The rewrite is a backwards-compat shim for v3.0.0–v4.16.8 page URLs, which are virtually all GETs; 301 has matching cache/permanence semantics but downgrades any stray POST to GET and drops the body. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * docs: add CHANGELOG entry for #1161 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 38d2274 commit 7733ec9

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Changed
2020
- Added `/api/avatar` to resolve user profile pictures. [#1159](https://github.com/sourcebot-dev/sourcebot/pull/1159)
21+
- Hardened post-auth redirects with an explicit same-origin `redirect` callback in the NextAuth config, and switched the legacy `/~/...` URL rewrite from a 308 to a 301. [#1161](https://github.com/sourcebot-dev/sourcebot/pull/1161)
2122

2223
### Fixed
2324
- Bumped `postcss` to `8.5.10`. [#1155](https://github.com/sourcebot-dev/sourcebot/pull/1155)

packages/web/src/auth.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
220220
}
221221
},
222222
callbacks: {
223+
// Restrict post-auth redirects (sign-in / sign-out, `callbackUrl`,
224+
// `redirectTo`) to the same origin as the application. This mirrors
225+
// Auth.js's documented default; we set it explicitly so the protection
226+
// is visible in code and not dependent on upstream defaults.
227+
// @see https://authjs.dev/reference/core#redirect
228+
async redirect({ url, baseUrl }) {
229+
if (url.startsWith("/")) {
230+
return `${baseUrl}${url}`;
231+
}
232+
233+
try {
234+
if (new URL(url).origin === baseUrl) {
235+
return url;
236+
}
237+
} catch {
238+
// Malformed URL — fall through to baseUrl.
239+
}
240+
241+
return baseUrl;
242+
},
223243
async jwt({ token, user: _user }) {
224244
const user = _user as User | undefined;
225245
// @note: `user` will be available on signUp or signIn triggers.

packages/web/src/proxy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { StatusCodes } from 'http-status-codes';
12
import { NextResponse } from 'next/server'
23
import type { NextRequest } from 'next/server'
34

@@ -27,12 +28,12 @@ export async function proxy(request: NextRequest) {
2728

2829
if (url.pathname.startsWith('/~/')) {
2930
url.pathname = url.pathname.replace(/^\/~/, '');
30-
return NextResponse.redirect(url, 308);
31+
return NextResponse.redirect(url, StatusCodes.MOVED_PERMANENTLY);
3132
}
3233

3334
if (url.pathname === '/~') {
3435
url.pathname = '/';
35-
return NextResponse.redirect(url, 308);
36+
return NextResponse.redirect(url, StatusCodes.MOVED_PERMANENTLY);
3637
}
3738

3839
return NextResponse.next();

0 commit comments

Comments
 (0)