Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Added `/api/avatar` to resolve user profile pictures. [#1159](https://github.com/sourcebot-dev/sourcebot/pull/1159)
- 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)

### Fixed
- Bumped `postcss` to `8.5.10`. [#1155](https://github.com/sourcebot-dev/sourcebot/pull/1155)
Expand Down
20 changes: 20 additions & 0 deletions packages/web/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
}
},
callbacks: {
// Restrict post-auth redirects (sign-in / sign-out, `callbackUrl`,
// `redirectTo`) to the same origin as the application. This mirrors
// Auth.js's documented default; we set it explicitly so the protection
// is visible in code and not dependent on upstream defaults.
// @see https://authjs.dev/reference/core#redirect
async redirect({ url, baseUrl }) {
if (url.startsWith("/")) {
return `${baseUrl}${url}`;
}

try {
if (new URL(url).origin === baseUrl) {
return url;
}
} catch {
// Malformed URL — fall through to baseUrl.
}

return baseUrl;
},
async jwt({ token, user: _user }) {
const user = _user as User | undefined;
// @note: `user` will be available on signUp or signIn triggers.
Expand Down
5 changes: 3 additions & 2 deletions packages/web/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StatusCodes } from 'http-status-codes';
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

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

if (url.pathname.startsWith('/~/')) {
url.pathname = url.pathname.replace(/^\/~/, '');
return NextResponse.redirect(url, 308);
return NextResponse.redirect(url, StatusCodes.MOVED_PERMANENTLY);
}

if (url.pathname === '/~') {
url.pathname = '/';
return NextResponse.redirect(url, 308);
return NextResponse.redirect(url, StatusCodes.MOVED_PERMANENTLY);
}

return NextResponse.next();
Expand Down
Loading