Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastify crashes on the upstream returns invalid status code #374

Closed
2 tasks done
ILovePug opened this issue Jul 5, 2024 · 1 comment · Fixed by #386
Closed
2 tasks done

fastify crashes on the upstream returns invalid status code #374

ILovePug opened this issue Jul 5, 2024 · 1 comment · Fixed by #386
Labels

Comments

@ILovePug
Copy link

ILovePug commented Jul 5, 2024

Prerequisites

  • I have written a descriptive issue title
  • 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.

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants