Files
peptide-starter-theme/page-auth.php
peptiderepo cb8b73f4ab v1.5.1: frontend auth hardening — rate limiting, email verification, honeypots, CSV safety
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>
2026-04-14 13:01:22 +07:00

168 lines
5.6 KiB
PHP

<?php
/**
* Template Name: Sign In / Register
*
* Branded authentication page replacing wp-login.php for the frontend.
* Handles sign-in and registration via WordPress native auth functions.
*
* @see assets/js/auth.js — form toggle and client-side validation
* @see functions.php — AJAX handler for auth form submission
*
* What: Renders sign-in / register forms with CSRF protection.
* Who calls it: WordPress template hierarchy when a page uses this template.
* Dependencies: WordPress core auth (wp_signon, wp_create_user).
*
* @package peptide-starter
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Redirect already logged-in users.
if ( is_user_logged_in() ) {
$redirect = isset( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : home_url( '/profile' );
wp_safe_redirect( $redirect );
exit;
}
// Capture redirect target for post-auth.
$redirect_to = isset( $_GET['redirect_to'] ) ? esc_url( wp_unslash( $_GET['redirect_to'] ) ) : home_url( '/' );
get_header();
?>
<main id="main" class="site-main">
<section class="ps-tool-page ps-auth-page">
<div class="ps-container ps-auth-container">
<div class="ps-auth-card">
<!-- Info Box -->
<div class="ps-alert ps-alert-info ps-auth-info">
<p>
<?php esc_html_e( 'To access the Subject Logs and store assay / lab results and protocols, you must log in or register so we can secure your data.', 'peptide-starter' ); ?>
</p>
</div>
<!-- Auth Status Messages -->
<div id="ps-auth-messages" class="ps-auth-messages" aria-live="polite"></div>
<!-- Sign In Form -->
<div id="ps-auth-login" class="ps-auth-form-wrap">
<h1><?php esc_html_e( 'Sign In', 'peptide-starter' ); ?></h1>
<form id="ps-login-form" class="ps-auth-form" method="post" novalidate>
<?php wp_nonce_field( 'ps_auth_login', 'ps_login_nonce' ); ?>
<input type="hidden" name="action" value="ps_auth_login">
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>">
<?php peptide_starter_render_honeypot( 'login' ); ?>
<div class="ps-form-group">
<label for="ps-login-email"><?php esc_html_e( 'Email', 'peptide-starter' ); ?></label>
<input
type="email"
id="ps-login-email"
name="email"
placeholder="<?php esc_attr_e( 'researcher@lab.com', 'peptide-starter' ); ?>"
required
autocomplete="email"
>
</div>
<div class="ps-form-group">
<label for="ps-login-password"><?php esc_html_e( 'Password', 'peptide-starter' ); ?></label>
<input
type="password"
id="ps-login-password"
name="password"
required
autocomplete="current-password"
>
</div>
<button type="submit" class="ps-btn ps-btn-primary ps-btn-full">
<?php esc_html_e( 'Sign In', 'peptide-starter' ); ?>
</button>
<p class="ps-auth-link">
<a href="<?php echo esc_url( wp_lostpassword_url() ); ?>">
<?php esc_html_e( 'Forgot password?', 'peptide-starter' ); ?>
</a>
</p>
</form>
<p class="ps-auth-toggle">
<?php esc_html_e( "Don't have an account?", 'peptide-starter' ); ?>
<a href="#register" id="ps-show-register"><?php esc_html_e( 'Register', 'peptide-starter' ); ?></a>
</p>
</div>
<!-- Register Form (hidden by default) -->
<div id="ps-auth-register" class="ps-auth-form-wrap ps-auth-form-wrap--hidden">
<h1><?php esc_html_e( 'Create Account', 'peptide-starter' ); ?></h1>
<form id="ps-register-form" class="ps-auth-form" method="post" novalidate>
<?php wp_nonce_field( 'ps_auth_register', 'ps_register_nonce' ); ?>
<input type="hidden" name="action" value="ps_auth_register">
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>">
<?php peptide_starter_render_honeypot( 'register' ); ?>
<div class="ps-form-group">
<label for="ps-reg-username"><?php esc_html_e( 'Username', 'peptide-starter' ); ?></label>
<input
type="text"
id="ps-reg-username"
name="username"
placeholder="<?php esc_attr_e( '5-15 alphanumeric characters', 'peptide-starter' ); ?>"
required
minlength="5"
maxlength="15"
pattern="[a-zA-Z0-9]+"
autocomplete="username"
>
<span class="ps-form-hint">
<?php esc_html_e( 'This becomes your public username.', 'peptide-starter' ); ?>
</span>
</div>
<div class="ps-form-group">
<label for="ps-reg-email"><?php esc_html_e( 'Email', 'peptide-starter' ); ?></label>
<input
type="email"
id="ps-reg-email"
name="email"
required
autocomplete="email"
>
</div>
<div class="ps-form-group">
<label for="ps-reg-password"><?php esc_html_e( 'Password', 'peptide-starter' ); ?></label>
<input
type="password"
id="ps-reg-password"
name="password"
required
minlength="8"
autocomplete="new-password"
>
<div id="ps-password-strength" class="ps-password-strength" aria-live="polite"></div>
</div>
<button type="submit" class="ps-btn ps-btn-primary ps-btn-full">
<?php esc_html_e( 'Register', 'peptide-starter' ); ?>
</button>
</form>
<p class="ps-auth-toggle">
<?php esc_html_e( 'Already have an account?', 'peptide-starter' ); ?>
<a href="#login" id="ps-show-login"><?php esc_html_e( 'Sign In', 'peptide-starter' ); ?></a>
</p>
</div>
</div>
</div>
</section>
</main>
<?php get_footer(); ?>