Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions AGENTS.md

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ Safety Net records every email it blocks or lets through on the **Email Log** pa
- The list shows 20 entries per page, newest first, with links that filter it by result (All, Would send, Would fail, Sent, Failed, each with its count) and a search box that finds text in the recipients, the subject or the message.
- **View** opens an entry in a popup (or on its own page without JavaScript), with three views of the message: **HTML** (or **Text** for a plain-text email), **Raw**, the MIME source rebuilt from the entry the way `wp_mail()` would build it, with the logged date and without the attachments, and **JSON**, the whole entry.
- Two switches on **Email Log > Settings**, both on until someone turns them off: **Log emails**, and **Delete old entries**, which deletes entries older than 6 months once a day. Turning logging off keeps the entries already logged, and they are still deleted after 6 months while the second switch is on. While logging is off, the log says so and links to Settings.
- Before a message is saved, the value of every `key=` and `confirm_key=` parameter in its links, such as the key in password reset, new account and privacy request confirmation links, is replaced with `[redacted]`. The subject is saved as it is, and so are other secrets in a message, such as the password in WordPress's multisite welcome emails or passwords some plugins send.
- HTML messages, WooCommerce's multipart emails among them, are shown in a sandboxed frame that runs no scripts and loads no remote images. Raw shows the redacted message too. **Send to me** sends a copy of an entry to your own address only, and only if it is an administrator's, with the logged sender and content type (HTML for multipart emails), the redacted message and no attachments, Cc, Bcc or Reply-To. If a plugin adds another recipient who is not an administrator, the copy is not sent. Entries can be deleted one by one, or all at once with **Clear log**.
- Before a message is saved, the value of every `key=` and `confirm_key=` parameter in its links, such as the key in password reset, new account and privacy request confirmation links, is replaced with `[redacted]`. The keys are saved apart from the message, only so that **Send to me** can put them back (see below): the list, the search, the entry and its HTML, Raw and JSON views never show them, and searching for a key, or the start of one, finds nothing, but query logs, such as Query Monitor's, can show them when they are saved. They are deleted with their entry. The subject is saved as it is, and so are other secrets in a message, such as the password in WordPress's multisite welcome emails or passwords some plugins send.
- HTML messages, WooCommerce's multipart emails among them, are shown in a sandboxed frame that runs no scripts and loads no remote images. Raw shows the redacted message too. **Send to me** sends a copy of an entry to your own address only, and only if it is an administrator's, with the logged sender and content type (HTML for multipart emails), the message and no attachments, Cc, Bcc or Reply-To. If a plugin adds another recipient who is not an administrator, the copy is not sent. Entries can be deleted one by one, or all at once with **Clear log**.
- For users who can edit users on a single site (administrators by default) and super admins on multisite, who by default can set any user's password anyway, the copy has the keys put back, so its password reset, new account and privacy confirmation links work, for example to test a flow for a test customer. WordPress rejects keys after 24 hours by default, and a newer request replaces a user's reset key, so links in copies of older entries no longer work. Everyone else gets the copy with the keys redacted, and so does everyone for entries logged by Safety Net 1.11.0, which did not save the keys. The entry says which copy you would get.
- The keys of an email to an administrator, or to another staff account (a user who can edit content, from contributors up, or manage the store, such as WooCommerce's shop managers), in its To, Cc or Bcc, are never saved, so its copies keep them redacted. Anyone can ask for such a user's password reset on the login page, so a saved key would let whoever can read the database (through a vulnerable plugin, a backup or a copy of the database) take over that account until the key expires. Administrators get their reset emails themselves. Keys are saved for customers' and subscribers' emails, which is what testing their flows needs. A copy with its keys put back is an ordinary email: plugins that log sent emails, such as SMTP plugins' email logs, record it with its keys.
- The log is emptied whenever Safety Net deletes users and orders, on the first load, with the Delete button in Tools > Safety Net or with `wp safety-net delete`, because it can hold customers' emails while `SAFETY_NET_DELETE_DATA` keeps data. The keep step leaves it alone.
- Only emails sent through `wp_mail()` are logged, the same ones Safety Net can block.
- The log is the `{prefix}safety_net_email_log` table, one per site on multisite (deleting a site drops its table), created when the first email is logged. When Safety Net is removed, the table stays in the database, and so do the `safety_net_email_log_*` options and the daily `safety_net_email_log_cleanup` WP-Cron event, which then stays scheduled with nothing to run. All of them go with the database when a staging site is pushed to production, where Safety Net does nothing, so the log is never cleaned up there. Clear the log before pushing to production.
Expand Down
40 changes: 38 additions & 2 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,45 @@ function stop_emails( $return, $args ) {
* @return bool
*/
function is_sent_only_to_admins( array $args ): bool {
$recipients = get_all_recipients( $args );

return $recipients && ! array_diff( $recipients, get_admin_emails() );
}

/**
* Whether any To, Cc or Bcc recipient of an email is an administrator, or a user who can edit content or manage the store.
*
* @param array $args The wp_mail() arguments.
*
* @return bool
*/
function is_sent_to_staff( array $args ): bool {
$recipients = get_all_recipients( $args );
if ( array_intersect( $recipients, get_admin_emails() ) ) {
return true;
}

foreach ( $recipients as $address ) {
$user = get_user_by( 'email', $address );
if ( $user && ( user_can( $user, 'edit_posts' ) || user_can( $user, 'manage_woocommerce' ) ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown -- WooCommerce's capability.
return true;
}
}

return false;
}

/**
* Returns the lowercased addresses of every To, Cc and Bcc recipient of an email.
*
* @param array $args The wp_mail() arguments.
*
* @return string[]
*/
function get_all_recipients( array $args ): array {
$recipients = get_mail_recipients( $args['to'] ?? '' );

// Split as wp_mail() does, so every Cc and Bcc it would send to is checked.
// Split as wp_mail() does, so every Cc and Bcc it would send to is counted.
$headers = $args['headers'] ?? array();
foreach ( is_array( $headers ) ? $headers : explode( "\n", str_replace( "\r\n", "\n", (string) $headers ) ) as $header ) {
if ( ! str_contains( (string) $header, ':' ) ) {
Expand All @@ -832,7 +868,7 @@ function is_sent_only_to_admins( array $args ): bool {
}
}

return $recipients && ! array_diff( $recipients, get_admin_emails() );
return $recipients;
}

/**
Expand Down
Loading
Loading