Skip to content

Fix: Enhanced ?next= parameter URL validation and anti-loop protections#633

Open
mhdgning131 wants to merge 2 commits intochithi-dev:mainfrom
mhdgning131:fix/security-and-bugs
Open

Fix: Enhanced ?next= parameter URL validation and anti-loop protections#633
mhdgning131 wants to merge 2 commits intochithi-dev:mainfrom
mhdgning131:fix/security-and-bugs

Conversation

@mhdgning131
Copy link
Copy Markdown

This PR implements stricter security controls and robustness for "next" parameter redirection logic across the login and logout flows with stricter validateRedirectUrl utility

  • Enforces a strict same-origin policy using the new URL() constructor with a trusted base.

  • Automatically strips the domain, protocol, and host from absolute URLs to ensure all redirects stay within the internal site structure (pathname + search + hash)

  • Blocks non-HTTP/S protocols such as javascript: or data:

  • Added explicit /admin path checks at redirect points to prevent infinite loop scenarios

Copy link
Copy Markdown
Collaborator

@baseplate-admin baseplate-admin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small change required :)

Comment on lines +15 to +31
export function validateRedirectUrl(url: string, origin: string): string {
try {
const parsed = new URL(url, origin);

if (parsed.origin !== origin) {
throw new Error('External redirects are not allowed.');
}

if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
throw new Error('Invalid protocol.');
}
return parsed.pathname + parsed.search + parsed.hash;
} catch (e) {
if (e instanceof Error) throw e;
throw new Error('Malformed redirect URL.');
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should live in src/lib/functions/urls.ts

@baseplate-admin baseplate-admin force-pushed the fix/security-and-bugs branch 4 times, most recently from 83d6504 to 0b5d135 Compare April 4, 2026 06:51
updated:   src/frontend/src/routes/(needs_onboarding)/login/+page.svelte
updated:   src/frontend/src/routes/(needs_onboarding)/logout/+page.server.ts
@baseplate-admin baseplate-admin force-pushed the fix/security-and-bugs branch from 0b5d135 to d060e65 Compare April 4, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants