fastify-reply-from icon indicating copy to clipboard operation
fastify-reply-from copied to clipboard

fastify crashes on the upstream returns invalid status code

Open ILovePug opened this issue 7 months ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

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;
    }
  });
});

  1. run node index.js
  2. visit http://localhost:3000/
  3. 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.

ILovePug avatar Jul 05 '24 19:07 ILovePug