Skip to content

Keep reset links working in Send to me copies - #219

Merged
Ninodevo merged 3 commits into
trunkfrom
add/126-email-log-reset-links
Oct 13, 2026
Merged

Ninodevo merged 3 commits into
trunkfrom
add/126-email-log-reset-links

Conversation

@Ninodevo

@Ninodevo Ninodevo commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

1.11.0 replaced every key= and confirm_key= link value with [redacted] before saving the email log entry. A Send to me copy of a password reset, a "set your password" email or a privacy request confirmation therefore had a dead link, and flows for test customers could not be tested from a copy.

  • Still masked everywhere visible: the message is stored masked as before. The list, search, popup HTML/Text, Raw, JSON and the AJAX fragment never show a key, and search still runs on the masked message, so it can't be used to guess a key.
  • Keys kept apart: the original values are stored in order in a new secrets column, written by a separate UPDATE after the insert. That keeps them out of SQLite's error text, which ends up in debug.log. They are never shown, searched or logged.
  • Keys put back only for some users: Send to me restores them only for users who by default can set any user's password already: edit_users on a single site, and super admins on multisite, where delegated manage_network_users would otherwise pass edit_users. Restoring happens only when every placeholder lines up with a stored key; otherwise the copy stays masked. The entry view says whether the copy's links will work and why not. A new sent-with-keys notice confirms when keys were put back.
  • No keys for staff accounts: keys are never stored for emails with an administrator, or a user who can edit_posts or manage_woocommerce (editors, shop managers and the like), in To, Cc or Bcc. Anyone can request an administrator's password reset from the login page, and Safety Net lets it through to administrators, and requests for other staff accounts work the same way, so a stored key would let anyone who can read the database take over that account within 24 hours. Administrators got the real email. Customers' and subscribers' emails keep their keys, which is what testing their flows needs.
  • Migration: DB_VERSION 2. A 1.11.0 table gets ALTER TABLE … ADD COLUMN secrets text, nullable so it is SQLite-portable. If the column is already there, the upgrade counts as done, and a newer version is never lowered. If the ALTER fails, it isn't retried in that request, one line is logged, and emails are logged without keys. A 1.11.0 table whose option was lost is detected after CREATE TABLE IF NOT EXISTS.
  • README and AGENTS.md updated.

Refs #126.

Notes

  • Entries logged by 1.11.0 have no stored keys, so their copies stay masked.
  • Keys expire after 24 hours (core's default), so links restored from older entries are rejected by WordPress.
  • A copy with its keys put back is an ordinary email, so mail and SMTP logging plugins on the site record it.
  • An email logged before its recipient became an administrator keeps its stored key until the entry is deleted. The log's 6-month cleanup and the wipe when users and orders are deleted still apply.

Test plan

  • E34: real keys from core's retrieve_password() for a test customer, a WooCommerce-style &key= HTML link and core's wp_send_user_request() (confirm_key=). Checks:
    • the list, popup, Raw, JSON and search (full keys and 6-character prefixes) never show them;
    • an administrator's copy equals the original message, and check_password_reset_key() / wp_validate_user_request_key() accept the restored keys;
    • a user without edit_users gets a masked copy;
    • mismatches stay masked.
  • E34b: a real administrator reset, and administrators in To, Cc and Bcc (uppercase, Name <addr>), store no keys and their copies stay masked. A customer-only control stores and restores its key.
  • E34c: an editor, a contributor and a store manager get no stored keys; a subscriber does.
  • M15 (multisite): a site administrator given manage_network_users gets a masked copy; a super admin gets the key back.
  • E3d–E3g:
    • an upgrade from an exact 1.11.0 table with old rows;
    • the column added concurrently, a lost option with an old table, an old option with the table gone, and a newer version never lowered;
    • the ALTER refused;
    • a refused insert or key write never puts a key in debug.log.
  • Full suite on PHP 8.3: 276 passed, 0 failed. phpcs: no new messages compared with trunk.

1.11.0 replaced every key= and confirm_key= link value with [redacted]
before saving, so a copy of a password reset, "set your password" or
privacy confirmation email had a dead link, and flows for test customers
could not be tested from a copy.

- The message is still stored masked, so the list, search, popup, Raw
  and JSON never show a key. The original values are stored apart, in a
  new secrets column, and are never shown, searched or logged.
- Send to me puts them back, in order, only for users who can already
  set any user's password: edit_users on a single site, super admins on
  multisite. Any mismatch sends the masked copy.
- Keys are never stored for emails to administrators: anyone can request
  an administrator's reset, which Safety Net lets through, and the
  administrators got the email itself.
- DB_VERSION 2 adds the column to 1.11.0 tables with ALTER TABLE, once,
  and logs without keys if that fails. Old entries stay masked.

Refs #126.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ninodevo Ninodevo mentioned this pull request Oct 13, 2026
5 tasks done
@t51eng-poseidon

t51eng-poseidon Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🔱 Poseidon started reviewing this PR — central run.

Comment thread includes/email-log.php Outdated
);

