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>
153 lines
5.2 KiB
PHP
153 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* Tests for inc/auth-handlers.php.
|
|
*
|
|
* Covers: unified error messages, rate limiting, nonce failure,
|
|
* honeypot drop, enumeration resistance.
|
|
*
|
|
* @package peptide-starter
|
|
*/
|
|
|
|
/**
|
|
* @runTestsInSeparateProcesses disabled — WP_UnitTestCase handles isolation.
|
|
*/
|
|
class Test_Auth_Handlers extends WP_UnitTestCase {
|
|
|
|
/**
|
|
* Capture wp_send_json_{success,error} via wp_die_handler so tests can
|
|
* assert on the response without ending the PHP process.
|
|
*
|
|
* @var array|null
|
|
*/
|
|
protected $captured = null;
|
|
|
|
public function set_up() {
|
|
parent::set_up();
|
|
$this->captured = null;
|
|
|
|
// Swap wp_die into a throwable we can catch.
|
|
add_filter( 'wp_die_ajax_handler', array( $this, 'get_die_handler' ) );
|
|
add_filter( 'wp_die_handler', array( $this, 'get_die_handler' ) );
|
|
|
|
// Capture the JSON body via the wp_json_encode filter path.
|
|
add_filter(
|
|
'wp_json_encode_data',
|
|
function ( $data ) {
|
|
$this->captured = $data;
|
|
return $data;
|
|
}
|
|
);
|
|
}
|
|
|
|
public function tear_down() {
|
|
remove_all_filters( 'wp_die_ajax_handler' );
|
|
remove_all_filters( 'wp_die_handler' );
|
|
remove_all_filters( 'wp_json_encode_data' );
|
|
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_%'" );
|
|
$_POST = array();
|
|
parent::tear_down();
|
|
}
|
|
|
|
public function get_die_handler() {
|
|
return array( $this, 'throwing_die' );
|
|
}
|
|
|
|
public function throwing_die( $message = '', $title = '', $args = array() ) {
|
|
throw new WPAjaxDieStopException( is_scalar( $message ) ? (string) $message : wp_json_encode( $message ) );
|
|
}
|
|
|
|
/**
|
|
* Run the login handler and return the captured response, if any.
|
|
*/
|
|
protected function invoke_login() {
|
|
try {
|
|
peptide_starter_ajax_login();
|
|
} catch ( WPAjaxDieStopException $e ) {
|
|
// Expected — JSON was sent.
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function test_login_missing_nonce_rejects_with_security_message() {
|
|
$_POST = array( 'email' => 'a@b.com', 'password' => 'whatever' );
|
|
$this->invoke_login();
|
|
$this->assertIsArray( $this->captured );
|
|
$this->assertFalse( $this->captured['success'] );
|
|
$this->assertStringContainsString( 'Security check', $this->captured['data']['message'] );
|
|
}
|
|
|
|
public function test_login_unknown_email_returns_unified_message() {
|
|
$_POST = array(
|
|
'ps_login_nonce' => wp_create_nonce( 'ps_auth_login' ),
|
|
'email' => 'nobody-' . wp_generate_password( 8, false ) . '@example.com',
|
|
'password' => 'wrong-password',
|
|
);
|
|
$this->invoke_login();
|
|
$this->assertSame( peptide_starter_login_failure_message(), $this->captured['data']['message'] );
|
|
}
|
|
|
|
public function test_login_wrong_password_returns_unified_message() {
|
|
$user = self::factory()->user->create_and_get( array( 'user_pass' => 'correct-horse-battery' ) );
|
|
$_POST = array(
|
|
'ps_login_nonce' => wp_create_nonce( 'ps_auth_login' ),
|
|
'email' => $user->user_email,
|
|
'password' => 'definitely-wrong',
|
|
);
|
|
$this->invoke_login();
|
|
$this->assertSame( peptide_starter_login_failure_message(), $this->captured['data']['message'] );
|
|
}
|
|
|
|
public function test_login_honeypot_tripped_returns_fake_success() {
|
|
$_POST = array(
|
|
'ps_login_nonce' => wp_create_nonce( 'ps_auth_login' ),
|
|
'email' => 'a@b.com',
|
|
'password' => 'x',
|
|
'ps_hp_login' => 'botfill',
|
|
);
|
|
$this->invoke_login();
|
|
$this->assertTrue( $this->captured['success'], 'Honeypot must silently fake success.' );
|
|
}
|
|
|
|
public function test_login_rate_limit_exceeded_still_returns_unified_message() {
|
|
// Exhaust the budget.
|
|
$config = peptide_starter_config_rate_limit( 'login' );
|
|
for ( $i = 0; $i < $config['limit']; $i++ ) {
|
|
Peptide_Starter_Rate_Limiter::record( 'login', peptide_starter_hash_identifier( 'same@same.com' ) );
|
|
}
|
|
$_POST = array(
|
|
'ps_login_nonce' => wp_create_nonce( 'ps_auth_login' ),
|
|
'email' => 'same@same.com',
|
|
'password' => 'anything',
|
|
);
|
|
$this->invoke_login();
|
|
$this->assertSame( peptide_starter_login_failure_message(), $this->captured['data']['message'] );
|
|
}
|
|
|
|
public function test_register_invalid_username_returns_unified_message() {
|
|
$_POST = array(
|
|
'ps_register_nonce' => wp_create_nonce( 'ps_auth_register' ),
|
|
'username' => 'ab',
|
|
'email' => 'newuser@example.com',
|
|
'password' => 'longenoughpassword',
|
|
);
|
|
update_option( 'users_can_register', 1 );
|
|
try { peptide_starter_ajax_register(); } catch ( WPAjaxDieStopException $e ) { /* ok */ }
|
|
$this->assertSame( peptide_starter_register_failure_message(), $this->captured['data']['message'] );
|
|
}
|
|
|
|
public function test_register_duplicate_email_returns_unified_message() {
|
|
self::factory()->user->create( array( 'user_email' => 'dup@example.com' ) );
|
|
update_option( 'users_can_register', 1 );
|
|
$_POST = array(
|
|
'ps_register_nonce' => wp_create_nonce( 'ps_auth_register' ),
|
|
'username' => 'newusername',
|
|
'email' => 'dup@example.com',
|
|
'password' => 'longenoughpassword',
|
|
);
|
|
try { peptide_starter_ajax_register(); } catch ( WPAjaxDieStopException $e ) { /* ok */ }
|
|
$this->assertSame( peptide_starter_register_failure_message(), $this->captured['data']['message'] );
|
|
}
|
|
}
|