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

fix a crash due to bad symlinks #193

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -697,7 +697,19 @@ 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);
}

1 change: 1 addition & 0 deletions test/fixtures/symlinks/a-bad-link
15 changes: 15 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -1344,6 +1344,21 @@ test('allow symlinks by setting the option', async t => {
t.is(text, spec);
});

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

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

const response = await fetch(`${url}/${name}`);

t.is(response.status, 404);

const text = await response.text();
t.is(text.trim(), '<span>Not Found</span>');
});

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