无效 Host/Referer 请求 header 最合适的 HTTP 状态代码是什么?

What is the most appropriate HTTP Status Code for Invalid Host/Referer request header?

我正在编写一个 Node.js/NPM 模块,用于保护快速服务器免受 DNS Rebind attacks 通过 server-side 主机和引用 [原文如此] header 验证。服务器管理员指定了列入白名单的主机 and/or 个引荐来源网址,任何不包含这些列入白名单的值的请求都将收到错误状态代码并且没有结果。

当客户在他们的请求中提供无效的 Host 或 Referer header 时,什么是最合适的 HTTP status code 到 return需要一个时根本无法提供? 我在想 401 未经授权,但我想征求第二意见。也可以为 400 Bad Request 提出一个论点,或者我想,412 Precondition Failed。

服务器是否有义务通过响应 headers 或 body 通知客户端他们的请求被拒绝的原因(例如,需要列入白名单的主机)或者是否可以响应一个错误代码,让客户想知道 security/obfuscation 的目的?

您可能是对的,最佳匹配似乎是未授权。但是最匹配的HTTP状态码并不是401,它实际上是403

403 状态代码表示 The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account of some sort.,这句话的后半部分非常适合您的用例。

参见:

对于所有 unauthorized/unauthenticated 请求,不建议回复任何解释,因为这将是您服务器的信息泄漏:http://projects.webappsec.org/w/page/13246936/Information%20Leakage