Skip to content

Login: Fix redirect_to handling for WordPress.com subdomains#109808

Open
lancewillett wants to merge 3 commits intotrunkfrom
fix/p2-login-redirect-to
Open

Login: Fix redirect_to handling for WordPress.com subdomains#109808
lancewillett wants to merge 3 commits intotrunkfrom
fix/p2-login-redirect-to

Conversation

@lancewillett
Copy link
Copy Markdown
Contributor

@lancewillett lancewillett commented Apr 3, 2026

Proposed Changes

  • Allow *.wordpress.com subdomains in the login redirect URL allowlist
  • Fall back to the original redirect_to URL parameter when the API doesn't return one

Why are these changes being made?

See P2HQ-55

When users visit a private P2 site while logged out, P2 correctly sets redirect_to to the original URL. After logging in, two issues prevent the redirect from working:

  1. isExternalUrl() in redirect-logged-in treats *.wordpress.com subdomains (e.g., myteamp2.wordpress.com) as external URLs because the allowlist only matches exact hostnames. The redirect_to is silently dropped.

  2. rebootAfterLogin relies solely on the API-sanitized redirect_to. If the API doesn't echo it back, the original URL parameter from the login page is ignored entirely, defaulting to /.

Users end up at wordpress.com/p2/ instead of the P2 they were trying to visit.

Testing Instructions

  1. Open an incognito/private browser window (must be logged out of WordPress.com)
  2. Navigate to a private P2 URL, e.g., cAFMY-p2
  3. Click "Log in" on the private landing page
  4. Verify the login URL contains redirect_to=https%3A%2F%2Fmyteamp2.wordpress.com%2F
  5. Complete the login flow (enter credentials)
  6. Expected: Redirected back to myteamp2.wordpress.com
  7. Before fix: Redirected to wordpress.com/p2/

Also test the "already logged in" path:

  1. While logged in, navigate to https://wordpress.com/log-in?redirect_to=https%3A%2F%2Fmyteamp2.wordpress.com%2F
  2. Click "Continue"
  3. Expected: Redirected to myteamp2.wordpress.com

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you tested accessibility for your changes? Ensure the feature remains usable with various user agents (e.g., browsers), interfaces (e.g., keyboard navigation), and assistive technologies (e.g., screen readers) (PCYsg-S3g-p2).
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
    • For UI changes, have we tested the change in various languages (for example, ES, PT, FR, or DE)? The length of text and words vary significantly between languages.
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-aUh-p2)?

After logging in, users visiting P2 sites (*.wordpress.com subdomains) were
redirected to / instead of their original URL. Two issues:

1. isExternalUrl() in redirect-logged-in only allowed exact hostname matches
   for wordpress.com, rejecting valid subdomains like testp2020a8c.wordpress.com.
   Now allows all *.wordpress.com subdomains.

2. rebootAfterLogin used only the API-sanitized redirect_to with a / fallback.
   If the API didn't echo redirect_to, the original URL parameter was lost.
   Now falls back to getRedirectToOriginal() before defaulting to /.

See: P2HQ-55

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add tests covering the two fixes in the previous commit:

- redirect-logged-in: *.wordpress.com subdomains are allowed, non-https
  subdomains are rejected
- reboot-after-login: falls back to original redirect_to when sanitized
  is null, prefers sanitized when both are set

See: P2HQ-55

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@matticbot
Copy link
Copy Markdown
Contributor

matticbot commented Apr 3, 2026

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • notifications
  • odyssey-stats
  • wpcom-block-editor

To test WordPress.com changes, run install-plugin.sh $pluginSlug fix/p2-login-redirect-to on your sandbox.

@lancewillett lancewillett marked this pull request as ready for review April 3, 2026 00:58
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@lancewillett lancewillett requested a review from tyxla April 3, 2026 01:08
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Apr 3, 2026
@lancewillett lancewillett added Login Enhancement Changes to an existing feature — removing, adding, or changing parts of it labels Apr 3, 2026
@lancewillett lancewillett self-assigned this Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Changes to an existing feature — removing, adding, or changing parts of it Login [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants