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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fastify crashes when reply code is not recognized #2169

Closed
chiku opened this issue Apr 1, 2020 · 5 comments 路 Fixed by #2184
Closed

Fastify crashes when reply code is not recognized #2169

chiku opened this issue Apr 1, 2020 · 5 comments 路 Fixed by #2184
Assignees
Labels
bug Confirmed bug v2.x Issue or pr related to Fastify v2

Comments

@chiku
Copy link
Contributor

chiku commented Apr 1, 2020

馃悰 Bug Report

Fastify crashes when reply code is not recognized.

To Reproduce

Steps to reproduce the behavior:

Run fastify server that returns an exotic response code.

const fastify = require('fastify')({ logger: true });

fastify.get('/', async (request, reply) => {
  reply.code(525); // exotic response code
  return { hello: 'world' };
});

const start = async () => {
  try {
    await fastify.listen(3000);
    fastify.log.info(`server listening on ${fastify.server.address().port}`);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();

Make a request to the server

curl http://localhost:3000

The application crashes.

The HTTP response code is 500 and the response body is as follows.

{"statusCode":500,"code":"FST_ERR_BAD_STATUS_CODE","error":"Internal Server Error","message":"FST_ERR_BAD_STATUS_CODE: Called reply with malformed status code"}

Expected behavior

The application should not crash

Your Environment

  • node version: v10.19.0
  • fastify version: 2.13.0
  • os: Mac
@chiku
Copy link
Contributor Author

chiku commented Apr 1, 2020

This is the application stack-trace

FastifyError [FST_ERR_BAD_STATUS_CODE]: FST_ERR_BAD_STATUS_CODE: Called reply with malformed status code
    at _Reply.Reply.code (node_modules/fastify/lib/reply.js:202:11)
    at Object.fastify.get (index.js:4:9)
    at preHandlerCallback (node_modules/fastify/lib/handleRequest.js:111:30)
    at preValidationCallback (node_modules/fastify/lib/handleRequest.js:100:5)
    at handler (node_modules/fastify/lib/handleRequest.js:69:5)
    at handleRequest (node_modules/fastify/lib/handleRequest.js:18:5)
    at onRunMiddlewares (node_modules/fastify/lib/middleware.js:22:5)
    at middlewareCallback (node_modules/fastify/lib/route.js:381:5)
    at Object.routeHandler [as handler] (node_modules/fastify/lib/route.js:359:7)
    at Router.lookup (node_modules/find-my-way/index.js:349:14)

@chiku
Copy link
Contributor Author

chiku commented Apr 1, 2020

I have figured that code fails if it is not defined in node_modules/statuses/codes.json

@Eomm
Copy link
Member

Eomm commented Apr 1, 2020

Good catch!

To reproduce we need to use this endpoint:

fastify.get('/', (request, reply) => {
  reply.code(525).send({ hello: 'world' })
})

with async handler the server doesn't crash.

In the earlier version 2.12, the code was simply set as the user wanted:

curl ...
HTTP/1.1 525 unknown

Moving this issue to the main repo as regression

I think we should:

  • throw if it is not a number
  • throw an error every time only in v3

cc @mcollina
PR that introduce this check: #2082

@Eomm Eomm transferred this issue from fastify/help Apr 1, 2020
@Eomm Eomm added bug Confirmed bug v2.x Issue or pr related to Fastify v2 labels Apr 1, 2020
@mcollina
Copy link
Member

mcollina commented Apr 1, 2020

The bug that #2082 tried to fix is reply.code(undefined). I think an alternative implementation of https://github.com/fastify/fastify/pull/2082/files#diff-d2fdf63c1d7f1d9f68270b6dfa8c930cR201 can be provided to pass the given unit test that does not crash the above code. I do not think we should have a different behavior between v2 and v3, this is just a regression.

@leorossi would you be able to do a follow-up PR?

@leorossi
Copy link
Contributor

leorossi commented Apr 1, 2020

Yes assign it to me please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug v2.x Issue or pr related to Fastify v2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants