Skip to content

Make wpApiRestUrl, xmlRpcUrl, and app-password creds single-writer#22947

Merged
jkmassel merged 11 commits into
trunkfrom
jkmassel/sitesqlutils-partial-updates
Jun 13, 2026
Merged

Make wpApiRestUrl, xmlRpcUrl, and app-password creds single-writer#22947
jkmassel merged 11 commits into
trunkfrom
jkmassel/sitesqlutils-partial-updates

Conversation

@jkmassel

@jkmassel jkmassel commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #22905. The REST API root (wpApiRestUrl) is discovered/healed out of band — never
carried by the general site-sync responses — yet every full-row insertOrUpdateSite UPDATE
rewrote it. When the in-memory SiteModel came from a source that doesn't carry it (/me/sites,
/sites/<id>, the RN bridge, cookie-nonce auth), the write stamped a stale null over the good
value, so on Atomic sites the recovered URL was wiped on every app foreground.

Two adjacent columns share that write path and are folded in here. The application-password
credentials
weren't actually leaking — the same copy-forward shielded them (it was gated on
them) — but it had to be torn out for the wpApiRestUrl fix, so they take the same exclusion as a
TOCTOU-free replacement. xmlRpcUrl is a third case: the WP.com sync reliably carries it, so
rather than exclude it we preserve it on absence — only a partial writer that omits it (the WPAPI
fetch) can null it. See #3.

Root cause

SiteSqlUtils.insertOrUpdateSite updates via WellSql.update().put(model, UpdateAllExceptId(...)),
which writes every column except _id. Per-handler preservation existed for some columns
but was gated on encrypted AP creds being present — which Atomic sites don't have — so it
didn't fire for them, and it was TOCTOU-fragile regardless.

Fix

