Skip to content

Commit 70f8716

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Set Forwarded header on proxied requests.
1 parent 2a14bc7 commit 70f8716

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/handlers/auth-proxy.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ function addAuthProxyHandler (app, sourcePath, target) {
2626
const sourcePathLength = sourcePath.length
2727
const settings = Object.assign({
2828
target,
29-
onProxyReq: addUserHeader,
30-
onProxyReqWs: addUserHeader,
29+
onProxyReq: addAuthHeaders,
30+
onProxyReqWs: addAuthHeaders,
3131
pathRewrite: path => path.substr(sourcePathLength)
3232
}, PROXY_SETTINGS)
3333

3434
// Activate the proxy
3535
app.use(`${sourcePath}*`, proxy(settings))
3636
}
3737

38-
// Adds a User header with the user's ID if the user is logged in
39-
function addUserHeader (proxyReq, req) {
40-
if (req.session && req.session.userId) {
41-
proxyReq.setHeader('User', req.session.userId)
38+
// Adds a headers with authentication information
39+
function addAuthHeaders (proxyReq, req) {
40+
const { session = {}, headers = {} } = req
41+
if (session.userId) {
42+
proxyReq.setHeader('User', session.userId)
43+
}
44+
if (headers.host) {
45+
proxyReq.setHeader('Forwarded', `host=${headers.host}`)
4246
}
4347
}

test/unit/auth-proxy.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ describe('Auth Proxy', () => {
5454
expect(headers).to.have.property('host', 'server-a.org')
5555
})
5656

57+
it('sets the Forwarded header on the proxy request', () => {
58+
const { headers } = response.body
59+
expect(headers).to.have.property('forwarded', `host=${HOST}`)
60+
})
61+
5762
it('returns status code 200', () => {
5863
expect(response).to.have.property('statusCode', 200)
5964
})
@@ -83,6 +88,11 @@ describe('Auth Proxy', () => {
8388
expect(headers).to.have.property('host', 'server-a.org')
8489
})
8590

91+
it('sets the Forwarded header on the proxy request', () => {
92+
const { headers } = response.body
93+
expect(headers).to.have.property('forwarded', `host=${HOST}`)
94+
})
95+
8696
it('returns status code 200', () => {
8797
expect(response).to.have.property('statusCode', 200)
8898
})
@@ -112,6 +122,11 @@ describe('Auth Proxy', () => {
112122
expect(headers).to.have.property('host', 'server-b.org')
113123
})
114124

125+
it('sets the Forwarded header on the proxy request', () => {
126+
const { headers } = response.body
127+
expect(headers).to.have.property('forwarded', `host=${HOST}`)
128+
})
129+
115130
it('returns status code 200', () => {
116131
expect(response).to.have.property('statusCode', 200)
117132
})
@@ -141,6 +156,11 @@ describe('Auth Proxy', () => {
141156
expect(headers).to.have.property('host', 'server-b.org')
142157
})
143158

159+
it('sets the Forwarded header on the proxy request', () => {
160+
const { headers } = response.body
161+
expect(headers).to.have.property('forwarded', `host=${HOST}`)
162+
})
163+
144164
it('returns status code 200', () => {
145165
expect(response).to.have.property('statusCode', 200)
146166
})
@@ -174,6 +194,11 @@ describe('Auth Proxy', () => {
174194
expect(headers).to.have.property('host', 'server-a.org')
175195
})
176196

197+
it('sets the Forwarded header on the proxy request', () => {
198+
const { headers } = response.body
199+
expect(headers).to.have.property('forwarded', `host=${HOST}`)
200+
})
201+
177202
it('returns status code 200', () => {
178203
expect(response).to.have.property('statusCode', 200)
179204
})

0 commit comments

Comments
 (0)