Skip to content

Commit b313122

Browse files
simoshodancastillo
andauthoredNov 18, 2024··
fix: return 502 on invalid upstream status code (#386)
* fix: return 502 on invalid upstream status code * chore: move warning log into error handler Co-authored-by: Dan Castillo <dan.castillo@jasper.ai> Signed-off-by: Simon Schoonjans <simon.schoonjans99@gmail.com> * chore: apply suggestion to always map to badGatewayError --------- Signed-off-by: Simon Schoonjans <simon.schoonjans99@gmail.com> Co-authored-by: Dan Castillo <dan.castillo@jasper.ai>
1 parent 7a344ea commit b313122

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed
 

‎index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const {
2020
ConnectionResetError,
2121
ConnectTimeoutError,
2222
UndiciSocketError,
23-
InternalServerError
23+
InternalServerError,
24+
BadGatewayError
2425
} = require('./lib/errors')
2526

2627
const fastifyReplyFrom = fp(function from (fastify, opts, next) {
@@ -199,7 +200,13 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
199200
} else {
200201
copyHeaders(rewriteHeaders(res.headers, this.request), this)
201202
}
202-
this.code(res.statusCode)
203+
try {
204+
this.code(res.statusCode)
205+
} catch (err) {
206+
// Since we know `FST_ERR_BAD_STATUS_CODE` will be recieved
207+
onError(this, { error: new BadGatewayError() })
208+
this.request.log.warn(err, 'response has invalid status code')
209+
}
203210
if (onResponse) {
204211
onResponse(this.request, this, res)
205212
} else {

‎lib/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ module.exports.ConnectionResetError = createError('ECONNRESET', 'Connection Rese
1212
module.exports.ConnectTimeoutError = createError('UND_ERR_CONNECT_TIMEOUT', 'Connect Timeout Error', 500)
1313
module.exports.UndiciSocketError = createError('UND_ERR_SOCKET', 'Undici Socket Error', 500)
1414
module.exports.InternalServerError = createError('FST_REPLY_FROM_INTERNAL_SERVER_ERROR', '%s', 500)
15+
module.exports.BadGatewayError = createError('FST_REPLY_FROM_BAD_GATEWAY', 'Bad Gateway', 502)
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const Fastify = require('fastify')
5+
const From = require('..')
6+
const http = require('node:http')
7+
const get = require('simple-get').concat
8+
9+
const instance = Fastify()
10+
instance.register(From)
11+
12+
t.plan(8)
13+
t.teardown(instance.close.bind(instance))
14+
15+
const target = http.createServer((req, res) => {
16+
t.pass('request proxied')
17+
t.equal(req.method, 'GET')
18+
res.statusCode = 888
19+
res.end('non-standard status code')
20+
})
21+
22+
instance.get('/', (_, reply) => {
23+
reply.from(`http://localhost:${target.address().port}`, {
24+
onResponse: (_, reply, res) => {
25+
t.equal(res.statusCode, 888)
26+
}
27+
})
28+
})
29+
30+
t.teardown(target.close.bind(target))
31+
32+
instance.listen({ port: 0 }, (err) => {
33+
t.error(err)
34+
35+
target.listen({ port: 0 }, (err) => {
36+
t.error(err)
37+
38+
get(
39+
`http://localhost:${instance.server.address().port}`,
40+
(err, res, data) => {
41+
t.error(err)
42+
t.equal(res.statusCode, 502)
43+
t.same(JSON.parse(data), {
44+
statusCode: 502,
45+
code: 'FST_REPLY_FROM_BAD_GATEWAY',
46+
error: 'Bad Gateway',
47+
message: 'Bad Gateway'
48+
})
49+
}
50+
)
51+
})
52+
})

0 commit comments

Comments
 (0)
Please sign in to comment.