Express.js middleware that protects Node.js servers from DNS Rebinding attacks by validating Host and Referer [sic] headers from incoming requests. If a request doesn't contain a whitelisted Host/Referer header, host-validation
will respond with a 403 Forbidden HTTP error.
DNS Rebinding is a savvy exploit that hasn't gotten the attention it deserves over the years. For this reason tons of services are vulnerable to it because of lack of developer knowledge of the attack or simply negligence and indifference to patch against it. Don't be that person.
# install using npm
npm install host-validation
const express = require('express')
const hostValidation = require('host-validation')
const app = express()
// allow development hosts, a domain name, and a regex for all subdomains.
// any requests that don't supply a whitelisted Host will be rejected
// with a 403 HTTP status code
// NOTE: custom errors can be returned by a config.fail function. see "custom
// failure handlers" below.
app.use(hostValidation({ hosts: ['127.0.0.1:3000',
'localhost:3000',
'mydomain.com',
/.*\.mydomain\.com$/] }))
app.get('/', (req, res) => {
res.send('Hello trusted client, thanks for including a whitelisted Host header.')
})
app.listen(3000, () => {
console.log('server accepting requests w/ valid Host headers port 3000')
})
DNS Rebinding is a clever technique used to bypass the browser's Same-Origin Policy. This policy blocks malicious websites or HTML advertisements from making arbitrary requests to other servers. Imagine a scenario where you are surfing a news website that serves banner ads. One of those ads contains malicious JavaScript that POSTs default router credentials to http://192.168.1.1/router_login.php
, a common IP address for routers on home networks, in an attempt to open an inbound connections from the Internet. Even if 5% of users don't change there router admin panel login (not their WiFi network login) that attack could still be successful hundreds of thousands of times a day depending on the reach of the malicious ad.
Now, because of Same-Origin Policy, your web browser actually blocks that request to 192.168.1.1 from ever taking place. It notices that http://malicious-ad.com
is a different host than 192.168.1.1
(which doesn't have Cross Origin Resource Sharing (CORS) headers enabled) and stops the request dead in it's tracks. All, good right?
Not so fast. Enter DNS Rebinding. In the above scenario, your browser won't allow the malicious ad to make a request to http://192.168.1.1/router_login.php
, but it would allow a request to http://malicious-ad.com/router_login.php
. That doesn't seem like a problem because there is no way that http://malicious-ad.com
could be hosting our router login page, right? Technically, yes, that is true, but what if http://malicious-ad.com
could act as a proxy to your home router. Better yet, what if the domain name http://malicious-ad.com
actually resolved to 192.168.1.1
so that it could trick your browser into thinking it is making a request to the same host but the request is actually made to your home router. This is exactly what DNS Rebinding does. It uses a malicious DNS server (like FakeDNS) to respond to the same DNS request with different results at different times. A common DNS Rebinding server might be configured to respond to http://2dfaa01a-59bf-47db-a7cc-ddf4245e68b9.malicious-ad.com
with 157.218.13.52
, the real IP address of the HTTP server serving the malicious ad the first time it is requested, and then 192.168.1.1
every time that same domain name is requested thereafter.
Any HTTP server that 1) doesn't use HTTPS or 2) has no user authentication and 3) doesn't validate the Host header of incoming requests is vulnerable to DNS Rebind attacks.
This package protects you from #2. If you are using HTTPS you don't need to use this package (good job!). But hey... I bet you don't use HTTPS when when you are developing on your local machine/network. Local networks are among the top targets for DNS Rebind attacks, so you should probably validate Host headers in that circumstance too.
The reason that "Host" header validation mitigates against DNS rebinding is that malicious requests sent from web browsers will have "Host" values that don't match the ones you would expect your server to have. For instance, your home router should expect to see "Host" values like 192.168.1.1
, 192.168.0.1
, or maybe router.asus.com
, but definitely not http://malicious-ad.com
. Simply checking that the "Host" header contains the value that you expect will prevent DNS rebinding attacks and leave your users protected.
This package is dead simple. Include a few new lines of code and protect yourself from DNS Rebind attacks without ever thinking about it again.
// host and referrer headers can accept strings or regular expressions
app.use(hostValidation({ hosts: ['mydomain.com', /.*\.mydomain\.com$/] }))
// host and referrer headers can accept strings or regular expressions
app.use(hostValidation({ referers: ['http://trusted-site.com/login.php',
/^http:\/\/othersite\.com\/login\/.*/] }))
// only accept POSTs from HTTPS referrers
app.use(hostValidation({ referers: [/^https:\/\//]})
// you can include both host and referer values in the config
// by default, only requests that match BOTH Host and Referer values will be allowed
app.use(hostValidation({ hosts: ['trusted-host.com'],
referers: ['https://trusted-host.com/login.php'] })
// you can use the { mode: 'either' } value in the config accept requests that match
// either the hosts or the referers requirements. Accepted values for mode include
// 'both' and 'either'. The default value is 'both' if none is specified.
app.use(hostValidation({ hosts: ['trusted-host.com'],
referers: ['https://trusted-host.com/login.php'],
mode: 'either' })
// route-specific rules can be specified like any Express.js middleware
app.use('/login', hostValidation({ hosts: ['trusted-host.com'] }))
app.use('/from-twitter', hostValidation({ referrers: [/^https:\/\/twitter.com\//] }))
Add a custom error handler that's run when host or referer validation fails. This function overwrites the default behavior of responding to failed requests with a 403 Forbidden
error.
//
app.use('/brew-tea', hostValidation({
hosts: ['office-teapot'],
fail: (req, res, next) => {
// send a 418 "I'm a Teapot" Error
res.status(418).send('I\'m the office teapot. Refer to me only as such.')
}
}))
For more example usage see test.js
# navigate to the module directory
cd node_modules/host-validation
# install the dev dependencies
npm install --dev
# run the tests in test.js
npm test
spring-boot-starter-validation 前言: validation让我们简化了开发过程,可以使用简单的一个注解就实现了很多常见的检验数据的功能,同时支持自定义注解。 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-val
验证器 描述 ozzo-validation是一个Go软件包,提供可配置和可扩展的数据验证功能。它具有以下功能: 使用常规的编程构造而不是容易出错的构造标记来指定应如何验证数据。 可以验证不同类型的数据,例如结构,字符串,字节片,片,映射,数组。 只要实现Validatable接口,就可以验证自定义数据类型。 可以验证实现该sql.Valuer接口的数据类型(例如sql.NullString)。
在开发JAVA服务器端代码时,我们会遇到对外部传来的参数合法性进行验证,而hibernate-validator提供了一些常用的参数校验注解,我们可以拿来使用。 1.maven中引入hibernate-validator对应的jar: org.hibernate hibernate-validator 4.3.1.Final 2.在Model中定义要校验的字段: import javax.vali
一、hibernate-validation验证框架常见注解 @Null 被注释的元素必须为 null @NotNull 被注释的元素必须不为 null @AssertTrue 被注释的元素必须为 true @AssertFalse 被注释的元素必须为 false @Min(value) 被注释的元素必须是一
Host请求报头指定的服务器的域名(虚拟主机),和(可选地)的 TCP 端口上哪个服务器正在侦听数。 如果没有给出端口,则暗示所请求服务的默认端口(例如,HTTP URL 为“80”)。 Host头字段必须在所有 HTTP / 1.1 请求消息被发送。一个400(错误请求)状态码将被发送到任何缺少Host头字段或包含多个 HTTP / 1.1 请求消息。 Header type Request h
whistle的host转发功能,在保留传统hosts语法规则的同时,提供了更加灵活强大的匹配模式来满足不同场景的开发、调试需要。 保留传统的hosts语法规则 如果你习惯了通过修改操作系统hosts文件 (windows下为C:Windows\System32\drivers\etc\hosts,mac下为/etc/hosts) 的方式来修改域名解析,那么使用whistle时也可以采用同样的方法
X-Forwarded-Host(XFH)报头是用于识别由客户机在所要求的原始主机一个事实上的标准报头Host的 HTTP 请求报头。 反向代理(负载均衡器,CDN)的主机名称和端口可能与处理请求的源服务器不同,在这种情况下,X-Forwarded-Host头部可用于确定最初使用哪个主机。 此标题用于调试,统计和生成依赖于位置的内容,并且通过设计它可以显示隐私敏感信息,例如客户端的 IP 地址。因
描述 USB Host 模块是 USB 设备的适配器。您可以使用它将 Makeblock 主板连接到USB设备,例如USB操纵杆,鼠标或拇指驱动器。所以你可以用自己的操纵杆或其他东西来控制你的机器人。 现在,我们可以使用无线手柄为 mBot 添加新的控制方法:mBot 使用 USB Host 模块控制无线手柄。 特点 主芯片: CH375B 预留了高级DIY所用的焊点 不支持热插拔 16mm间隔M
Host Status Monitor App 是一个用Java 开发的网站监控工具,用来监控网站的宕机时间。
这是一个用于构建基于 Windows Phone 7 的 HTML/JavaScript 托管框架,使用 HTML5 技术。该框架由支持在标准 xap 格式中嵌入 html 应用的控件。