Skip to content

Share the WP.com host check and apply it to notification media - #25867

Merged
crazytonyli merged 4 commits into
fix/media-token-allowlistfrom
fix/wpcom-host-allowlist-shared
Aug 5, 2026
Merged

Share the WP.com host check and apply it to notification media#25867
crazytonyli merged 4 commits into
fix/media-token-allowlistfrom
fix/wpcom-host-allowlist-shared

Conversation

@jkmassel

@jkmassel jkmassel commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Stacked on #25863 — this targets that branch, not trunk. Merge #25863 first.

Summary

  • Consolidates the WordPress.com host check into a single URL.isWordPressComHost helper in WordPressShared, replacing the copies that had grown up in MediaRequestAuthenticator and the notification service extension.
  • Applies the same exact/subdomain allowlist to the notification service extension's media requests, extending the hardening from Restrict WP.com media request authentication to WP.com hosts #25863 to that path.
  • Replaces substring-based URL routing in SiteIconViewModel with host comparisons.

Changes

1. WordPressShared/URL+Helpers.swift

Add isWordPressComHost — matches wordpress.com/wp.com exactly or as a subdomain, lowercased first (hostnames are case-insensitive, RFC 4343). Documented as the allowlist for attaching the account-wide WP.com token, and why it is deliberately broader than the neighbouring isHostedAtWPCom.

2. MediaRequestAuthenticator.swift

Replace the local isTokenAllowed (added in #25863) with url.isWordPressComHost at all three call sites.

3. NotificationService.swift

Gate the media token header on url.isWordPressComHost in place of a host.contains(...) match, remove the now-unused isWPComSite, and drop a leftover debug print.

4. SiteIconViewModel+Extensions.swift

Route icon URLs by parsed host instead of contains over the whole URL string. The two WP.com checks (isDotcomURL, isPhotonURL) fed the same branch and collapse into isWordPressComHost; the Gravatar check becomes a host-anchored comparison. No credentials flow here — the stake is that a crafted URL could previously skip the Photon proxy and be fetched directly. Note one deliberate broadening: non-.files *.wordpress.com hosts are now sized directly (?w=&h=) rather than Photon-wrapped; both are WP.com infrastructure and both resize.

5. Tests

New URLWordPressComHostTests in WordPressSharedTests covers the allowlist, case-insensitivity, subdomain lookalikes, userinfo-spoofed hosts, and hostless URLs. The equivalent unit test moves out of MediaRequestAuthenticatorTests; its behavioural checks stay. SiteIconViewModelTests gains lookalike-routing cases and pins the *.wordpress.com broadening.

Kept in WordPressShared rather than WordPressCore on purpose: the notification service extension links WordPressShared but not WordPressCore, and WordPressCore pulls in wordpress-rs — too heavy for a memory-capped extension.

Test plan

  • swift testURLWordPressComHostTests passes (6/6, verified locally)
  • WordPressUnitTests (CI) → MediaRequestAuthenticatorTests and SiteIconViewModelTests, plus the app and notification extension compiling against the shared helper

Related

Lift the WordPress.com host check into `URL.isWordPressComHost` in
WordPressShared so `MediaRequestAuthenticator` and the notification
service extension share one allowlist instead of duplicating it.

Gate the notification extension's media token on that helper, replacing
a `host.contains(...)` match, and drop a leftover debug `print`.
@wpmobilebot

wpmobilebot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number33614
VersionPR #25867
Bundle IDorg.wordpress.alpha
Commit545feab
Installation URL6qjd11ttu3am8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number33614
VersionPR #25867
Bundle IDcom.jetpack.alpha
Commit545feab
Installation URL6687qmg8cntko
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

Parse the icon URL and compare hosts when deciding between the direct,
blavatar, and Photon optimizers, replacing `contains` matches over the
whole URL string. The two WP.com checks collapse into the shared
`isWordPressComHost` helper; the Gravatar check gets its own
host-anchored comparison.
@wpmobilebot

Copy link
Copy Markdown
Contributor

🤖 Build Failure Analysis

This build has failures. Claude has analyzed them - check the build annotations for details.

@jkmassel
jkmassel requested a review from crazytonyli August 4, 2026 21:15
@@ -35,10 +35,13 @@ extension SiteIconViewModel {
extension SiteIconViewModel {
/// Returns the Size Optimized URL for a given Path.
static func optimizedURL(for path: String, imageSize: CGSize = SiteIconViewModel.Size.regular.size, isP2: Bool = false) -> URL? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What you think about changing path to url: #25868?

let optimizedURL = SiteIconViewModel.optimizedURL(for: path)

// Then
XCTAssertEqual(optimizedURL?.host, "i0.wp.com")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This assertion fails.

@jkmassel jkmassel added the Media label Aug 5, 2026
@jkmassel jkmassel added this to the 27.2 milestone Aug 5, 2026
@crazytonyli
crazytonyli merged commit 8411265 into fix/media-token-allowlist Aug 5, 2026
28 checks passed
@crazytonyli
crazytonyli deleted the fix/wpcom-host-allowlist-shared branch August 5, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants