Skip to content

Commit

Permalink
Handle errors for bad symlinks (#219)
Browse files Browse the repository at this point in the history
* Handle errors for missing symlinks

* Fix test
  • Loading branch information
AndyBitz authored Oct 15, 2024
1 parent fe7d998 commit 974a4fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,20 @@ module.exports = async (request, response, config = {}, methods = {}) => {
// resolve the symlink and run a new `stat` call just for the
// target of that symlink.
if (isSymLink) {
absolutePath = await handlers.realpath(absolutePath);
try {
absolutePath = await handlers.realpath(absolutePath);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}

// The requested symlink is invalid
return handlers.sendError(absolutePath, response, acceptsJSON, current, handlers, config, {
statusCode: 404,
code: 'not_found',
message: 'The requested path could not be found'
});
}
stats = await handlers.lstat(absolutePath);
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/symlinks/a-bad-link
14 changes: 14 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,20 @@ test('allow symlinks by setting the option', async () => {
expect(text).toBe(spec);
});

test('A bad symlink should be a 404', async () => {
const name = 'symlinks/a-bad-link';

const url = await getUrl({
symlinks: true
});

const response = await fetch(`${url}/${name}`);
expect(response.status).toBe(404);

const text = await response.text();
expect(text.trim()).toBe('<span>Not Found</span>');
});

test('etag header is set', async () => {
const url = await getUrl({
renderSingle: true,
Expand Down

0 comments on commit 974a4fe

Please sign in to comment.