我正在使用库Network Error Logging添加NEL
标头和报告以Report-To
在我的 Express.js 服务器上添加标头。
我的代码就像
app.use(reportTo({
groups: [
{
group: 'default',
max_age: 31536000,
include_subdomains: true,
endpoints: [
{
url: myReportToURL,
priority: 1,
},
],
},
],
}));
app.use(NEL({
report_to: 'default',
max_age: 31536000,
include_subdomains: true,
}));
有关它们的更多信息,请访问
- https://www.w3.org/TR/reporting/
- https://www.w3.org/TR/network-error-logging/#network-error-reports
- https://scotthelme.co.uk/network-error-logging-deep-dive/
我以前使用https://report-uri.com收集 NEL 错误,效果很好。
它收集了不同类型的 NEL 错误,如屏幕截图所示:
但是,现在我想构建自己的错误解析器然后收集。
我从 CSP 错误解析器中获得了洞察力,根据这个答案我可以解析
bodyParser.json({ type: 'application/json' });
bodyParser.json({ type: 'application/csp-report' });
对于 NEL,我发现了这个https://w3c.github.io/reporting/#media-type-registration
那么我应该这样解析吗?
bodyParser.json({ type: 'application/reports+json' });
或者是什么意思?
bodyParser.json({ type: 'application/json' });
bodyParser.json({ type: 'application/reports' });
如果有人知道如何在本地触发 NEL 错误,那对测试也很有帮助。谢谢!
更新 1(2020 年 10 月 14 日)
发现一个 W3C 示例正在使用application/reports+json
.
另一个 W3C 示例是使用application/report
(注意没有s
)。
所以我在这里创建了一张在 W3C GitHub 上询问的票。
更新 2(2020 年 10 月 14 日)
我已请求修复W3C 文档问题。正确的格式是application/reports+json
.
关于如何在本地触发 NEL 错误。我在 GitHub 上收到了 Express.js NEL 和 Report-To 库作者 James (@Cherry)的建议。所以我尝试连接https://fakedomainabccba.com
以获得dns.name_not_resolved
或类似的 DNS 错误。
但是,Chrome 88.0.4291.0 没有发送 NEL 错误,也没有显示在网络选项卡中。
更新 3(2020 年 10 月 16 日)
这是我最新的代码。我尝试通过使用两个端点进行比较来同时登录报告 URI 和我自己的服务器。报告 URI 确实收到了新报告,但是,我的服务器仍然存在问题。
(网站和API域相同,所以我不应该有CORS问题。如果它被CORS阻止,我可以在日志中看到它。)
app.use(reportTo({
groups: [
{
group: 'default',
max_age: 31536000,
include_subdomains: true,
endpoints: [
{
url: 'https://xxx.report-uri.com/xxx', // Report URI still works
priority: 1,
},
{
url: 'https://www.example.com/api/report-to', // "example" is my domain currently still has issue
priority: 1,
},
],
},
],
}));
app.use(NEL({
report_to: 'default',
max_age: 31536000,
include_subdomains: true,
}));
router.post('/api/report-to', bodyParser.json({ type: 'application/reports+json' }), (req, res) => {
console.log('reportTo', req.body);
res.sendStatus(200);
});
更新 4(工作解决方案,2020 年 10 月 28 日)
感谢@IanClelland 的帮助!在我删除 URL 中滥用的内部端口后,它现在可以工作了。也证实了,正如伊恩所说
报告 API 将仅交付到单个端点,以最大限度地减少传出带宽,因此使用多个端点进行冗余的人不会重复计算报告。
所以最终的工作版本看起来像
app.use(reportTo({
groups: [
{
group: 'default',
max_age: 31536000,
include_subdomains: true,
endpoints: [
{
url: 'https://www.example.com/api/report-to',
priority: 1,
},
],
},
],
}));
app.use(NEL({
report_to: 'default',
max_age: 31536000,
include_subdomains: true,
}));
router.post('/api/report-to', bodyParser.json({ type: 'application/reports+json' }), (req, res) => {
console.log('reportTo', req.body);
res.sendStatus(200);
});
我收到的成功日志看起来像
{
"age":42187,
"body":{
"elapsed_time":674,
"method":"GET",
"phase":"application",
"protocol":"h2",
"referrer":"",
"sampling_fraction":1,
"server_ip":"2606:4700:3032::681b:b258",
"status_code":404,
"type":"http.error"
},
"type":"network-error",
"url":"https://www.example.com/undefined",
"user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4305.0 Safari/537.36"
}