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,10 +517,13 @@ class WebAssetServer implements AssetReader { ...@@ -517,10 +517,13 @@ 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 (packageFile.existsSync()) { if (filePath != null) {
return packageFile; final File packageFile = globals.fs.file(filePath);
if (packageFile.existsSync()) {
return packageFile;
}
} }
} }
......
...@@ -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,20 +221,22 @@ class FlutterWebPlatform extends PlatformPlugin { ...@@ -222,20 +221,22 @@ class FlutterWebPlatform extends PlatformPlugin {
scheme: 'package', scheme: 'package',
pathSegments: request.requestedUri.pathSegments.skip(1), pathSegments: request.requestedUri.pathSegments.skip(1),
)); ));
final String dirname = p.dirname(fileUri.toFilePath()); if (fileUri != null) {
final String basename = p.basename(fileUri.toFilePath()); final String dirname = p.dirname(fileUri.toFilePath());
final shelf.Handler handler = createStaticHandler(dirname); final String basename = p.basename(fileUri.toFilePath());
final shelf.Request modifiedRequest = shelf.Request( final shelf.Handler handler = createStaticHandler(dirname);
request.method, final shelf.Request modifiedRequest = shelf.Request(
request.requestedUri.replace(path: basename), request.method,
protocolVersion: request.protocolVersion, request.requestedUri.replace(path: basename),
headers: request.headers, protocolVersion: request.protocolVersion,
handlerPath: request.handlerPath, headers: request.headers,
url: request.url.replace(path: basename), handlerPath: request.handlerPath,
encoding: request.encoding, url: request.url.replace(path: basename),
context: request.context, encoding: request.encoding,
); context: request.context,
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