fastify / fastify-reply-from

fastify plugin to forward the current http request to another server
MIT License
148 stars 76 forks source link

fastify crashes on the upstream returns invalid status code #374

Open ILovePug opened 2 months ago

ILovePug commented 2 months ago

Prerequisites

Fastify version

4.28.1

Plugin version

9.8.0

Node.js version

20.13.1

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.7.5

Description

When the upstream server returns an invalid status code (ex. 888), fastify server will crash.

steps to reproduce:

  1. npm i express fastify @fastify/reply-from
  2. create a index.js file with the following code
    
    "use strict";

const Fastify = require("fastify"); const express = require("express");

const target = express(); target.get("/", (req, res) => { res.status(888).send("Hello World!"); });

const proxy = Fastify();

proxy.register(require("@fastify/reply-from"), { base: "http://localhost:3001/", });

proxy.get("/", (request, reply) => { reply.from("/"); });

target.listen({ port: 3001 }, (err) => { if (err) { throw err; }

proxy.listen({ port: 3000 }, (err) => { if (err) { throw err; } }); });

3. run `node index.js`
4. visit `http://localhost:3000/`
5. the app will crash with error

node:events:497 throw er; // Unhandled 'error' event ^ FastifyError [Error]: Called reply with an invalid status code: 888 at Reply.code (/fastify-reply-error/node_modules/fastify/lib/reply.js:338:11) at /fastify-reply-error/node_modules/@fastify/reply-from/index.js:201:12 at /fastify-reply-error/node_modules/@fastify/reply-from/index.js:284:9 at /fastify-reply-error/node_modules/@fastify/reply-from/lib/request.js:177:7 at RequestHandler.runInAsyncScope (node:async_hooks:206:9) at RequestHandler.onHeaders (/fastify-reply-error/node_modules/undici/lib/api/api-request.js:104:14) at Request.onHeaders (/fastify-reply-error/node_modules/undici/lib/core/request.js:275:29) at Parser.onHeadersComplete (/fastify-reply-error/node_modules/undici/lib/client.js:920:27) at wasm_on_headers_complete (/fastify-reply-error/node_modules/undici/lib/client.js:536:30) at wasm://wasm/0003626a:wasm-function[11]:0x494 Emitted 'error' event on BodyReadable instance at: at BodyReadable.emit (/fastify-reply-error/node_modules/undici/lib/api/readable.js:73:18) at emitErrorNT (node:internal/streams/destroy:169:8) at emitErrorCloseNT (node:internal/streams/destroy:128:3) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'FST_ERR_BAD_STATUS_CODE', statusCode: 500 }



### Link to code that reproduces the bug

_No response_

### Expected Behavior

The app should not crash.