【问题标题】:How to implement csrf with subdomains in NodeJS如何在 NodeJS 中实现带有子域的 csrf
【发布时间】:2021-01-01 03:08:48
【问题描述】:
const csrfProtection = csrf({ 
    cookie: { 
      domain: '.' + config_web.domain,
      secure: true,
      httpOnly: true,
      //sameSite: 'none'
    }, 
});

app.use(expresssession({
   store: new RedisStore({ client: redisClient }),
   secret: 'keyboard cat',
   resave: false,
   saveUninitialized: false,
   cookie: { 
     domain: '.' + config_web.domain, 
     maxAge: parseInt(cookiesTime), 
     secure: true, 
     httpOnly: true 
   }
}));

const corsOptions = {
  origin: ['https://api.domain.com', 'https://main.domain.com'],
  methods: 'POST',
  credentials: true,
  allowedHeaders: '*',//['Content-Type', 'Authorization', 'X-Requested-With'],
  optionsSuccessStatus: 200
}

app.use(cors(corsOptions));

我有主站点https://main.domain.com 并将通过https://api.domain.com 调用api。 分离两个子域后,从 csrf 调用 api 总是失败。我想知道我是否设置了错误的任何 cookie 内容?

【问题讨论】:

  • config_web.domain 的值是?我的意思是它看起来像什么?我猜是domin.com?

标签: node.js express security cors csrf


【解决方案1】:
$.ajax({
   url: a_cross_domain_url,
   xhrFields: {
      withCredentials: true
   }
});

刚刚发现在前端添加 xhrFields 然后就可以了

【讨论】:

    猜你喜欢
    • 2015-03-03
    • 2021-12-29
    • 2023-04-03
    • 2016-03-27
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2016-10-06
    • 2018-08-23
    相关资源
    最近更新 更多