[web] Respond with 404 to non-found asset or package files (#67088)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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/')));
|
||||
|
||||
Reference in New Issue
Block a user