Security-only release per ADR-0001. Closes all six ship-blockers from the post-v1.5.0 audit: PSEC-001 (no rate limiting), PSEC-002 (account enumeration via separate error messages), PSEC-003 (registration auto-login with no email verification), PSEC-004 (ungated "My Data" templates), PSEC-005 (CSV formula injection on subscriber export), PSEC-006 (no honeypot/CAPTCHA on public forms). Adds inc/config.php (single source of truth for all security thresholds, filterable via peptide_starter_security_config), inc/rate-limiter.php (Peptide_Starter_Rate_Limiter + Cloudflare-aware client IP resolver, transient-backed, never stores raw IPs), and inc/email-verification.php (43-char token, 24h TTL, /verify rewrite, rate-limited resend endpoint). Auth handlers rewritten to: unify all failure messages, rate-limit before any expensive check, drop registrations with a filled honeypot silently, stop auto-login on register, send a verification email instead. The require_login() helper lives in inc/helpers.php and is applied at the top of page-tracker.php, page-subject-log.php, and page-protocol-builder.php; page-calculator.php stays open (no PII, no writes). Newsletter signup gains: consent checkbox, privacy link, honeypot, autoload=false on the wp_options blob, unsubscribe token stored per row, /newsletter-unsubscribe route, merged success+duplicate into a single "ok" state. Admin export runs every cell through peptide_starter_csv_safe() which prefixes leading =/+/-/@/tab/CR with a quote. Contact handler hardens against email-header injection (rejects name values with \r \n , < >), rate-limits, honors honeypot, tags mail failures with a request ID for correlation without logging message content. Adds Tools → Mail Test (inc/mail-diagnostic.php): a permanent wp_mail probe so we can check Hostinger outbound deliverability any time it looks flaky. Adds a one-time admin_init migration that enrols pre-v1.5.1 subscribers into the verification flow (currently a no-op — zero users — but guarded for the future). Other cleanup from the audit: fallback primary menu moved out of header.php into inc/helpers.php (redeclaration risk), placeholder footer language links replaced with "Translations coming soon", footer copyright now reads through the customizer getter, settings-panel gains a proper Tab/Shift+Tab focus trap and saves/restores body overflow, documentation heading IDs slugified from heading text with collision counters, JS files standardized on const/let. Six new PHPUnit suites cover rate limiter lifecycle, auth handler error unification and rate limits, email verification token generation and expiry, contact handler header injection + rate limit + honeypot, newsletter signup consent + CSV safety, and require_login gate behavior. ARCHITECTURE.md and CONVENTIONS.md updated in the same commit — new module layout, data flows, security patterns for rate-limiting, honeypotting, gating, and CSV-safe export. Version bumped to 1.5.1 in style.css and functions.php. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
|
/**
|
|
* Tests for newsletter signup handler + CSV-safe export helper.
|
|
*
|
|
* @package peptide-starter
|
|
*/
|
|
|
|
class Test_Newsletter extends WP_UnitTestCase {
|
|
|
|
public function tear_down() {
|
|
global $wpdb;
|
|
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_ps_rl_%'" );
|
|
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_ps_rl_%'" );
|
|
delete_option( 'ps_newsletter_emails' );
|
|
$_POST = array();
|
|
parent::tear_down();
|
|
}
|
|
|
|
public function test_csv_safe_prefixes_dangerous_leading_chars() {
|
|
$this->assertSame( "'=cmd|' /C calc'!A0", peptide_starter_csv_safe( "=cmd|' /C calc'!A0" ) );
|
|
$this->assertSame( "'+1234", peptide_starter_csv_safe( '+1234' ) );
|
|
$this->assertSame( "'-1234", peptide_starter_csv_safe( '-1234' ) );
|
|
$this->assertSame( "'@evil", peptide_starter_csv_safe( '@evil' ) );
|
|
$this->assertSame( "'\tTab", peptide_starter_csv_safe( "\tTab" ) );
|
|
$this->assertSame( "'\rCR", peptide_starter_csv_safe( "\rCR" ) );
|
|
}
|
|
|
|
public function test_csv_safe_passes_safe_values_through() {
|
|
$this->assertSame( 'alice@example.com', peptide_starter_csv_safe( 'alice@example.com' ) );
|
|
$this->assertSame( '2026-04-14 10:30:00', peptide_starter_csv_safe( '2026-04-14 10:30:00' ) );
|
|
$this->assertSame( '', peptide_starter_csv_safe( '' ) );
|
|
}
|
|
|
|
public function test_signup_stores_email_with_unsub_token() {
|
|
$_POST = array(
|
|
'ps_newsletter_nonce' => wp_create_nonce( 'peptide_starter_newsletter' ),
|
|
'email' => 'sub@example.com',
|
|
'ps_consent' => '1',
|
|
);
|
|
$this->invoke_signup();
|
|
|
|
$emails = get_option( 'ps_newsletter_emails', array() );
|
|
$this->assertCount( 1, $emails );
|
|
$this->assertSame( 'sub@example.com', $emails[0]['email'] );
|
|
$this->assertNotEmpty( $emails[0]['unsub_token'] );
|
|
}
|
|
|
|
public function test_signup_without_consent_rejected() {
|
|
$_POST = array(
|
|
'ps_newsletter_nonce' => wp_create_nonce( 'peptide_starter_newsletter' ),
|
|
'email' => 'nope@example.com',
|
|
);
|
|
$this->invoke_signup();
|
|
$emails = get_option( 'ps_newsletter_emails', array() );
|
|
$this->assertCount( 0, $emails );
|
|
}
|
|
|
|
public function test_signup_duplicate_does_not_add_second_row() {
|
|
$initial = array(
|
|
array(
|
|
'email' => 'dup@example.com',
|
|
'date' => '2026-04-10 00:00:00',
|
|
'unsub_token' => 'abc',
|
|
),
|
|
);
|
|
update_option( 'ps_newsletter_emails', $initial, false );
|
|
|
|
$_POST = array(
|
|
'ps_newsletter_nonce' => wp_create_nonce( 'peptide_starter_newsletter' ),
|
|
'email' => 'dup@example.com',
|
|
'ps_consent' => '1',
|
|
);
|
|
$this->invoke_signup();
|
|
|
|
$emails = get_option( 'ps_newsletter_emails', array() );
|
|
$this->assertCount( 1, $emails );
|
|
}
|
|
|
|
/**
|
|
* Invoke the handler and catch the wp_safe_redirect exit.
|
|
*/
|
|
protected function invoke_signup() {
|
|
$captured = null;
|
|
$filter = function ( $url ) use ( &$captured ) {
|
|
$captured = $url;
|
|
return false;
|
|
};
|
|
add_filter( 'wp_redirect', $filter );
|
|
|
|
try {
|
|
peptide_starter_handle_newsletter_signup();
|
|
} catch ( Exception $e ) { /* exit path */ }
|
|
|
|
remove_filter( 'wp_redirect', $filter );
|
|
}
|
|
}
|