Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const lru = require('tiny-lru')
const querystring = require('querystring')
const Stream = require('stream')
const buildRequest = require('./lib/request')

const {
filterPseudoHeaders,
copyHeaders,
Expand Down Expand Up @@ -43,6 +44,15 @@ function fastProxy (opts = {}) {
const sourceHttp2 = req.httpVersionMajor === 2
let headers = { ...sourceHttp2 ? filterPseudoHeaders(req.headers) : req.headers }

if (!headers.host) {
Copy link
Member

@climba03003 climba03003 Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the host if the protocol is http/2 here?

From the document it must be exist for http/1.1. How about http/2?

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host
// @TODO: Should add performance-aware host header value validation(regex-based) as a further step?
res.statusCode = 400
res.end()

return
}

headers['x-forwarded-host'] = headers.host
headers.host = url.hostname
if (url.port) {
Expand Down
39 changes: 39 additions & 0 deletions test/11.host-header-val.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global describe, it */
'use strict'

const request = require('supertest')
let gateway, close, proxy, gHttpServer

describe('Host header validation', () => {
it('init', async () => {
const fastProxy = require('../index')({
base: 'http://127.0.0.1:3000'
})

proxy = fastProxy.proxy
close = fastProxy.close
})

it('init & start gateway', async () => {
// init gateway
gateway = require('restana')()

gateway.all('/service/*', function (req, res) {
delete req.headers.host
proxy(req, res, req.url, {})
})

gHttpServer = await gateway.start(8080)
})

it('should fail with Bad Request when Host header is missing', async () => {
await request(gHttpServer)
.get('/service/headers')
.expect(400)
})

it('close all', async () => {
close()
await gateway.close()
})
})