Skip to content

Error: Failed to open file: ENAMETOOLONG #170

Description

@elawad

Support plan

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

Context

  • node version: 20.12.0
  • module version with issue: 7.1.0
  • last module version without issue: n/a
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi
  • any other relevant information: n/a

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

Using the Directory Handler, if the filename param exceeds the 255 limit, an ENAMETOOLONG error is thrown.
Could this error be gracefully handled by Inert while using the Directory Handler?

import Hapi from '@hapi/hapi';
import Inert from '@hapi/inert';

const server = Hapi.server({
  port: 3000,
  debug: { request: '*' },
});

(async () => {
  await server.register([Inert]);

  server.route({
    method: 'GET',
    path: '/{filename*}',
    handler: {
      directory: { path: 'public' },
    },

    /* // Uncomment to gracefully handle error.
    options: {
      pre: [
        (request) => {
          const { filename } = request.params;
          if (filename?.length > 255) throw Boom.notFound();
          return null;
        },
      ],
    },
    */
  });

  await server.start();
})();

Make a GET request to: (filename is 256 chars, exceeding the 255 limit)
http://localhost:3000/123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456

What was the result you got?

Error: Failed to open file: ENAMETOOLONG: name too long

What result did you expect?

Possibly a 404, similar to how other invalid paths are handled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions