settings = $settings; } /** * Classify the preview route without granting authorization. * * @return bool Whether this is a read-only admin-post preview route. */ public static function is_request() { global $pagenow; $action = $_GET['action'] ?? $_POST['action'] ?? ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput -- Exact string allowlist below classifies the route only; it grants no authorization. return 'admin-post.php' === $pagenow && is_string( $action ) && in_array( $action, array( self::DOCUMENT, self::STYLES ), true ); } /** Register authenticated endpoints; WordPress rejects anonymous requests. */ public function register_hooks() { add_action( 'admin_post_' . self::DOCUMENT, array( $this, 'document' ) ); add_action( 'admin_post_' . self::STYLES, array( $this, 'styles' ) ); } /** * One editor load gets an expiring, session-bound channel and separate nonces. * Tokens are stateless and are never stored as options or transients. * * @return array */ public static function configuration() { if ( ! current_user_can( Settings::capability() ) ) { return array(); } $channel = wp_generate_uuid4(); $expires = time() + self::LIFETIME; $token = self::signature( $channel, $expires ); $common = array( 'channel' => $channel, 'expires' => $expires, 'token' => $token, ); return array( 'channel' => $channel, 'document' => add_query_arg( array_merge( $common, array( 'action' => self::DOCUMENT, 'nonce' => wp_create_nonce( self::DOCUMENT . '_' . $channel ), ) ), admin_url( 'admin-post.php' ) ), 'endpoint' => admin_url( 'admin-post.php' ), 'parameters' => array_merge( $common, array( 'action' => self::STYLES, 'nonce' => wp_create_nonce( self::STYLES . '_' . $channel ), ) ), ); } /** * Fail closed before reading settings or rendering. The signed expiry limits replay. * * @param array $request GET or POST, unslashed. * @param string $action Nonce action namespace. * @param string $method Required HTTP method. * @return true|\WP_Error */ public function authorize( array $request, $action, $method ) { if ( ! is_user_logged_in() || ! current_user_can( Settings::capability() ) ) { return new \WP_Error( 'loginstudio_forbidden', __( 'You cannot access this preview.', 'login-brush' ), 403 ); } if ( ( $_SERVER['REQUEST_METHOD'] ?? '' ) !== $method ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Exact comparison with an internal GET/POST constant; never rendered. return new \WP_Error( 'loginstudio_method', __( 'Invalid preview request method.', 'login-brush' ), 405 ); } if ( ! isset( $request['nonce'] ) || ! is_string( $request['nonce'] ) ) { return new \WP_Error( 'loginstudio_token', __( 'Reload the editor to renew the preview.', 'login-brush' ), 403 ); } $nonce = sanitize_text_field( $request['nonce'] ); foreach ( array( 'channel', 'expires', 'token' ) as $key ) { if ( ! isset( $request[ $key ] ) || ! is_scalar( $request[ $key ] ) ) { return new \WP_Error( 'loginstudio_token', __( 'Reload the editor to renew the preview.', 'login-brush' ), 403 ); } } $channel = (string) $request['channel']; $expires = (string) $request['expires']; if ( ! preg_match( '/^[a-f0-9-]{36}$/D', $channel ) || ! preg_match( '/^[0-9]{10}$/D', $expires ) || (int) $expires < time() || (int) $expires > time() + self::LIFETIME || ! hash_equals( self::signature( $channel, (int) $expires ), (string) $request['token'] ) || ! wp_verify_nonce( $nonce, $action . '_' . $channel ) ) { return new \WP_Error( 'loginstudio_token', __( 'Reload the editor to renew the preview.', 'login-brush' ), 403 ); } return true; } /** * Bind the preview channel to the current user, site and session. * * @param string $channel Channel UUID. * @param int $expires Expiry. * @return string */ private static function signature( $channel, $expires ) { return hash_hmac( 'sha256', get_current_user_id() . '|' . wp_get_session_token() . '|' . get_current_blog_id() . '|' . $channel . '|' . $expires, wp_salt( 'nonce' ) ); } /** Render only an authorized GET, never a login submission. */ public function document() { $request = wp_unslash( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- authorize() verifies the channel nonce and signed session before rendering. $result = $this->authorize( $request, self::DOCUMENT, 'GET' ); if ( is_wp_error( $result ) ) { wp_die( esc_html( $result->get_error_message() ), '', array( 'response' => (int) $result->get_error_data() ) ); } nocache_headers(); header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); header( 'Referrer-Policy: no-referrer' ); // Append rather than replace: stricter site policies continue to apply. header( "Content-Security-Policy: default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: http:; font-src 'self' data:; connect-src 'none'; form-action 'none'; base-uri 'none'; object-src 'none'; frame-ancestors 'self'", false ); header( 'X-Content-Type-Options: nosniff' ); require_once __DIR__ . '/class-preview-renderer.php'; require_once LOGINSTUDIO_PATH . 'includes/class-css-generator.php'; require_once LOGINSTUDIO_PATH . 'includes/class-login.php'; $settings = $this->settings->all(); // Draft appearance is deliberately visible even while customization is disabled. $settings['general']['enabled'] = true; $css = ( new CSS_Generator() )->generate( $settings ); ( new Preview_Renderer() )->render( $request['channel'], is_wp_error( $css ) ? '' : $css, Login::logo_text( $settings ), Login::button_text( $settings ) ); exit; } /** Validate bounded drafts and return CSS without any settings write. */ public function styles() { $request = wp_unslash( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- authorize() verifies the channel nonce and signed session before processing the draft. $result = $this->authorize( $request, self::STYLES, 'POST' ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ), $result->get_error_data() ); } $payload = $request['settings'] ?? null; if ( ! is_string( $payload ) || strlen( $payload ) > 32768 ) { wp_send_json_error( array( 'message' => __( 'Invalid preview settings.', 'login-brush' ) ), 400 ); } $input = json_decode( $payload, true, 5 ); if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $input ) || ( $input['schema_version'] ?? null ) !== Settings_Schema::VERSION ) { wp_send_json_error( array( 'message' => __( 'Invalid preview settings.', 'login-brush' ) ), 400 ); } $result = $this->settings->validate( $input ); if ( $result['errors'] ) { wp_send_json_error( array( 'message' => __( 'Some preview settings are invalid.', 'login-brush' ) ), 400 ); } $result['settings']['general']['enabled'] = true; require_once LOGINSTUDIO_PATH . 'includes/class-css-generator.php'; $css = ( new CSS_Generator() )->generate( $result['settings'] ); if ( is_wp_error( $css ) ) { wp_send_json_error( array( 'message' => $css->get_error_message() ), 400 ); } require_once LOGINSTUDIO_PATH . 'includes/class-login.php'; nocache_headers(); wp_send_json_success( array( 'css' => $css, 'logoText' => Login::logo_text( $result['settings'] ), 'buttonText' => Login::button_text( $result['settings'] ), ) ); } }