Skip to content

Commit 2261cdb

Browse files
authored
allow passthrough redirects (#125)
1 parent 33dba25 commit 2261cdb

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

example.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ const proxy = require('.')
55

66
async function startOrigin () {
77
const origin = Fastify()
8+
89
origin.get('/', async (request, reply) => {
910
return 'this is root'
1011
})
1112

13+
origin.get('/redirect', async (request, reply) => {
14+
return reply.redirect(302, 'https://fastify.io')
15+
})
16+
1217
origin.get('/a', async (request, reply) => {
1318
return 'this is a'
1419
})

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
2-
32
const From = require('fastify-reply-from')
43
const WebSocket = require('ws')
54

65
const httpMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
6+
const urlPattern = /^https?:\/\//
77

88
function liftErrorCode (code) {
99
if (typeof code !== 'number') {
@@ -31,6 +31,10 @@ function waitConnection (socket, write) {
3131
}
3232
}
3333

34+
function isExternalUrl (url = '') {
35+
return urlPattern.test(url)
36+
};
37+
3438
function proxyWebSockets (source, target) {
3539
function close (code, reason) {
3640
closeWebSocket(source, code, reason)
@@ -148,7 +152,7 @@ async function httpProxy (fastify, opts) {
148152

149153
function rewriteHeaders (headers) {
150154
const location = headers.location
151-
if (location) {
155+
if (location && !isExternalUrl(location)) {
152156
headers.location = location.replace(rewritePrefix, fastify.prefix)
153157
}
154158
if (oldRewriteHeaders) {

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ async function run () {
1717
return 'this is a'
1818
})
1919

20+
origin.get('/redirect', async (request, reply) => {
21+
return reply.redirect(302, 'https://fastify.io')
22+
})
23+
2024
origin.post('/this-has-data', async (request, reply) => {
2125
if (request.body.hello === 'world') {
2226
reply.header('location', '/something')
@@ -58,6 +62,27 @@ async function run () {
5862
t.equal(resultA.body, 'this is a')
5963
})
6064

65+
test('redirects passthrough', async t => {
66+
const server = Fastify()
67+
server.register(proxy, {
68+
upstream: `http://localhost:${origin.server.address().port}`
69+
})
70+
71+
await server.listen(0)
72+
t.tearDown(server.close.bind(server))
73+
74+
const {
75+
headers: { location },
76+
statusCode
77+
} = await got(
78+
`http://localhost:${server.server.address().port}/redirect`, {
79+
followRedirect: false
80+
}
81+
)
82+
t.equal(location, 'https://fastify.io')
83+
t.equal(statusCode, 302)
84+
})
85+
6186
test('no upstream will throw', async t => {
6287
const server = Fastify()
6388
server.register(proxy)

0 commit comments

Comments
 (0)