Two mechanisms, matched to how each column is sourced:

  • Excluded from the generic mapperWP_API_REST_URL and the app-password credential columns.
    These are never carried by a site-sync response, so the full-row UPDATE skips them entirely (via a new
    UpdateAllExceptId skip-list) and a targeted single-column writer is their sole writer on an
    existing row. INSERT is untouched. Every caller that legitimately set one is rerouted to the targeted
    writer; redundant writes are removed.
  • Preserved on absenceXMLRPC_URL. The WP.com sync does carry it, so the full-row UPDATE still
    writes it (that's how a changed endpoint lands); it just keeps the stored value when the inbound model
    carries none, so a partial writer can't null it.

Column-by-column:

1. WP_API_REST_URL

  • updateWpApiRestUrl (the heal writer from Populate missing wpApiRestUrl during editor preload #22903) becomes the sole writer; clearWpApiRestUrl
    for sign-out.
  • Rerouted the discover/persist callers: updateApplicationPassword, CookieNonceAuthenticator,
    ReactNativeStore, and fetchSiteWPAPIFromApplicationPassword (URL-keyed, since the freshly
    fetched site has no local id).

2. Application-password credentials

Hardening, not a clobber fix — the gated copy-forward already shielded these (see Root cause), so
they weren't leaking; the exclusion is a robust, TOCTOU-free replacement once that copy-forward goes.

  • API_REST_USERNAME, API_REST_PASSWORD and their two IV columns, excluded as a set — the
    IVs are required to decrypt the ciphertext, so persisting the values without them breaks reads.
  • New updateApplicationPasswordCredentials (encrypts) / clearApplicationPasswordCredentials,
    wired into updateApplicationPassword, removeApplicationPassword (full sign-out — also clears
    wpApiRestUrl), clearApplicationPasswordColumns (rotation — intentionally preserves
    wpApiRestUrl), and the WPAPI app-password fetch.
  • createOrUpdateSites persists creds via the targeted writer too: insertOrUpdateSite gained an
    insertOrUpdateSiteReturningId variant so the handler can target the exact written row. This
    fixes app-password login of an existing XML-RPC site, where the exclusion would otherwise
    drop the creds.

3. XMLRPC_URL — preserved, not excluded

Unlike the other two, the WP.com REST sync reliably returns meta.links.xmlrpc (verified against
/me/sites), so excluding the column would silently drop a changed endpoint (e.g. a domain migration)
on an existing row. Instead, insertOrUpdateSite preserves it on absence — copying the stored value
forward only when the inbound model has none — so the WPAPI fetch (which builds a model with a null
xmlRpcUrl) can't clobber a stored/rediscovered value.

  • updateXmlRpcUrl / SiteStore.persistXmlRpcUrl remain for the single-column rediscovery heal — one
    targeted write instead of an ~100-column full-row updateSite.
  • attemptXmlRpcRediscovery (the My Site app-password card) persists the rediscovered endpoint through
    that writer.

Cleanup enabled by the migration

  • Removed the 404 no-op wpApiRestUrl writes in CookieNonceAuthenticator / ReactNativeStore
    (and RN's now-orphaned persistSiteSafely / sitePersistanceFunction) — they reset the column
    in memory to drive rediscovery and never needed to persist.
  • Removed the moot gated credential/wpApiRestUrl copy-forward blocks in updateSite /
    createOrUpdateSites.
  • Collapsed updateApplicationPassword / removeApplicationPassword / clearApplicationPasswordColumns
    to their targeted writers — the in-memory mutations and self-insertOrUpdateSite they used to
    do persisted nothing once the columns were excluded.

Defensive hardening (unrelated to the clobber)

  • decryptAPIRestCredentials now also short-circuits when an IV column is blank (not just when the
    ciphertext is empty), so a malformed ciphertext-without-IV row reads as "no credentials" instead
    of throwing on decrypt(ciphertext, ""). Cheap, and on the same read path.

Out of scope / follow-ups

Testing instructions

Automated

  1. Run ./gradlew :libs:fluxc:testDebugUnitTest
  • Green (bar a pre-existing timezone-dependent StatsUtilsTest flake unrelated to this change).
    SiteSqlUtilsTest covers each column family — a stale full-row update preserves the protected
    column while still updating the others, and for xmlRpcUrl that a carried value still overwrites
    (the migration case) — plus the targeted writers and the blank-IV decrypt guard.
  1. Run ./gradlew :WordPress:testJetpackDebugUnitTest --tests "*ApplicationPasswordViewModelSliceTest"
  • Green — XML-RPC rediscovery now verifies the targeted persistXmlRpcUrl write.

Manual — Atomic site (the original repro)

  1. Sign in with an Atomic site (e.g. *.jurassic.ninja) and open it so wpApiRestUrl is
    discovered/healed.
  2. Background/foreground a few times (triggers FETCH_SITE/FETCH_SITES), then kill and relaunch.
  • WP_API_REST_URL survives — no longer reset to NULL across launches.

Manual — self-hosted application-password site

  1. Add an application password to a self-hosted site, then load My Site a few times.
  • Credentials persist; no false "Unable to connect to your site" banner.
  1. If the site's XML-RPC was disabled and is re-enabled, confirm the card's rediscovery persists.
  • xmlRpcUrl is repopulated and survives a relaunch.
  1. Remove the application password.
  • Credentials and wpApiRestUrl are cleared.

Appendix: site-type × column handling

How each site type's three out-of-band columns are persisted, and what this PR changes (Δ).

Mechanisms: 🔒 excluded — the full-row sync UPDATE skips the column; only a dedicated targeted writer persists it (never carried by a sync, so a full-row write could only zero it). 🛡️ preserve-on-absence — the full-row UPDATE writes the column when the inbound model carries a value, but keeps the stored value when it doesn't. Targeted writers: updateWpApiRestUrl / updateXmlRpcUrl / updateApplicationPasswordCredentials (+ clear* and URL-keyed *ForWPAPISite variants).

Site type wpApiRestUrl xmlRpcUrl App-password creds
WP.com Simple (isWPCom && !atomic) Getter always synthesizes public-api.wordpress.com/wp/v2/sites/<id> on read, so the stored column is moot (mapper writes the synthetic value on INSERT; 🔒 only stops UPDATEs rewriting it). No change. From meta.links.xmlrpc; 🛡️ written on sync, but unused (Simple sites don't speak XML-RPC). No meaningful change. Not carried in normal use — WP.com bearer token; the Jetpack-tunnel mint only works for Atomic/Jetpack sites. No change.
WP.com Atomic / Jetpack-via-REST (isUsingWpComRestApi, non-simple) Real REST root discovered/healed out of band. Δ Was wiped to NULL on every app foreground (#22905); now 🔒 excluded → preserved, healed via updateWpApiRestUrl. From meta.links.xmlrpc; 🛡️ written on sync (incl. a migrated endpoint). Minted by the My-Site card (Jetpack tunnel). Δ Was zeroed by credential-less /me/sites syncs; now 🔒 excluded → preserved via updateApplicationPasswordCredentials.
Self-hosted — application password (ORIGIN_WPAPI) Set at fetch. 🔒 excluded → persisted by URL-keyed updateWpApiRestUrlForWPAPISite (fresh model has no local id yet). fetchWPAPISite leaves it null. Δ 🛡️ preserve-on-absence keeps a rediscovered value — a re-fetch would otherwise clobber it; healed via persistXmlRpcUrl (rediscovery card, gated !isUsingWpComRestApi). Set at fetch. 🔒 excluded → URL-keyed updateApplicationPasswordCredentialsForWPAPISite.
Self-hosted — XML-RPC (ORIGIN_XMLRPC) Set via app-password discovery (apiRootUrl) when present. 🔒 excluded → written on the stamped row by updateWpApiRestUrl. Identity column — carried by the model → 🛡️ written normally. App-password login: Δ now persisted on the exact written row via insertOrUpdateSiteReturningId + updateApplicationPasswordCredentials (the existing-site half of the #22905 fix; the full-row write alone dropped them).

Notes

  • The clobber bug only ever bit columns a sync doesn't carry — wpApiRestUrl and creds on Atomic / Jetpack-via-REST / self-hosted sites. WP.com Simple was never affected (synthetic getter), and xmlRpcUrl was never the real problem (the WP.com sync reliably returns meta.links.xmlrpc).
  • xmlRpcUrl was de-escalated from excluded to preserve-on-absence during review: excluding it would have silently dropped a migrated endpoint for WP.com sites, and the only real non-WP.com clobber (the ORIGIN_WPAPI re-fetch) is covered by preserve-on-absence.
  • Clear paths (action-driven): full sign-out clears creds and wpApiRestUrl; credential rotation clears creds but preserves wpApiRestUrl (reused across rotations).

@dangermattic

dangermattic commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator
2 Warnings
⚠️ This PR is larger than 300 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.
⚠️ PR is not assigned to a milestone.

Generated by 🚫 Danger

@wpmobilebot

wpmobilebot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

App Icon📲 You can test the changes from this Pull Request in WordPress Android by scanning the QR code below to install the corresponding build.

App NameWordPress Android
Build TypeDebug
Versionpr22947-fb89734
Build Number1493
Application IDorg.wordpress.android.prealpha
Commitfb89734
Installation URL1edrangp9kgqo
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

App Icon📲 You can test the changes from this Pull Request in Jetpack Android by scanning the QR code below to install the corresponding build.

App NameJetpack Android
Build TypeDebug
Versionpr22947-fb89734
Build Number1493
Application IDcom.jetpack.android.prealpha
Commitfb89734
Installation URL5imsqk1sk00t0
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

🤖 Build Failure Analysis

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

@jkmassel jkmassel changed the title Stop full-row site writes from clobbering wpApiRestUrl Make wpApiRestUrl, xmlRpcUrl, and app-password creds single-writer Jun 5, 2026
@jkmassel jkmassel force-pushed the jkmassel/sitesqlutils-partial-updates branch from 6885e4c to 67118a8 Compare June 9, 2026 22:18
@jkmassel jkmassel force-pushed the jkmassel/sitesqlutils-partial-updates branch from 67118a8 to 88e8833 Compare June 10, 2026 21:56
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.00000% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.28%. Comparing base (a88923a) to head (fb89734).
⚠️ Report is 1 commits behind head on trunk.

Files with missing lines Patch % Lines
...ava/org/wordpress/android/fluxc/store/SiteStore.kt 80.55% 1 Missing and 6 partials ⚠️
...uxc/network/rest/wpapi/CookieNonceAuthenticator.kt 0.00% 3 Missing ⚠️
...ordpress/android/fluxc/persistence/SiteSqlUtils.kt 96.07% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk   #22947      +/-   ##
==========================================
+ Coverage   37.22%   37.28%   +0.06%     
==========================================
  Files        2330     2330              
  Lines      125667   125701      +34     
  Branches    17115    17122       +7     
==========================================
+ Hits        46777    46872      +95     
+ Misses      75104    75032      -72     
- Partials     3786     3797      +11     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jkmassel jkmassel marked this pull request as ready for review June 10, 2026 23:08
@jkmassel jkmassel requested a review from oguzkocer June 10, 2026 23:08

@oguzkocer oguzkocer left a comment

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.

Looks good :shipit:

I've tested the race condition I reported here and it worked great.

jkmassel added 11 commits June 13, 2026 09:26
On Atomic / Jetpack-WPCom-REST sites a recovered wpApiRestUrl was wiped
to NULL on every app foreground: any full-row insertOrUpdateSite UPDATE
built from a partial in-memory SiteModel (FETCH_SITE/FETCH_SITES, RN,
cookie-nonce) wrote every column, clobbering the healed value.

Exclude WP_API_REST_URL from the generic UpdateAllExceptId mapper so
updateWpApiRestUrl is the sole writer on an existing row, and route every
legitimate writer through the targeted helpers.

Fixes #22905.
The same full-row insertOrUpdateSite UPDATE that clobbered wpApiRestUrl
also zeroed the application-password credential columns: a credential-less
inbound SiteModel (e.g. a /me/sites sync) overwrote the encrypted
username/password and their IVs with empty values.

Exclude API_REST_USERNAME, API_REST_PASSWORD and their IV columns from the
UpdateAllExceptId mapper (as a set — the IVs are required to decrypt), and
route the credential writers through targeted single-column writers.

Extends the #22905 fix to the credential columns.
xmlRpcUrl is set either out of band (XML-RPC rediscovery) or by the
WP.com REST sync (meta.links.xmlrpc). A full-row insertOrUpdateSite
UPDATE built from a partial SiteModel that doesn't carry it — e.g. the
WPAPI fetch, which builds a model with a null xmlRpcUrl — wrote that
null over a good value.

Preserve XMLRPC_URL on absence in the generic update path: keep the
stored value when the inbound model has none, persist it when the model
carries one (the WP.com sync, including a changed endpoint after a
domain migration). Add a targeted updateXmlRpcUrl writer for the
single-column rediscovery heal; the recovery flow that calls it lands
separately.

Extends the #22905 fix to XMLRPC_URL.
attemptXmlRpcRediscovery dispatched newUpdateSiteAction to change one
field, which (with XMLRPC_URL excluded from the full-row mapper) dropped
the value and rewrote ~80 unchanged columns. Add SiteStore.persistXmlRpcUrl
and route rediscovery through it — one targeted write, mirroring the
wpApiRestUrl heal. Drops the now-unused dispatcher from the slice.
createOrUpdateSites (the XML-RPC app-password login store path) does a
full-row write that excludes the credential columns, with no targeted
writer to follow up — so re-logging into an already-stored self-hosted
site silently dropped the credentials.

Add SiteSqlUtils.insertOrUpdateSiteReturningId, which returns the local
id of the row it wrote; insertOrUpdateSite becomes a thin rows-affected
wrapper over it (behavior unchanged). createOrUpdateSites uses the
returned id to persist credentials + wpApiRestUrl via the targeted
writers on the exact row.
- The cookie-nonce and React Native 404 handlers reset wpApiRestUrl in
  memory to force rediscovery on retry, then persisted via
  insertOrUpdateSite/persistSiteSafely — now a no-op for the excluded
  column. Drop the dead DB write (keep the in-memory reset), which also
  orphaned ReactNativeStore.persistSiteSafely and its injected persist
  function; remove those.
- updateSite / createOrUpdateSites copied credentials + wpApiRestUrl from
  the DB onto the inbound model before the write; the mapper exclusion
  already preserves those columns, so drop the moot copy (editor-prefs
  copy stays).
- Rework ReactNativeStoreWPAPITest to mock SiteSqlUtils and assert on
  updateWpApiRestUrl (the discover path persists there now).
After the single-writer migration the full-row write skips the credential and
WP_API_REST_URL columns, so the in-memory mutations and the self-insertOrUpdateSite
in updateApplicationPassword, removeApplicationPassword, and
clearApplicationPasswordColumns persisted nothing — the targeted writers do all the
work. Drop the dead code (keeping the new-site insert in updateApplicationPassword)
and remove the now-unused insertOrUpdateSite stubs from the tests.
decryptAPIRestCredentials bailed only when the ciphertext columns were empty; a row
with ciphertext but a blank IV would reach decrypt(ciphertext, "") and throw on read.
Also short-circuit when either IV is empty, treating the malformed row as having no
credentials.
updateApplicationPasswordCredentials and its URL-keyed variant were
verified only against a mocked SiteSqlUtils, so their ContentValues
mapping never ran. A .first/.second swap, a wrong column, or a dropped
IV would ship uncaught — and the new blank-IV decrypt guard would mask
it as 'no credentials' rather than surfacing it.

EncryptionUtils can't run under Robolectric (AndroidKeyStore), so add a
stubbed-EncryptionUtils SiteSqlUtils and assert the column mapping via
the raw storedSite() read, plus a decrypt round-trip and the ORIGIN_WPAPI
URL-scoping (writes the matching row, leaves a same-url non-WPAPI row
untouched).
UPDATE_APPLICATION_PASSWORD, the WPAPI app-password fetch, and
app-password XML-RPC login in createOrUpdateSites each duplicated the
read-plain / null-guard / write-credentials-then-wpApiRestUrl logic, with
subtle divergences (rowsAffected reassigned vs not; the URL write nested
under the credentials check in one path, independent in another).

Extract persistAppPasswordColumns(site, persistCredentials,
persistWpApiRestUrl); each caller passes its targeted writer pair (local
id or URL-keyed). wpApiRestUrl stays gated behind credentials on purpose:
a credential-less /me/sites model can be a WP.com simple site whose
getWpApiRestUrl() synthesizes a public-api proxy URL, and gating keeps
that synthetic value out of the DB.

removeApplicationPassword now sums both clear-writes instead of returning
only the wpApiRestUrl count.
updateApplicationPasswordCredentials and clearApplicationPasswordCredentials
hand-wrote the same four-column ContentValues, so the column set lived in
two places — a future column change has to touch both or clear leaves a
stale column. Route both through one private writeApplicationPasswordCredentialColumns.

ApplicationPasswordViewModelSliceTest: the xmlRpc-rediscovery-success
assertion was pre-satisfied by @before seeding siteTest.xmlRpcUrl to the
same value; reset it to null first so the assertion proves rediscovery
assigned it.
@jkmassel jkmassel force-pushed the jkmassel/sitesqlutils-partial-updates branch from 88e8833 to fb89734 Compare June 13, 2026 15:26
@jkmassel jkmassel enabled auto-merge (squash) June 13, 2026 15:26
@jkmassel jkmassel merged commit 1ba5e7a into trunk Jun 13, 2026
19 of 22 checks passed
@jkmassel jkmassel deleted the jkmassel/sitesqlutils-partial-updates branch June 13, 2026 15:40
jkmassel added a commit that referenced this pull request Jun 13, 2026
…riter

#22947 made the app-password columns single-writer — excluded from the generic full-row update,
written only by updateApplicationPasswordCredentials — so a fresh getSiteByLocalId after a mint
reliably carries the credentials and a concurrent site write can't clobber them (#22905). The pipeline
no longer needs to thread them as a value: remove ProvisionedCredentials, AuthResult.credentials, and
the detectCapabilities overlay. ensureAuth returns a bare SiteAuthState; detectCapabilities and
recoverXmlRpcIfNeeded read the credentials off the fresh read.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recovered wpApiRestUrl doesn't survive across launches on Atomic sites

5 participants