提交表单时,设置了与跨站点资源关联的 cookie,<URL>但没有"SameSite"属性



我在客户网站上有一个 worldpay 表格,如果不禁用 safari 上的防止跨站点跟踪,它将无法在 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");

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

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

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`;
}
}

现在一切都有效。

相关内容

  • 没有找到相关文章

最新更新