// Anyone can request an administrator's password reset, so a stored key would let whoever reads the database take over that account.
$secrets = $has_secrets && $keys && ! is_sent_to_an_admin( $args ) ? wp_json_encode( $keys ) : false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new guard ! is_sent_to_an_admin( $args ) keeps keys out of the table only for administrators, so reset and set-password keys for other privileged accounts are still stored, e.g. WooCommerce's shop_manager (which can manage orders and edit customers) or an editor created on the staging site. Anyone can request a reset for those users from the login page too, so the threat this guard is written for applies to them in the same way: whoever can read the database (a vulnerable plugin, a backup, a copy) can take over that account until the key expires. Consider storing keys only when no recipient has elevated capabilities, e.g. by checking each recipient's user with user_can( $user, 'edit_posts' ) or 'manage_woocommerce' next to the administrator check. Or narrow the README's guarantee so it says that only administrators' keys are left out.

🤖 Fix prompt for AI agents
Verify each finding against the current code and only fix it if
needed.

In `includes/email-log.php`:
- Line 288: The assignment `$secrets = $has_secrets && $keys &&
  ! is_sent_to_an_admin( $args ) ? wp_json_encode( $keys ) : false;`
  in `log_email()` skips storing keys only when a To, Cc or Bcc
  recipient is an administrator, so reset keys for other privileged
  users (WooCommerce shop_manager, editor, any role with edit_users or
  manage_woocommerce created on staging) are still saved in the
  secrets column; anyone can request their reset on the login page,
  so a database reader can take over those accounts until the key
  expires. Extend the check, e.g. a helper next to
  `is_sent_to_an_admin()` in includes/admin.php that looks up each
  address from `get_all_recipients()` with `get_user_by( 'email' )`
  and returns true when the user has elevated capabilities. Keep the
  existing administrator check, the TEXT_BYTES length guard and the
  separate UPDATE after the insert unchanged. If you keep the current
  behaviour on purpose, update README.md so it says that only
  administrators' keys are left out.

@t51eng-poseidon t51eng-poseidon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔱 Poseidon Review

Summary: Saves the key= and confirm_key= values apart from the redacted message, in a new secrets column, so Send to me can put them back for users who can already set passwords. It also adds a DB_VERSION 2 migration. Risk is low: every path I traced keeps the keys out of the list, the search, JSON, Raw and the AJAX output, and the migration fails closed.

Walkthrough

  • ✨ New: secrets column (DB_VERSION 2) and restore_keys()/can_restore_keys(), so Send to me copies can have working reset and confirmation links; a sent-with-keys notice and a per-entry copy_note().
  • ♻️ Refactor: get_all_recipients() is pulled out of is_sent_only_to_admins() and shared with the new is_sent_to_an_admin(); KEY_PATTERN and REDACTED constants.
  • 🧪 Tests: E34/E34b key storage and restore, E3d–E3g upgrade and failure paths, M15 on multisite.
  • 📝 Docs: README and AGENTS.md describe the stored keys, who gets them back and the migration.

