首页 > 解决方案 > 我是否成功更改了禁止的 http 标头?

问题描述

根据此页面:https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_​​header_name,我不应该能够以编程方式更改禁止的 http 标头。例如,诸如“Origin”之类的禁止标头。

我有这个 Node.JS 代码:

router.get('/', function(req, res, next) {
  try {
    console.log('entry point');
    const res2 = request.post({
      url: 'http://localhost:3000/test',
      headers: {'Origin': "Lol"}
    });
    console.log({res2: res2});
    res.status(200).json({res2: res2});
  } catch (error) {
    console.error(error);
  }
});

你可以看到我肯定将“Origin”标题设置为“Lol”值。

所以我的期望是:一个错误,我的中间件默认设置的另一个值或其他东西,但无论如何,当我打印 req 对象时,我不应该得到'lol'值(对吗?)。

实际输出:

{ 
   "res2":{ 
      "uri":{ 
         "protocol":"http:",
         "slashes":true,
         "auth":null,
         "host":"localhost:3000",
         "port":"3000",
         "hostname":"localhost",
         "hash":null,
         "search":null,
         "query":null,
         "pathname":"/test",
         "path":"/test",
         "href":"http://localhost:3000/test"
      },
      "method":"POST",
      "headers":{ 
         "Origin":"Lol",
         "host":"localhost:3000"
      }
   }
}

您可以看到值是“Lol”,这与预期的不同。

所以我肯定错过了一些东西。你们 IT 人员知道它可能是什么吗?

在此先感谢您的帮助。

标签: node.jshttp

解决方案


正如@Adam Leblanc 在评论中所说:我的NodeJS 应用程序是发送请求的“用户代理”,这意味着它是“所有者”。所以它可以很容易地用任何东西来评估'Origin'标题。

这是一个菜鸟问题,但我相信我们都是从新手开始的。感谢您的回答亚当。


推荐阅读