This commit is contained in:
Javier Casares 2026-06-09 12:48:19 +00:00
commit ed5c696fbe

View file

@ -216,7 +216,7 @@ class Plugin {
*/
public function filter_mail_from( string $original_email ): string {
$settings = $this->get_mailer_settings();
$from_email = isset( $settings['from_email'] ) ? sanitize_email( $settings['from_email'] ) : '';
$from_email = isset( $settings['from_email'] ) ? sanitize_email( is_string( $settings['from_email'] ) ? $settings['from_email'] : '' ) : '';
if ( empty( $from_email ) ) {
return sanitize_email( $original_email );
@ -234,7 +234,7 @@ class Plugin {
*/
public function filter_mail_from_name( string $original_name ): string {
$settings = $this->get_mailer_settings();
$from_name = isset( $settings['from_name'] ) ? sanitize_text_field( $settings['from_name'] ) : '';
$from_name = isset( $settings['from_name'] ) ? sanitize_text_field( is_string( $settings['from_name'] ) ? $settings['from_name'] : '' ) : '';
if ( '' === $from_name ) {
return sanitize_text_field( $original_name );
@ -256,25 +256,25 @@ class Plugin {
*/
public function configure_reply_to( PHPMailer $phpmailer ): void {
$settings = $this->get_mailer_settings();
// Check if reply-to email is configured.
$reply_to_email = isset( $settings['reply_to_email'] ) ? sanitize_email( $settings['reply_to_email'] ) : '';
$reply_to_email = isset( $settings['reply_to_email'] ) ? sanitize_email( is_string( $settings['reply_to_email'] ) ? $settings['reply_to_email'] : '' ) : '';
// If no reply-to email configured, nothing to do.
if ( empty( $reply_to_email ) || ! is_email( $reply_to_email ) ) {
return;
}
// Check if Reply-To header already exists.
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( ! empty( $phpmailer->getReplyToAddresses() ) ) {
// Reply-To already set by wp_mail() or another plugin, don't override.
return;
}
// Get reply-to name (optional).
$reply_to_name = isset( $settings['reply_to_name'] ) ? sanitize_text_field( $settings['reply_to_name'] ) : '';
$reply_to_name = isset( $settings['reply_to_name'] ) ? sanitize_text_field( is_string( $settings['reply_to_name'] ) ? $settings['reply_to_name'] : '' ) : '';
// Add the Reply-To header.
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$phpmailer->addReplyTo( $reply_to_email, $reply_to_name );
@ -300,21 +300,9 @@ class Plugin {
$settings = wp_parse_args( $settings, $defaults );
$settings['password'] = \Robotstxt_SMTP_Encryption::decrypt( $settings['password'] ?? '' );
$settings['amazon_ses_access_key'] = \Robotstxt_SMTP_Encryption::decrypt( $settings['amazon_ses_access_key'] ?? '' );
$settings['amazon_ses_secret_key'] = \Robotstxt_SMTP_Encryption::decrypt( $settings['amazon_ses_secret_key'] ?? '' );
if ( ! is_string( $settings['password'] ) ) {
$settings['password'] = '';
}
if ( ! is_string( $settings['amazon_ses_access_key'] ) ) {
$settings['amazon_ses_access_key'] = '';
}
if ( ! is_string( $settings['amazon_ses_secret_key'] ) ) {
$settings['amazon_ses_secret_key'] = '';
}
$settings['password'] = \Robotstxt_SMTP_Encryption::decrypt( is_string( $settings['password'] ?? '' ) ? ( $settings['password'] ?? '' ) : '' );
$settings['amazon_ses_access_key'] = \Robotstxt_SMTP_Encryption::decrypt( is_string( $settings['amazon_ses_access_key'] ?? '' ) ? ( $settings['amazon_ses_access_key'] ?? '' ) : '' );
$settings['amazon_ses_secret_key'] = \Robotstxt_SMTP_Encryption::decrypt( is_string( $settings['amazon_ses_secret_key'] ?? '' ) ? ( $settings['amazon_ses_secret_key'] ?? '' ) : '' );
/**
* Filters the SMTP settings before they are consumed by the mailer.
@ -374,9 +362,10 @@ class Plugin {
}
// If the value is not encrypted and not empty, it needs migration.
if ( ! \Robotstxt_SMTP_Encryption::is_encrypted( $settings[ $field ] ) ) {
$needs_migration = true;
$settings[ $field ] = \Robotstxt_SMTP_Encryption::encrypt( $settings[ $field ] );
$field_value = is_string( $settings[ $field ] ) ? $settings[ $field ] : '';
if ( ! \Robotstxt_SMTP_Encryption::is_encrypted( $field_value ) ) {
$needs_migration = true;
$settings[ $field ] = \Robotstxt_SMTP_Encryption::encrypt( $field_value );
}
}
@ -436,7 +425,7 @@ class Plugin {
}
// Check if there are any credentials stored.
$has_credentials = false;
$has_credentials = false;
$credential_fields = array( 'password', 'amazon_ses_access_key', 'amazon_ses_secret_key' );
foreach ( $credential_fields as $field ) {
if ( ! empty( $settings[ $field ] ) ) {
@ -476,7 +465,7 @@ class Plugin {
$dismiss_url = add_query_arg(
array(
'robotstxt_smtp_dismiss_encryption_notice' => '1',
'_wpnonce' => wp_create_nonce( 'robotstxt_smtp_dismiss_encryption_notice' ),
'_wpnonce' => wp_create_nonce( 'robotstxt_smtp_dismiss_encryption_notice' ),
)
);
@ -573,11 +562,11 @@ class Plugin {
$phpmailer->isSMTP();
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$phpmailer->Host = $settings['host'];
$phpmailer->Port = (int) $settings['port'];
$phpmailer->Host = is_string( $settings['host'] ) ? $settings['host'] : '';
$phpmailer->Port = isset( $settings['port'] ) && is_numeric( $settings['port'] ) ? (int) $settings['port'] : 25;
$phpmailer->SMTPAuth = ! empty( $settings['username'] ) || ! empty( $settings['password'] );
$phpmailer->Username = $phpmailer->SMTPAuth ? $settings['username'] : '';
$phpmailer->Password = $phpmailer->SMTPAuth ? $settings['password'] : '';
$phpmailer->Username = $phpmailer->SMTPAuth ? ( is_string( $settings['username'] ) ? $settings['username'] : '' ) : '';
$phpmailer->Password = $phpmailer->SMTPAuth ? ( is_string( $settings['password'] ) ? $settings['password'] : '' ) : '';
$phpmailer->SMTPAutoTLS = false;
$phpmailer->SMTPSecure = '';
// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
@ -640,8 +629,8 @@ class Plugin {
$settings = $this->get_mailer_settings();
$subject = isset( $mail_data['subject'] ) ? sanitize_text_field( (string) $mail_data['subject'] ) : '';
$message = isset( $mail_data['message'] ) ? (string) $mail_data['message'] : '';
$subject = isset( $mail_data['subject'] ) ? sanitize_text_field( is_string( $mail_data['subject'] ) ? $mail_data['subject'] : '' ) : '';
$message = isset( $mail_data['message'] ) ? ( is_string( $mail_data['message'] ) ? $mail_data['message'] : '' ) : '';
$headers = $this->normalize_headers( $mail_data['headers'] ?? array() );
$to = $this->normalize_recipients( $mail_data['to'] ?? array() );
$from = $this->determine_from_header( $headers, $settings );
@ -657,7 +646,7 @@ class Plugin {
true
);
if ( is_wp_error( $post_id ) || 0 === $post_id ) {
if ( is_wp_error( $post_id ) ) {
return;
}
@ -706,8 +695,8 @@ class Plugin {
$data = array();
}
$subject = isset( $data['subject'] ) ? sanitize_text_field( (string) $data['subject'] ) : '';
$message = isset( $data['message'] ) ? (string) $data['message'] : '';
$subject = isset( $data['subject'] ) ? sanitize_text_field( is_string( $data['subject'] ) ? $data['subject'] : '' ) : '';
$message = isset( $data['message'] ) ? ( is_string( $data['message'] ) ? $data['message'] : '' ) : '';
$headers = $this->normalize_headers( $data['headers'] ?? array() );
$to = $this->normalize_recipients( $data['to'] ?? array() );
$from = $this->determine_from_header( $headers, $settings );
@ -724,7 +713,7 @@ class Plugin {
true
);
if ( is_wp_error( $post_id ) || 0 === $post_id ) {
if ( is_wp_error( $post_id ) ) {
return;
}
@ -776,7 +765,7 @@ class Plugin {
'/\bAUTH\s+PLAIN\s+[A-Za-z0-9+\/=]+/i',
'AUTH PLAIN [REDACTED]',
$message
);
) ?? $message;
// Redact AUTH LOGIN command with base64 credentials.
// Matches AUTH LOGIN anywhere in line.
@ -784,7 +773,7 @@ class Plugin {
'/\bAUTH\s+LOGIN\s+[A-Za-z0-9+\/=]+/i',
'AUTH LOGIN [REDACTED]',
$message
);
) ?? $message;
// Redact other AUTH mechanisms with credentials.
// Handles CRAM-MD5, DIGEST-MD5, XOAUTH2, NTLM, GSSAPI, EXTERNAL.
@ -792,7 +781,7 @@ class Plugin {
'/\bAUTH\s+(CRAM-MD5|DIGEST-MD5|XOAUTH2|NTLM|GSSAPI|EXTERNAL)\s+[A-Za-z0-9+\/=\-_\.]+/i',
'AUTH ${1} [REDACTED]',
$message
);
) ?? $message;
// Redact base64 challenge-response (server sends 334 + base64).
// The 334 code indicates server is awaiting authentication data.
@ -800,7 +789,7 @@ class Plugin {
'/\b334\s+[A-Za-z0-9+\/=]+/',
'334 [CHALLENGE REDACTED]',
$message
);
) ?? $message;
// Redact standalone base64 strings (likely auth responses).
// Matches lines that are predominantly base64 (20+ chars).
@ -809,7 +798,7 @@ class Plugin {
'/^(\[\d+\]\s*)?([A-Za-z0-9+\/=]{20,})\s*$/mi',
'${1}[CREDENTIAL REDACTED]',
$message
);
) ?? $message;
return $message;
}
@ -819,7 +808,7 @@ class Plugin {
*
* @since 1.0.0
*
* @return array Sanitized debug log entries with credentials redacted.
* @return list<string> Sanitized debug log entries with credentials redacted.
*/
private function get_sanitized_debug_log(): array {
if ( empty( $this->current_debug_log ) ) {
@ -871,7 +860,7 @@ class Plugin {
*/
public function cleanup_stats(): void {
$settings = $this->get_mailer_settings();
$days = isset( $settings['stats_retention_days'] ) ? absint( $settings['stats_retention_days'] ) : 28;
$days = isset( $settings['stats_retention_days'] ) && is_numeric( $settings['stats_retention_days'] ) ? absint( $settings['stats_retention_days'] ) : 28;
if ( $days <= 0 ) {
return;
@ -994,7 +983,7 @@ class Plugin {
$normalized = array();
foreach ( $headers as $header ) {
$line = trim( (string) $header );
$line = trim( is_string( $header ) ? $header : '' );
if ( '' !== $line ) {
$normalized[] = $line;
@ -1027,7 +1016,7 @@ class Plugin {
$normalized = array();
foreach ( $recipients as $recipient ) {
$normalized[] = sanitize_text_field( trim( (string) $recipient ) );
$normalized[] = sanitize_text_field( trim( is_string( $recipient ) ? $recipient : '' ) );
}
return array_filter( $normalized );
@ -1048,8 +1037,8 @@ class Plugin {
}
}
$from_email = isset( $settings['from_email'] ) ? sanitize_email( $settings['from_email'] ) : '';
$from_name = isset( $settings['from_name'] ) ? sanitize_text_field( $settings['from_name'] ) : '';
$from_email = isset( $settings['from_email'] ) ? sanitize_email( is_string( $settings['from_email'] ) ? $settings['from_email'] : '' ) : '';
$from_name = isset( $settings['from_name'] ) ? sanitize_text_field( is_string( $settings['from_name'] ) ? $settings['from_name'] : '' ) : '';
if ( $from_email && $from_name ) {
return $from_name . ' <' . $from_email . '>';
@ -1065,7 +1054,7 @@ class Plugin {
/**
* Retrieves the logging-related settings from the option store.
*
* @return array<string, mixed>
* @return array{enabled: bool, mode: string, count: int, days: int}
*/
private function get_logging_settings(): array {
$settings = $this->get_mailer_settings();
@ -1073,10 +1062,10 @@ class Plugin {
return array(
'enabled' => ! empty( $settings['logs_enabled'] ),
'mode' => in_array( $settings['logs_retention_mode'], array( 'count', 'days' ), true )
? $settings['logs_retention_mode']
? (string) $settings['logs_retention_mode']
: 'count',
'count' => isset( $settings['logs_retention_count'] ) ? (int) $settings['logs_retention_count'] : 0,
'days' => isset( $settings['logs_retention_days'] ) ? (int) $settings['logs_retention_days'] : 0,
'count' => isset( $settings['logs_retention_count'] ) && is_numeric( $settings['logs_retention_count'] ) ? (int) $settings['logs_retention_count'] : 0,
'days' => isset( $settings['logs_retention_days'] ) && is_numeric( $settings['logs_retention_days'] ) ? (int) $settings['logs_retention_days'] : 0,
);
}
@ -1093,7 +1082,8 @@ class Plugin {
$scope = $this->settings_page ? $this->settings_page->get_scope() : Settings_Page::SCOPE_SITE;
$key_second = $this->get_rate_limit_transient_key( $scope, 'second' );
$count = (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key_second ) : get_transient( $key_second ) );
$raw_second = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key_second ) : get_transient( $key_second );
$count = is_numeric( $raw_second ) ? (int) $raw_second : 0;
++$count;
if ( Settings_Page::SCOPE_NETWORK === $scope ) {
set_site_transient( $key_second, $count, 1 );
@ -1102,7 +1092,8 @@ class Plugin {
}
$key_hour = $this->get_rate_limit_transient_key( $scope, 'hour' );
$count = (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key_hour ) : get_transient( $key_hour ) );
$raw_hour = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key_hour ) : get_transient( $key_hour );
$count = is_numeric( $raw_hour ) ? (int) $raw_hour : 0;
++$count;
if ( Settings_Page::SCOPE_NETWORK === $scope ) {
set_site_transient( $key_hour, $count, HOUR_IN_SECONDS );
@ -1111,7 +1102,8 @@ class Plugin {
}
$key_day = $this->get_rate_limit_transient_key( $scope, 'day' );
$count = (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key_day ) : get_transient( $key_day ) );
$raw_day = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key_day ) : get_transient( $key_day );
$count = is_numeric( $raw_day ) ? (int) $raw_day : 0;
++$count;
if ( Settings_Page::SCOPE_NETWORK === $scope ) {
set_site_transient( $key_day, $count, DAY_IN_SECONDS );
@ -1135,49 +1127,55 @@ class Plugin {
$settings = $this->settings_page->get_settings();
$scope = $this->settings_page->get_scope();
if ( isset( $settings['rate_limit_per_second'] ) && $settings['rate_limit_per_second'] > 0 ) {
$key = $this->get_rate_limit_transient_key( $scope, 'second' );
$count = (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key ) : get_transient( $key ) );
if ( $settings['rate_limit_per_second'] > 0 ) {
$limit_second = (int) $settings['rate_limit_per_second'];
$key = $this->get_rate_limit_transient_key( $scope, 'second' );
$raw = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key ) : get_transient( $key );
$count = is_numeric( $raw ) ? (int) $raw : 0;
if ( $count >= $settings['rate_limit_per_second'] ) {
if ( $count >= $limit_second ) {
return new WP_Error(
'rate_limit_exceeded_second',
sprintf(
/* translators: %d: rate limit per second */
__( 'Rate limit exceeded: Maximum %d emails per second.', 'robotstxt-smtp' ),
$settings['rate_limit_per_second']
$limit_second
)
);
}
}
if ( isset( $settings['rate_limit_per_hour'] ) && $settings['rate_limit_per_hour'] > 0 ) {
$key = $this->get_rate_limit_transient_key( $scope, 'hour' );
$count = (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key ) : get_transient( $key ) );
if ( $settings['rate_limit_per_hour'] > 0 ) {
$limit_hour = (int) $settings['rate_limit_per_hour'];
$key = $this->get_rate_limit_transient_key( $scope, 'hour' );
$raw = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key ) : get_transient( $key );
$count = is_numeric( $raw ) ? (int) $raw : 0;
if ( $count >= $settings['rate_limit_per_hour'] ) {
if ( $count >= $limit_hour ) {
return new WP_Error(
'rate_limit_exceeded_hour',
sprintf(
/* translators: %d: rate limit per hour */
__( 'Rate limit exceeded: Maximum %d emails per hour.', 'robotstxt-smtp' ),
$settings['rate_limit_per_hour']
$limit_hour
)
);
}
}
if ( isset( $settings['rate_limit_per_day'] ) && $settings['rate_limit_per_day'] > 0 ) {
$key = $this->get_rate_limit_transient_key( $scope, 'day' );
$count = (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key ) : get_transient( $key ) );
if ( $settings['rate_limit_per_day'] > 0 ) {
$limit_day = (int) $settings['rate_limit_per_day'];
$key = $this->get_rate_limit_transient_key( $scope, 'day' );
$raw = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $key ) : get_transient( $key );
$count = is_numeric( $raw ) ? (int) $raw : 0;
if ( $count >= $settings['rate_limit_per_day'] ) {
if ( $count >= $limit_day ) {
return new WP_Error(
'rate_limit_exceeded_day',
sprintf(
/* translators: %d: rate limit per day */
__( 'Rate limit exceeded: Maximum %d emails per 24 hours.', 'robotstxt-smtp' ),
$settings['rate_limit_per_day']
$limit_day
)
);
}
@ -1223,10 +1221,14 @@ class Plugin {
$scope = $this->settings_page->get_scope();
$raw_second = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $this->get_rate_limit_transient_key( $scope, 'second' ) ) : get_transient( $this->get_rate_limit_transient_key( $scope, 'second' ) );
$raw_hour = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $this->get_rate_limit_transient_key( $scope, 'hour' ) ) : get_transient( $this->get_rate_limit_transient_key( $scope, 'hour' ) );
$raw_day = Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $this->get_rate_limit_transient_key( $scope, 'day' ) ) : get_transient( $this->get_rate_limit_transient_key( $scope, 'day' ) );
return array(
'second' => (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $this->get_rate_limit_transient_key( $scope, 'second' ) ) : get_transient( $this->get_rate_limit_transient_key( $scope, 'second' ) ) ),
'hour' => (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $this->get_rate_limit_transient_key( $scope, 'hour' ) ) : get_transient( $this->get_rate_limit_transient_key( $scope, 'hour' ) ) ),
'day' => (int) ( Settings_Page::SCOPE_NETWORK === $scope ? get_site_transient( $this->get_rate_limit_transient_key( $scope, 'day' ) ) : get_transient( $this->get_rate_limit_transient_key( $scope, 'day' ) ) ),
'second' => is_numeric( $raw_second ) ? (int) $raw_second : 0,
'hour' => is_numeric( $raw_hour ) ? (int) $raw_hour : 0,
'day' => is_numeric( $raw_day ) ? (int) $raw_day : 0,
);
}
@ -1237,23 +1239,25 @@ class Plugin {
*
* @since 1.2.1
*
* @param null|bool $return Short-circuit return value.
* @return null|bool|WP_Error Null to continue, WP_Error if rate limit exceeded.
*
* @phpcs:disable WordPress.NamingConventions.ValidFunctionName.ParameterNameInvalid -- WordPress core filter uses $return parameter name.
* @param null|bool $pre_wp_mail Short-circuit return value passed by the pre_wp_mail filter.
* @return null|bool Null to continue, false to abort (WP 5.7+ short-circuit).
*/
public function pre_wp_mail_rate_limit_check( $return ) {
if ( null !== $return ) {
return $return;
public function pre_wp_mail_rate_limit_check( $pre_wp_mail ) {
if ( null !== $pre_wp_mail ) {
return $pre_wp_mail;
}
$check = $this->check_rate_limit();
if ( is_wp_error( $check ) ) {
// Store error for the phpmailer_init hook (which fires after this filter).
// Return false to short-circuit wp_mail() immediately (WP 5.7+ behavior),
// avoiding the unnecessary PHPMailer instantiation and hook cascade.
$this->rate_limit_error = $check;
return false;
}
return $return;
return null;
}
/**
@ -1268,12 +1272,12 @@ class Plugin {
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Required by WordPress action hook signature.
*/
public function enforce_rate_limit_in_phpmailer( PHPMailer $phpmailer ): void {
if ( isset( $this->rate_limit_error ) && is_wp_error( $this->rate_limit_error ) ) {
if ( isset( $this->rate_limit_error ) ) {
$error = $this->rate_limit_error;
unset( $this->rate_limit_error );
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception message not output to browser.
throw new \PHPMailer\PHPMailer\Exception( $error->get_error_message(), $error->get_error_code() );
throw new \PHPMailer\PHPMailer\Exception( $error->get_error_message(), (int) $error->get_error_code() );
}
}
}