🟡 Suggestions

  • includes/email-log.php:L288: Only administrator recipients are kept out of key storage, so reset keys for other privileged users created on staging (WooCommerce shop_manager, editor) are stored and can be read from the database. Those users face the same takeover risk the guard is written for. Extend the check to other elevated capabilities, or narrow the README's guarantee so it covers administrators only.

🤖 Prompt for AI agents — fix all findings
Verify each finding against the current code and only fix it if
needed.

Suggestions:
In `includes/email-log.php`:
- Line 288: The assignment `$secrets = $has_secrets && $keys &&
  ! is_sent_to_an_admin( $args ) ? wp_json_encode( $keys ) : false;`
  in `log_email()` skips storing keys only when a To, Cc or Bcc
  recipient is an administrator, so reset keys for other privileged
  users (WooCommerce shop_manager, editor, any role with edit_users or
  manage_woocommerce created on staging) are still saved in the
  secrets column; anyone can request their reset on the login page,
  so a database reader can take over those accounts until the key
  expires. Extend the check, e.g. a helper next to
  `is_sent_to_an_admin()` in includes/admin.php that looks up each
  address from `get_all_recipients()` with `get_user_by( 'email' )`
  and returns true when the user has elevated capabilities. Keep the
  existing administrator check, the TEXT_BYTES length guard and the
  separate UPDATE after the insert unchanged. If you keep the current
  behaviour on purpose, update README.md so it says that only
  administrators' keys are left out.

ℹ️ Review info
  • Commit: bdbf81e
  • Files reviewed: includes/email-log.php, includes/admin.php, README.md, AGENTS.md
  • Files skipped: tests/smoke/fixtures/mu-plugins/00-sn-test-helper.php, tests/smoke/fixtures/php/seed.php, tests/smoke/scenarios.mjs, tests/smoke/scenarios/email-log.test.mjs, tests/smoke/scenarios/multisite-main-first.test.mjs (test fixtures and scenarios, not shipped; skimmed only for what they cover)
  • Model: claude-opus-5-5

Keys were left out only for administrators. Anyone can request a
password reset for an editor or a shop manager created on the staging
site too, so a stored key would let a database reader take over those
accounts until it expires. Keys are now left out when any recipient is
an administrator or can edit_posts or manage_woocommerce; customers'
and subscribers' emails still keep them.

Refs #126.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@t51eng-poseidon t51eng-poseidon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔱 Poseidon Review

Summary: Stores key= and confirm_key= values apart from the redacted message so Send to me can restore them for users who can already set passwords. The latest push extends the no-store rule from administrators to every staff account (edit_posts or manage_woocommerce). Risk is low; the only things left are two small wording issues.

Walkthrough

  • ✨ New: secrets column (DB_VERSION 2), restore_keys()/can_restore_keys(), a sent-with-keys notice and a per-entry copy_note().
  • 🐛 Fix: is_sent_to_an_admin() → is_sent_to_staff(), so keys are no longer stored for emails to editors, contributors, authors or shop managers.
  • 🧪 Tests: E34/E34b/E34c key storage and restore, E3d–E3g upgrade and failure paths, M15 on multisite.
  • 📝 Docs: README and AGENTS.md describe the staff rule.

💡 Nitpicks (2)
  • includes/email-log.php:L1813: The copy_note() text says staff accounts "got the email itself". That is only true for administrators. While SAFETY_NET_DELETE_DATA keeps data, stop_emails() blocks a Password Reset whenever any recipient is not an administrator, so an editor or shop manager never received it. Reword it so it doesn't claim delivery for non-admin staff.
  • includes/email-log.php:L287: The comment above the $secrets assignment still mentions only "an administrator's password reset", but the guard now covers every staff account. Update it to match is_sent_to_staff().

🤖 Prompt for AI agents — fix all findings
Verify each finding against the current code and only fix it if
needed.

