Skip to content

Commit

Permalink
Ensure Same Host
Browse files Browse the repository at this point in the history
Prevent DNS rebinding attacks by checking the Host header.  Such attacks should
already be mitigated by the Origin header checks; however, this adds another
layer of protection.
  • Loading branch information
execjosh authored and felixhageloh committed Aug 28, 2022
1 parent b3f370f commit ff544ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions server/src/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ watchDir = require('./directory_watcher.coffee')
WidgetBundler = require('./WidgetBundler.js')
Settings = require('./Settings')
StateServer = require('./StateServer')
ensureSameHost = require('./ensureSameHost')
ensureSameOrigin = require('./ensureSameOrigin')
disallowIFraming = require('./disallowIFraming')
CommandServer = require('./command_server.coffee')
Expand Down Expand Up @@ -70,9 +71,11 @@ module.exports = (port, widgetPath, settingsPath, publicPath, options, callback)
# set up the server
host = "127.0.0.1"
messageBus = null
allowedOrigin = "http://#{host}:#{port}"
allowedHost = "#{host}:#{port}"
allowedOrigin = "http://#{allowedHost}"
middleware = connect()
.use(disallowIFraming)
.use(ensureSameHost(allowedHost))
.use(ensureSameOrigin(allowedOrigin))
.use(CommandServer(widgetPath, options.loginShell))
.use(StateServer(store))
Expand All @@ -90,7 +93,7 @@ module.exports = (port, widgetPath, settingsPath, publicPath, options, callback)
messageBus = MessageBus(
server: server,
verifyClient: (info) ->
info.origin == allowedOrigin || info.origin == 'Übersicht'
info.req.headers.host == allowedHost && (info.origin == allowedOrigin || info.origin == 'Übersicht')
)
sharedSocket.open("ws://#{host}:#{port}")
callback?()
Expand Down
9 changes: 9 additions & 0 deletions server/src/ensureSameHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function ensureSameHost(host) {
return ((req, res, next) => {
if (req.headers.host && req.headers.host === host) {
return next()
}
res.writeHead(400)
res.end()
})
}

0 comments on commit ff544ff

Please sign in to comment.