提交表单时,与 <URL> 处的跨站点资源关联的 cookie 被设置为没有“SameSite”属性

cookie associated with a cross-site resource at <URL> was set without the `SameSite` attribute when submitting a form

我在客户网站上有一个 worldpay 表格,如果不在 safari 上禁用防止跨站点跟踪,它就无法在 ipad 上使用。 好像跟跨站cookies有关。

表单如下所示:

<div class="d-none">
    <form action="https://secure.worldpay.com/wcc/purchase" target="payment-iframe" method="post" id="worldpayForm">
        <input type="hidden" id="instId" name="instId">
        <input type="hidden" id="cartId" name="cartId">
        <input type="hidden" id="amount" name="amount">
        <input type="hidden" id="currency" name="currency">
        <input type="submit" id="makePayment" value="Buy This ">
    </form>
</div>

<iframe name="payment-iframe" style="min-width: 760px; min-height: 450px; border: 0;"></iframe>

在 JavaScript 中有一行是这样做的:

$('#worldpayForm').submit();

如果我注释掉该行,我不会收到 chrome 警告。 我在某个地方读到你可以添加这个:

response.setHeader("Set-Cookie", "HttpOnly;Secure;SameSite=Strict");

但这似乎只是在谈论向其他域发出请求。我如何在我的案例中应用它?

我设法解决了这个问题。是的,我确实已经有了自己的 cookie 解决方案:

export function getCookie(name) {
    var i, x, y;
    const arCookies = document.cookie.split(';');
    for (i = 0; i < arCookies.length; i++) {
        x = arCookies[i].substr(0, arCookies[i].indexOf('='));
        y = arCookies[i].substr(arCookies[i].indexOf('=') + 1);
        x = x.replace(/^\s+|\s+$/g, '');
        if (x === name) {
            return unescape(y);
        }
    }
    return '';
}

export function setCookie(name, value, expiresInDays) {
    const expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + expiresInDays);
    const cookieValue = escape(value) + (expiresInDays === null ? '' : `; expires=${expiryDate.toUTCString()}`);
    document.cookie = `${name}=${cookieValue}`;
}

export function clearCookies() {
    const cookies = document.cookie.split(';');

    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i];
        const eqPos = cookie.indexOf('=');
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;`;
    }
}

我刚刚更新为:

export function getCookie(name) {
    var i, x, y;
    const arCookies = document.cookie.split(';');
    for (i = 0; i < arCookies.length; i++) {
        x = arCookies[i].substr(0, arCookies[i].indexOf('='));
        y = arCookies[i].substr(arCookies[i].indexOf('=') + 1);
        x = x.replace(/^\s+|\s+$/g, '');
        if (x === name) {
            return unescape(y);
        }
    }
    return '';
}

export function setCookie(name, value, expiresInDays) {
    const expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + expiresInDays);
    const cookieValue = escape(value) + (expiresInDays === null ? '' : `; expires=${expiryDate.toUTCString()}`);
    document.cookie = `${name}=${cookieValue};SameSite=Strict;secure`;
}

export function clearCookies() {
    const cookies = document.cookie.split(';');

    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i];
        const eqPos = cookie.indexOf('=');
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;SameSite=Strict;secure`;
    }
}

现在一切正常。