无法从Heroku上托管的Express应用设置cookie

时间:2020-05-30 17:19:40

标签: node.js express heroku cookies setcookie

我已经在Heroku中托管了前端和后端。

  • 前端-xxxxxx.herokuapp.com(反应应用)
  • 后端-yyyyyy.herokuapp.com(快递)

我正在尝试实施Google身份验证。从Google OAuth2获得令牌后,我试图通过Express应用在Cookie中设置import 'dart:io'; int inputf1; int inputf2; int inputf3; void main() { stdout.writeln('Type The First Number'); var input1 = stdin.readLineSync(); stdout.writeln('You typed: $input1 as the first number'); sleep( Duration(seconds: 1)); stdout.writeln('\nType The Second Number'); var input2 = stdin.readLineSync(); stdout.writeln('You typed: $input2 as the second number'); sleep( Duration(seconds: 1)); inputf1 = int.parse(input1); inputf2 = int.parse(input2); inputf3 = inputf1 + inputf2; print('\nfinal answer is : $inputf3'); sleep( Duration(seconds: 10)); } 和用户详细信息。

下面是后端中的一段代码,

id_token

对于我来说,它在我的本地计算机上运行得很好,但是在heroku上却无法运行。

这些是我已经尝试过的域-authRouter.get('/token', async (req, res) => { try { const result = await getToken(String(req.query.code)) const { id_token, userId, name, exp } = result; const cookieConfig = { domain: '.herokuapp.com', expires: new Date(exp * 1000), secure: true } res.status(201) .cookie('auth_token', id_token, { httpOnly: true, ...cookieConfig }) .cookie('user_id', userId, cookieConfig) .cookie('user_name', name, cookieConfig) .send("Login succeeded") } catch (err) { res.status(401).send("Login failed"); } }); .herokuapp.com。另外,我尝试不指定域字段本身。

我可以在响应标头上看到herokuapp.com的详细信息,但是/ token端点失败而没有返回任何状态代码,并且我看不到在应用程序选项卡上设置的cookie。

请参见以下图片

enter image description here 我在这里看不到任何状态码,但是它说失败了。 I can't see any status code here 这些是我可以看到的cookie信息,但是如果通过“应用程序”选项卡进行检查则无法使用。 enter image description here

我在这里想念什么?有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

希望您可以尝试通过以下方式确保安全: secure: req.secure || req.headers['x-forwarded-proto'] === 'https'

答案 1 :(得分:0)

您是对的,从技术上讲这应该可行。

除非它能够正常工作,否则可能会导致大规模的安全漏洞,因为任何能够创建Heroku子域的人都可以为所有其他子域生成会话cookie。

这不仅是Heroku的安全问题,还是任何其他允许您拥有子域的服务的安全问题。

这就是为什么从那时起创建并维护域列表以列出不应在子域之间共享cookie的公共域的原因。该列表通常由浏览器使用。

您可以想象,域heroku.com是此列表的一部分。

如果您想了解更多信息,此列表称为Mozilla基金会的Public Suffix List