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

时间:2020-01-29 00:10:33

标签: javascript google-chrome safari cross-domain

我在客户网站上有一个worldpay表单,并且在禁用ipad上的 prevent跨站点跟踪之前,它不能在ipad上运行。 它似乎与跨站点Cookie有关。

表单如下:

<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();

如果我将该行注释掉,则不会收到镶边警告。 我读过某个地方,您可以添加以下内容:

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

但这似乎只是在谈论向其他域发出请求。在我的情况下该如何应用?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。是的,我确实有自己的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`;
    }
}

现在一切正常。