Polyfill SameSite Cookie属性

时间:2020-01-29 08:33:23

标签: samesite

是否有一种方法可以填充SameSite cookie属性,以便可以在旧版浏览器或特定浏览器的旧版本中使用此功能?

https://caniuse.com/#feat=same-site-cookie-attribute

1 个答案:

答案 0 :(得分:1)

我认为我必须通过反向代理解决此问题:

const express = require('express');
const cp = require('cookie-parser');
const app = express();
app.use(cp());

app.get('/set', (req, res) => {
  // Set the new style cookie
  res.cookie('3pcookie', 'value', { sameSite: 'none', secure: true });
  // And set the same value in the legacy cookie
  res.cookie('3pcookie-legacy', 'value', { secure: true });
  res.end();
});

app.get('/', (req, res) => {
  let cookieVal = null;

  if (req.cookies['3pcookie']) {
    // check the new style cookie first
    cookieVal = req.cookies['3pcookie'];
  } else if (req.cookies['3pcookie-legacy']) {
    // otherwise fall back to the legacy cookie
    cookieVal = req.cookies['3pcookie-legacy'];
  }

  res.end();
});

app.listen(process.env.PORT);

请参阅https://web.dev/samesite-cookie-recipes/