Skip to content

Server crash: Error: Unknown error #4363

Description

@sveisvei

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: v16.15.0
  • module version with issue: 20.2.2
  • last module version without issue: ?
  • environment (e.g. node, browser, native): node
  • used with hapi application
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

So playing with some invalid headers, I found a bug.

Test case hapi server

"use strict";

const Hapi = require("@hapi/hapi");

const init = async () => {
  const server = Hapi.server({
    port: 8080,
    host: "localhost",
  });

  server.route({
    method: "*",
    path: "/{p*}",
    options: {
      payload: {
        parse: true, // turning this off, will remove the issue
      },
    },
    handler: (req, h) => {
      console.log("handler");
      return h.response("fail").code(404);
    },
  });

  await server.start();
  console.log("Server running on %s", server.info.uri);
};

process.on("unhandledRejection", (err) => {
  console.log(err);
  process.exit(1);
});

init();

command to reproduce the crash:

nc 127.0.0.1 8080 < body.txt

body.txt

POST / HTTP/1.1
Host: localhost
Content-Length: 5
Transfer Encoding:chunked

1
Z\n\Q\n\n

What was the result you got?

Error: Unknown error
    at new module.exports (./node_modules/.pnpm/@hapi+hoek@9.2.1/node_modules/@hapi/hoek/lib/error.js:23:19)
    at Object.module.exports [as assert] (./node_modules/.pnpm/@hapi+hoek@9.2.1/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Response._marshal (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/response.js:566:14)
    at exports.content (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/headers.js:41:24)
    at Object.internals.marshal (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/transmit.js:41:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.internals.fail (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/transmit.js:52:9)
    at async Object.exports.send (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/transmit.js:23:13)
    at async Request._reply (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/request.js:457:9)

Inspecting the request.response surfaces this stack:

Error: Invalid request payload JSON format
      at Object.internals.object (./node_modules/.pnpm/@hapi+subtext@7.0.3/node_modules/@hapi/subtext/lib/index.js:200:32)
      at Object.internals.parse (./node_modules/.pnpm/@hapi+subtext@7.0.3/node_modules/@hapi/subtext/lib/index.js:110:22)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Object.exports.parse (./node_modules/.pnpm/@hapi+subtext@7.0.3/node_modules/@hapi/subtext/lib/index.js:57:30)
      at async internals.payload (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/route.js:430:35)
      at async Request._lifecycle (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/request.js:371:32)
      at async Request._execute (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/request.js:281:9) {
    data: SyntaxError: Unexpected token Z in JSON at position 2
        at JSON.parse (<anonymous>)
        at Object.exports.parse (./node_modules/.pnpm/@hapi+bourne@2.0.0/node_modules/@hapi/bourne/lib/index.js:19:22)
        at Object.internals.object (./node_modules/.pnpm/@hapi+subtext@7.0.3/node_modules/@hapi/subtext/lib/index.js:197:27)
        at Object.internals.parse (./node_modules/.pnpm/@hapi+subtext@7.0.3/node_modules/@hapi/subtext/lib/index.js:110:22)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at async Object.exports.parse (./node_modules/.pnpm/@hapi+subtext@7.0.3/node_modules/@hapi/subtext/lib/index.js:57:30)
        at async internals.payload (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/route.js:430:35)
        at async Request._lifecycle (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/request.js:371:32)
        at async Request._execute (./node_modules/.pnpm/@hapi+hapi@20.2.2/node_modules/@hapi/hapi/lib/request.js:281:9)

So my assumption is that the there is some missing cleanup while handling the actual error response.

What result did you expect?

Server should not die.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions