From 8a1505f4f0e6f3bf8e4ba9a68267a0e9733a41f4 Mon Sep 17 00:00:00 2001 From: Andy Bitz Date: Tue, 15 Oct 2024 14:46:46 +0200 Subject: [PATCH 1/2] Handle errors for missing symlinks --- src/index.js | 14 +++++++++++++- test/fixtures/symlinks/a-bad-link | 1 + test/integration.test.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/symlinks/a-bad-link diff --git a/src/index.js b/src/index.js index 34c2501..008ae8f 100644 --- a/src/index.js +++ b/src/index.js @@ -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); } diff --git a/test/fixtures/symlinks/a-bad-link b/test/fixtures/symlinks/a-bad-link new file mode 100644 index 0000000..7148ef6 --- /dev/null +++ b/test/fixtures/symlinks/a-bad-link @@ -0,0 +1 @@ +not-a-file diff --git a/test/integration.test.js b/test/integration.test.js index d0e5d67..8613f77 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -1343,6 +1343,21 @@ test('allow symlinks by setting the option', async () => { expect(text).toBe(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}`); + + expect(response.status).toBe(404); + + const text = await response.text(); + expect(text.trim()).toBe('Not Found'); +}); + test('etag header is set', async () => { const url = await getUrl({ renderSingle: true, From cb68ec90083b7b224163bcf414aa608f5ea22fb3 Mon Sep 17 00:00:00 2001 From: Andy Bitz Date: Tue, 15 Oct 2024 14:52:32 +0200 Subject: [PATCH 2/2] Fix test --- src/index.js | 1 + test/fixtures/symlinks/a-bad-link | 2 +- test/integration.test.js | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 120000 test/fixtures/symlinks/a-bad-link diff --git a/src/index.js b/src/index.js index 008ae8f..564f012 100644 --- a/src/index.js +++ b/src/index.js @@ -703,6 +703,7 @@ module.exports = async (request, response, config = {}, methods = {}) => { if (err.code !== 'ENOENT') { throw err; } + // The requested symlink is invalid return handlers.sendError(absolutePath, response, acceptsJSON, current, handlers, config, { statusCode: 404, diff --git a/test/fixtures/symlinks/a-bad-link b/test/fixtures/symlinks/a-bad-link deleted file mode 100644 index 7148ef6..0000000 --- a/test/fixtures/symlinks/a-bad-link +++ /dev/null @@ -1 +0,0 @@ -not-a-file diff --git a/test/fixtures/symlinks/a-bad-link b/test/fixtures/symlinks/a-bad-link new file mode 120000 index 0000000..7a904be --- /dev/null +++ b/test/fixtures/symlinks/a-bad-link @@ -0,0 +1 @@ +not-a-file \ No newline at end of file diff --git a/test/integration.test.js b/test/integration.test.js index 8613f77..cebc479 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -1343,7 +1343,7 @@ test('allow symlinks by setting the option', async () => { expect(text).toBe(spec); }); -test('A bad symlink should be a 404', async t => { +test('A bad symlink should be a 404', async () => { const name = 'symlinks/a-bad-link'; const url = await getUrl({ @@ -1351,7 +1351,6 @@ test('A bad symlink should be a 404', async t => { }); const response = await fetch(`${url}/${name}`); - expect(response.status).toBe(404); const text = await response.text();