Unverified Commit 96711b00 authored by Danny Tuppeny's avatar Danny Tuppeny Committed by GitHub

Don't crash on requests for invalid package URLs (#59250)

parent 3efc517a
...@@ -517,12 +517,15 @@ class WebAssetServer implements AssetReader { ...@@ -517,12 +517,15 @@ class WebAssetServer implements AssetReader {
// The file might have been a package file which is signaled by a // The file might have been a package file which is signaled by a
// `/packages/<package>/<path>` request. // `/packages/<package>/<path>` request.
if (segments.first == 'packages') { if (segments.first == 'packages') {
final File packageFile = globals.fs.file(_packages.resolve(Uri( final Uri filePath = _packages.resolve(Uri(
scheme: 'package', pathSegments: segments.skip(1)))); scheme: 'package', pathSegments: segments.skip(1)));
if (filePath != null) {
final File packageFile = globals.fs.file(filePath);
if (packageFile.existsSync()) { if (packageFile.existsSync()) {
return packageFile; return packageFile;
} }
} }
}
// Otherwise it must be a Dart SDK source or a Flutter Web SDK source. // Otherwise it must be a Dart SDK source or a Flutter Web SDK source.
final Directory dartSdkParent = globals.fs final Directory dartSdkParent = globals.fs
......
...@@ -41,7 +41,6 @@ import '../dart/package_map.dart'; ...@@ -41,7 +41,6 @@ import '../dart/package_map.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
import '../project.dart'; import '../project.dart';
import '../web/chrome.dart'; import '../web/chrome.dart';
import 'test_compiler.dart'; import 'test_compiler.dart';
import 'test_config.dart'; import 'test_config.dart';
...@@ -222,6 +221,7 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -222,6 +221,7 @@ class FlutterWebPlatform extends PlatformPlugin {
scheme: 'package', scheme: 'package',
pathSegments: request.requestedUri.pathSegments.skip(1), pathSegments: request.requestedUri.pathSegments.skip(1),
)); ));
if (fileUri != null) {
final String dirname = p.dirname(fileUri.toFilePath()); final String dirname = p.dirname(fileUri.toFilePath());
final String basename = p.basename(fileUri.toFilePath()); final String basename = p.basename(fileUri.toFilePath());
final shelf.Handler handler = createStaticHandler(dirname); final shelf.Handler handler = createStaticHandler(dirname);
...@@ -237,6 +237,7 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -237,6 +237,7 @@ class FlutterWebPlatform extends PlatformPlugin {
); );
return handler(modifiedRequest); return handler(modifiedRequest);
} }
}
return shelf.Response.notFound('Not Found'); return shelf.Response.notFound('Not Found');
} }
......
...@@ -364,6 +364,13 @@ void main() { ...@@ -364,6 +364,13 @@ void main() {
expect(response.statusCode, HttpStatus.notFound); expect(response.statusCode, HttpStatus.notFound);
})); }));
test('handles serving unresolvable package file', () => testbed.run(() async {
final Response response = await webAssetServer
.handleRequest(Request('GET', Uri.parse('http://foobar/packages/notpackage/file')));
expect(response.statusCode, HttpStatus.notFound);
}));
test('serves /packages/<package>/<path> files as if they were ' test('serves /packages/<package>/<path> files as if they were '
'package:<package>/<path> uris', () => testbed.run(() async { 'package:<package>/<path> uris', () => testbed.run(() async {
final Uri expectedUri = packages.resolve( final Uri expectedUri = packages.resolve(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment