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

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

Open
sholladay opened this issue Jul 2, 2020 · 1 comment
Open

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

sholladay opened this issue Jul 2, 2020 · 1 comment
Labels
bug Bug or defect

Comments

@sholladay
Copy link

sholladay commented Jul 2, 2020

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Community
  • is this issue currently blocking your project? (yes/no): No
  • is this issue affecting a production system? (yes/no): No

Context

  • node version: 14.1.0
  • module version with issue: 5.1.3 through 6.0.1
  • last module version without issue: 5.1.2
  • environment (e.g. node, browser, native): Node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi
  • any other relevant information: I believe this regression was caused by 4514a5e

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.

@sholladay sholladay added the support Questions, discussions, and general support label Jul 2, 2020
@sholladay sholladay changed the title Directory listings faill with a 500 when path is absolute Directory listings fail with a 500 when path is absolute Jul 2, 2020
@devinivy devinivy added bug Bug or defect and removed support Questions, discussions, and general support labels Sep 2, 2020
@jasonswearingen
Copy link

jasonswearingen commented Feb 18, 2022

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.

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

No branches or pull requests

3 participants