From 5d6321b509a7410d47046e31db6e8db531a573cd Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Fri, 2 Oct 2020 11:02:03 -0700 Subject: [PATCH] [web] Respond with 404 to non-found asset or package files (#67088) --- .../lib/src/isolated/devfs_web.dart | 5 +++ .../general.shard/web/devfs_web_test.dart | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/packages/flutter_tools/lib/src/isolated/devfs_web.dart b/packages/flutter_tools/lib/src/isolated/devfs_web.dart index d5c9ec5ead..83ff7b443c 100644 --- a/packages/flutter_tools/lib/src/isolated/devfs_web.dart +++ b/packages/flutter_tools/lib/src/isolated/devfs_web.dart @@ -406,6 +406,11 @@ class WebAssetServer implements AssetReader { } if (!file.existsSync()) { + // Paths starting with these prefixes should've been resolved above. + if (requestPath.startsWith('assets/') || + requestPath.startsWith('packages/')) { + return shelf.Response.notFound(''); + } return _serveIndex(); } diff --git a/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart b/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart index a68070a810..dae427070d 100644 --- a/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart +++ b/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart @@ -260,6 +260,47 @@ void main() { expect(await response.readAsString(), htmlContent); })); + test('does not serve outside the base path', () => testbed.run(() async { + webAssetServer.basePath = 'base/path'; + + const String htmlContent = ''; + final Directory webDir = globals.fs.currentDirectory + .childDirectory('web') + ..createSync(); + webDir.childFile('index.html').writeAsStringSync(htmlContent); + + final Response response = await webAssetServer + .handleRequest(Request('GET', Uri.parse('http://foobar/'))); + + expect(response.statusCode, HttpStatus.notFound); + })); + + test('does not serve index.html when path is inside assets or packages', () => testbed.run(() async { + const String htmlContent = ''; + final Directory webDir = globals.fs.currentDirectory + .childDirectory('web') + ..createSync(); + webDir.childFile('index.html').writeAsStringSync(htmlContent); + + Response response = await webAssetServer + .handleRequest(Request('GET', Uri.parse('http://foobar/assets/foo/bar.png'))); + expect(response.statusCode, HttpStatus.notFound); + + response = await webAssetServer + .handleRequest(Request('GET', Uri.parse('http://foobar/packages/foo/bar.dart.js'))); + expect(response.statusCode, HttpStatus.notFound); + + webAssetServer.basePath = 'base/path'; + + response = await webAssetServer + .handleRequest(Request('GET', Uri.parse('http://foobar/base/path/assets/foo/bar.png'))); + expect(response.statusCode, HttpStatus.notFound); + + response = await webAssetServer + .handleRequest(Request('GET', Uri.parse('http://foobar/base/path/packages/foo/bar.dart.js'))); + expect(response.statusCode, HttpStatus.notFound); + })); + test('serves default index.html', () => testbed.run(() async { final Response response = await webAssetServer .handleRequest(Request('GET', Uri.parse('http://foobar/')));