如何为 WooCommerce 支付表单添加 SameSite=Strict Cookie?

解读

在国内电商场景下,支付环节对 Cookie 的“第一方”与“安全”属性要求极高。
WooCommerce 的结算、支付网关(微信、支付宝、银联、Ping++ 等)大多依赖 setcookie() 或 wp_set_auth_cookie() 来保持会话,而浏览器对跨站请求默认的 SameSite=Lax 会导致 POST/iframe 支付回跳时 Cookie 被屏蔽,出现“登录态丢失、订单状态无法更新”的线上故障。
面试官问“如何加 SameSite=Strict”,并不是让你简单改一行参数,而是考察:

  1. 你是否知道 PHP 7.3 以下没有原生 SameSite 支持;
  2. 你是否能在 兼容 PHP 5.6~8.3、HTTPS 强制、CDN 缓存、多端小程序 的国内环境里给出可落地的代码;
  3. 你是否能把 Cookie 的“安全、域名、路径、过期时间、HostOnly、优先级”与 WooCommerce 的 支付网关回调、订单锁、用户会话 完整串起来。

知识点

  • SameSite 属性三值:Strict、Lax、None;
  • PHP 版本差异:<7.3 需手动追加 ; SameSite=Strict,≥7.3 支持 $options['samesite'];
  • WordPress Cookie 钩子:wp_set_auth_cookie、setcookie、set_logged_in_cookie、woocommerce_set_customer_auth_cookie;
  • WooCommerce 支付流程:woocommerce_checkout_process → 创建订单 → 外部网关 → woocommerce_payment_complete;
  • 国内常见坑:
    – 微信内嵌浏览器对 SameSite=None 同时要求 Secure;
    – 支付宝回跳是 POST + 302,Strict 会阻断,必须 只在结算页设置 Strict,网关回调页保持 Lax;
    – CDN 边缘节点缓存 Set-Cookie 头,需要 Nginx fastcgi_cache_bypass 或 阿里云全站缓存规则 排除 /checkout/*;
  • CI/CD 安全:自动化测试需断言 Set-Cookie 头包含 SameSite=Strict,防止回归。

答案

给出一段可直接放进 插件 wp-content/plugins/my-samesite/my-samesite.php 的生产级代码,兼顾 PHP 版本、多网关、单元测试。

<?php
/**
 * Plugin Name: My Woo SameSite Strict
 * Description: 为 WooCommerce 结算页关键 Cookie 追加 SameSite=Strict
 * Version: 1.0.0
 * Requires PHP: 5.6
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }

add_action( 'plugins_loaded', 'my_samesite_init', 20 );

function my_samesite_init() {
    // 只在结算、支付、我的账户页面生效,避免全站 Strict 阻断回跳
    if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
        return;
    }

    // 1. 拦截 WP 核心 setcookie
    add_filter( 'wp_set_auth_cookie', 'my_samesite_cookie_patch', 10, 1 );
    // 2. 拦截 Woo 客户会话 Cookie
    add_filter( 'woocommerce_set_customer_auth_cookie', 'my_samesite_cookie_patch', 10, 1 );
}

/**
 * 统一给 Cookie 追加 SameSite=Strict
 *
 * @param string $cookie_header 原始 Set-Cookie 头
 * @return string
 */
function my_samesite_cookie_patch( $cookie_header ) {
    // 已存在 SameSite 不再重复
    if ( stripos( $cookie_header, 'SameSite=' ) !== false ) {
        return $cookie_header;
    }

    // PHP < 7.3 场景:直接拼接
    $cookie_header .= '; SameSite=Strict';

    // 强制 Secure(国内生产环境全站 HTTPS)
    if ( stripos( $cookie_header, 'Secure' ) === false && is_ssl() ) {
        $cookie_header .= '; Secure';
    }

    return $cookie_header;
}

// 对 PHP ≥7.3 使用新的 setcookie 签名
add_action( 'init', 'my_samesite_php73_compat', 1 );
function my_samesite_php73_compat() {
    if ( version_compare( PHP_VERSION, '7.3.0', '>=' ) ) {
        // 覆盖默认的 setcookie 行为
        add_filter( 'send_auth_cookies', '__return_false' ); // 先关闭默认发送
        add_action( 'set_auth_cookie', 'my_samesite_send_php73', 10, 5 );
    }
}

function my_samesite_send_php73( $auth_cookie, $expire, $expiration, $user_id, $scheme ) {
    $secure = $scheme === 'secure_auth' && is_ssl();
    $option = array(
        'expires'  => $expire,
        'path'     => SITECOOKIEPATH,
        'domain'   => COOKIE_DOMAIN,
        'secure'   => $secure,
        'httponly' => true,
        'samesite' => 'Strict',
    );
    setcookie( AUTH_COOKIE, $auth_cookie, $option );
}

部署步骤

  1. 把插件打包为 my-samesite.zip,通过 宝塔面板 或 阿里云 Codeup CI 自动部署到 /wp-content/plugins/;
  2. 在 Nginx 配置 里对 /checkout 路径关闭 proxy_cache,防止 CDN 吞掉 Set-Cookie;
  3. 用 Postman 请求 POST /checkout/?wc-ajax=checkout,断言响应头包含 SameSite=Strict;
  4. 回归测试 微信 H5 支付:在内嵌浏览器完成支付后,订单状态应变为“处理中”,否则说明 Strict 阻断,需把网关回调路径 /?wc-api=wechatpay_callback 加入白名单,改为 Lax。

拓展思考

  • SameSite=Strict 并非越严格越好:国内小程序webview、企业微信、钉钉内嵌场景,首次跳转会被判定为 跨站,导致 Cookie 丢失,必须结合 URL 重写 + token 透传 做降级;
  • WooCommerce 高并发订单锁:Strict 会阻断 异步 Webhook,需要把 wp_woocommerce_session_ 这条 Cookie 保持 Lax,而仅把 wordpress_logged_in_ 设为 Strict,实现 “会话分离”;
  • PHPUnit 测试覆盖:在 tests/phpunit/includes/test-samesite.php 里用 Brain\Monkey\Functions 模拟 setcookie,断言 samesite 值,防止后续 PHP 升级或 Woo 升级导致回归;
  • 国密算法合规:若项目过 等保三级,Cookie 中不能出现用户 ID 明文,需先 AES-GCM 加密再写入,SameSite 只是安全链中的一环;
  • 灰度发布:使用 阿里云 MSE 全链路灰度,按 Header x-canary=1 把 10% 流量导到新 Cookie 策略,监控 订单支付成功率 与 JS 报错数,确保 Strict 策略真正可上线。