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 {
// The file might have been a package file which is signaled by a
// `/packages/<package>/<path>` request.
if (segments.first == 'packages') {
final File packageFile = globals.fs.file(_packages.resolve(Uri(
scheme: 'package', pathSegments: segments.skip(1))));
if (packageFile.existsSync()) {
return packageFile;
final Uri filePath = _packages.resolve(Uri(
scheme: 'package', pathSegments: segments.skip(1)));
if (filePath != null) {
final File packageFile = globals.fs.file(filePath);
if (packageFile.existsSync()) {
return packageFile;
}
}
}
......
......@@ -41,7 +41,6 @@ import '../dart/package_map.dart';
import '../globals.dart' as globals;
import '../project.dart';
import '../web/chrome.dart';
import 'test_compiler.dart';
import 'test_config.dart';
......@@ -222,20 +221,22 @@ class FlutterWebPlatform extends PlatformPlugin {
scheme: 'package',
pathSegments: request.requestedUri.pathSegments.skip(1),
));
final String dirname = p.dirname(fileUri.toFilePath());
final String basename = p.basename(fileUri.toFilePath());
final shelf.Handler handler = createStaticHandler(dirname);
final shelf.Request modifiedRequest = shelf.Request(
request.method,
request.requestedUri.replace(path: basename),
protocolVersion: request.protocolVersion,
headers: request.headers,
handlerPath: request.handlerPath,
url: request.url.replace(path: basename),
encoding: request.encoding,
context: request.context,
);
return handler(modifiedRequest);
if (fileUri != null) {
final String dirname = p.dirname(fileUri.toFilePath());
final String basename = p.basename(fileUri.toFilePath());
final shelf.Handler handler = createStaticHandler(dirname);
final shelf.Request modifiedRequest = shelf.Request(
request.method,
request.requestedUri.replace(path: basename),
protocolVersion: request.protocolVersion,
headers: request.headers,
handlerPath: request.handlerPath,
url: request.url.replace(path: basename),
encoding: request.encoding,
context: request.context,
);
return handler(modifiedRequest);
}
}
return shelf.Response.notFound('Not Found');
}
......
......@@ -364,6 +364,13 @@ void main() {
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 '
'package:<package>/<path> uris', () => testbed.run(() async {
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