Nitpicks:
In `includes/email-log.php`:
- Line 1813: In `copy_note()`, the string returned when
  `null === $restored && is_entry_to_staff( $entry )` ends with "keys
  are not stored for emails to administrators or other staff
  accounts, who got the email itself." Only administrators are
  guaranteed delivery: `stop_emails()` in includes/admin.php blocks a
  Password Reset with status `blocked_recipients` whenever data is kept
  and any recipient is not an administrator. Editors and shop managers
  therefore never got the email, and the note misleads the admin
  viewing the entry. Reword the translatable string so it only says
  keys are not stored for staff accounts and drops the delivery claim
  for non-admins (for example, "…because anyone can request a staff
  account's reset"). Keep the condition, the text domain and the
  surrounding note structure unchanged.
- Line 287: The comment above `$secrets = $has_secrets && $keys &&
  ! is_sent_to_staff( $args ) ? wp_json_encode( $keys ) : false;` in
  `log_email()` still says "Anyone can request an administrator's
  password reset". The guard now also covers users who can edit_posts
  or manage_woocommerce, so the comment understates what it protects.
  Update it to say staff accounts (administrators, content editors and
  store managers). Leave the expression and the TEXT_BYTES guard below
  it unchanged.

♻️ Previously flagged

Resolved

  • ✅ includes/email-log.php:L288: now uses is_sent_to_staff(), which also skips storing keys for edit_posts/manage_woocommerce users; README updated to match

ℹ️ Review info
  • Commit: 3ab2433
  • Files reviewed: includes/email-log.php, includes/admin.php, README.md, AGENTS.md
  • Files skipped: tests/smoke/fixtures/mu-plugins/00-sn-test-helper.php, tests/smoke/fixtures/php/seed.php, tests/smoke/scenarios.mjs, tests/smoke/scenarios/email-log.test.mjs, tests/smoke/scenarios/multisite-main-first.test.mjs (test fixtures and scenarios, not shipped; skimmed only)
  • Model: claude-opus-5-5

Only administrators get their own reset emails; Safety Net blocks the
others, so the entry's note and the comments now give the real reason:
anyone can request a staff account's password reset.

Refs #126.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@t51eng-poseidon t51eng-poseidon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔱 Poseidon Review

Summary — The latest push only rewords the copy_note() string, the $secrets comment, the AGENTS.md text and the matching test constant, so they no longer claim that non-admin staff received the email. Both nitpicks from the last pass are closed. Risk is low and nothing new turned up.

Walkthrough

  • 📝 Docs: The copy_note() text and the comment in log_email() now say keys are left out because anyone can request a staff account's password reset. They no longer claim that staff got the email.
  • 🧪 Tests: KEYS_FOR_ADMINS in email-log.test.mjs is updated to the new string.

💡 Nitpicks (0) · Deferred (1)

Deferred improvements

  • AGENTS.md:L105: Still says "for an entry to an administrator (is_entry_to_staff() …)". The helper now covers every staff account, so this could say "administrator or other staff account". This line has not changed since the prior review.

♻️ Previously flagged

Resolved

  • ✅ includes/email-log.php:L1813: The copy_note() string now ends "since anyone can request their password reset". The delivery claim is gone.
  • ✅ includes/email-log.php:L287: The comment now says "a staff account's password reset", which matches is_sent_to_staff().

ℹ️ Review info
  • Commit: f16de50
  • Files reviewed: includes/email-log.php, includes/admin.php, AGENTS.md, README.md, tests/smoke/scenarios/email-log.test.mjs
  • Files skipped: tests/smoke/fixtures/mu-plugins/00-sn-test-helper.php, tests/smoke/fixtures/php/seed.php, tests/smoke/scenarios.mjs, tests/smoke/scenarios/multisite-main-first.test.mjs (test fixtures and scenarios with no changes since the prior review; not shipped)
  • Model: claude-opus-5-5

@Ninodevo
Ninodevo merged commit 209ed6a into trunk Oct 13, 2026
20 checks passed
Ninodevo added a commit that referenced this pull request Oct 13, 2026
Resolves the overlap with #219: log_email() keeps both the secrets column
and the allowed_switch status, the docs combine both changes, and ST6
expects the email log's table version 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant