Skip to content

Security: Harden redirectLink against open-redirects in login flow #2663

@coderabbitai

Description

@coderabbitai

Description

The login component in pages/sites/[slug]/[locale]/login.tsx currently uses redirectLink from localStorage without validation, which could lead to open-redirect vulnerabilities if the localStorage value is manipulated client-side.

Current Implementation

Lines 58-62 in login.tsx:

const redirectLink = localStorage.getItem('redirectLink');
if (redirectLink) {
  localStorage.removeItem('redirectLink');
  push(redirectLink);
}

Security Risk

Since localStorage can be manipulated by client-side scripts or browser developer tools, an attacker could set redirectLink to an external URL, causing the application to redirect users to malicious sites.

Suggested Solution

Validate and constrain redirectLink to same-origin paths before redirecting:

localStorage.removeItem('redirectLink');
try {
  const url = new URL(redirectLink, window.location.origin);
  if (url.origin === window.location.origin) {
    // internal path: preserve locale, query, hash
    push(`${url.pathname}${url.search}${url.hash}`);
  } else {
    // external or unexpected origin: safe fallback
    push('/profile');
  }
} catch {
  // malformed or empty: safe fallback
  push('/profile');
}

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions