Directory listings fail with a 500 when path is absolute
- Dominant language
- JavaScript
- Stars
- 236
- Forks
- 48
- PR merge metrics
- No merged PRs in 30d
Description
#### 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 https://github.com/hapijs/inert/commit/4514a5e367aeb03be94fc04294c5b30e509eebef
#### 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.
```js
'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.
Contributor guide
Assessment
This issue has not been assessed yet.