[web] Respond with 404 to non-found asset or package files (#67088)

This commit is contained in:
Mouad Debbar
2020-10-02 11:02:03 -07:00
committed by GitHub
parent 385ae40218
commit 5d6321b509
2 changed files with 46 additions and 0 deletions

View File

@@ -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();
}

View File

@@ -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 = '<html><head></head><body id="test"></body></html>';
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 = '<html><head></head><body id="test"></body></html>';
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/')));