hapijs / inert

Static file and directory handlers for hapi.js
Other
238 stars 49 forks source link

Directory listings fail with a 500 when path is absolute #145

Open sholladay opened 4 years ago

sholladay commented 4 years ago

Support plan

Context

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

Trying to serve a directory based on an absolute path, as shown in the example below. Using the example, visiting / in the browser fails to render the directory listing. Visiting a subpath that is a file does work, such as /app.js. However, subpaths that are directories also fail to render their directory listing.

In the example, removing path.resolve() "fixes" the issue. Please note that, in my real app, I have path set to a function and the actual value is determined by some business logic that walks the filesystem and returns an absolute path.

'use strict';

const path = require('path');
const hapi = require('@hapi/hapi');
const inert = require('@hapi/inert');

const server = hapi.server({
    debug : {
        log     : ['error'],
        request : ['error']
    },
    port : 3000
});

const start = async () => {
    await server.register([inert]);

    server.route({
        method : 'GET',
        path   : '/{filepath*}',
        handler : {
            directory : {
                path    : path.resolve('build'),
                listing : true
            }
        }
    });

    await server.start();
};

start();

What was the result you got?

A 500 Internal Server Error, which seems to be caused by inert joining an absolute path with another absolute path, resulting in a path that does not exist. For some reason, this doesn't happen on subpaths that are files.

What result did you expect?

The directory listing should be shown.

jasonswearingen commented 2 years ago

I also am seeing this error, a regression from old Hapi v16 which I'm upgrading from

        _server.route( {
            method: [ 'GET', 'POST' ],
            path: '/examples/corpus/{param*}',
            config: {
                //payload: { parse: true },
                //cors: true,jsonp:"callback",
            },
            handler: {
                directory: {
                    path: nlib.path.join( __dirname, "../../www/corpus/" ),
                    listing: true,
                    redirectToSlash: true,
                    index: [ "index.html" ],
                    showHidden: false,
                    lookupCompressed: true,
                    defaultExtension: "html",
                }
            }
        } );

Switching to relative path: "./www/corpus/", works

I'm using

  "@hapi/hapi": "20.2.1",
    "@hapi/inert": "^6.0.4",

FYI full path works on Windows, not on Ubuntu 20.04.