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

Handle errors for bad symlinks #219

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading