Skip to content

Commit 7c0ccea

Browse files
authored
[flutter_tools] Restrict WebAssetServer source resolution to source map extensions (flutter#191501)
## Description Restricts `_resolveDartFile` in `WebAssetServer` to only resolve files with source map extensions (`.dart`, `.map`) from the project root, package, and SDK paths, aligning behavior with `ReleaseAssetServer`. Previously, `WebAssetServer._resolveDartFile` attempted to resolve any relative path directly against `fileSystem.currentDirectory`, which caused raw project files (such as `<root>/assets/...`) to be served directly with HTTP 200 when requested by debug web runtime asset loaders, masking invalid asset key references that failed on release web and native platforms. ## Issues Fixed Fixes flutter#84346 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 9df0f58 commit 7c0ccea

2 files changed

Lines changed: 194 additions & 48 deletions

File tree

‎packages/flutter_tools/lib/src/isolated/web_asset_server.dart‎

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class WebAssetServer implements AssetReader {
526526
// Try and resolve the path relative to the built asset directory.
527527
if (!file.existsSync()) {
528528
final Uri potential = fileSystem
529-
.directory(getAssetBuildDirectory())
529+
.directory(getAssetBuildDirectory(null, fileSystem))
530530
.uri
531531
.resolve(requestPath.replaceFirst('assets/', ''));
532532
file = fileSystem.file(potential);
@@ -692,67 +692,75 @@ _flutter.buildConfig = ${jsonEncode(buildConfig)};
692692
);
693693
}
694694

695-
// Attempt to resolve `path` to a dart file.
695+
/// File extensions that may legitimately be requested from the project and
696+
/// Flutter SDK roots for source-map resolution.
697+
static const _sourceMapExtensions = <String>{'.dart', '.map'};
698+
699+
/// Attempts to resolve [path] to a dart file.
696700
File _resolveDartFile(String path) {
697701
// Return the actual file objects so that local engine changes are automatically picked up.
698702
switch (path) {
699703
case 'dart_sdk.js':
700704
return _resolveDartSdkJsFile;
701705
case 'dart_sdk.js.map':
702706
return _resolveDartSdkJsMapFile;
703-
}
704-
// This is the special generated entrypoint.
705-
if (path == 'web_entrypoint.dart') {
706-
return entrypointCacheDirectory.childFile('web_entrypoint.dart');
707-
}
708-
709-
// If this is a dart file, it must be on the local file system and is
710-
// likely coming from a source map request. The tool doesn't currently
711-
// consider the case of Dart files as assets.
712-
final File dartFile = fileSystem.file(fileSystem.currentDirectory.uri.resolve(path));
713-
if (dartFile.existsSync()) {
714-
return dartFile;
715-
}
707+
case 'web_entrypoint.dart':
708+
return entrypointCacheDirectory.childFile('web_entrypoint.dart');
709+
}
710+
711+
final String extension = fileSystem.path.extension(path);
712+
if (_sourceMapExtensions.contains(extension)) {
713+
// If this is a dart file, it must be on the local file system and is
714+
// likely coming from a source map request. The tool doesn't currently
715+
// consider the case of Dart files as assets.
716+
final File dartFile = fileSystem.file(fileSystem.currentDirectory.uri.resolve(path));
717+
if (dartFile.existsSync()) {
718+
return dartFile;
719+
}
716720

717-
final List<String> segments = path.split('/');
718-
if (segments.first.isEmpty) {
719-
segments.removeAt(0);
720-
}
721+
final List<String> segments = path.split('/');
722+
if (segments.first.isEmpty) {
723+
segments.removeAt(0);
724+
}
721725

722-
// The file might have been a package file which is signaled by a
723-
// `/packages/<package>/<path>` request.
724-
if (segments.first == 'packages') {
725-
final Uri? filePath = _packages.resolve(
726-
Uri(scheme: 'package', pathSegments: segments.skip(1)),
727-
);
728-
if (filePath != null) {
729-
final File packageFile = fileSystem.file(filePath);
730-
if (packageFile.existsSync()) {
731-
return packageFile;
726+
// The file might have been a package file which is signaled by a
727+
// `/packages/<package>/<path>` request.
728+
if (segments.first == 'packages') {
729+
final Uri? filePath = _packages.resolve(
730+
Uri(scheme: 'package', pathSegments: segments.skip(1)),
731+
);
732+
if (filePath != null) {
733+
final File packageFile = fileSystem.file(filePath);
734+
if (packageFile.existsSync()) {
735+
return packageFile;
736+
}
732737
}
733738
}
734-
}
735739

736-
// Otherwise it must be a Dart SDK source or a Flutter Web SDK source.
737-
final Directory dartSdkParent = fileSystem
738-
.directory(
739-
globals.artifacts!.getArtifactPath(
740-
Artifact.engineDartSdkPath,
741-
platform: TargetPlatform.web_javascript,
742-
),
743-
)
744-
.parent;
745-
final File dartSdkFile = fileSystem.file(dartSdkParent.uri.resolve(path));
746-
if (dartSdkFile.existsSync()) {
747-
return dartSdkFile;
748-
}
740+
// Otherwise it must be a Dart SDK source or a Flutter Web SDK source.
741+
final Directory dartSdkParent = fileSystem
742+
.directory(
743+
globals.artifacts!.getArtifactPath(
744+
Artifact.engineDartSdkPath,
745+
platform: TargetPlatform.web_javascript,
746+
),
747+
)
748+
.parent;
749+
final File dartSdkFile = fileSystem.file(dartSdkParent.uri.resolve(path));
750+
if (dartSdkFile.existsSync()) {
751+
return dartSdkFile;
752+
}
749753

750-
final Directory flutterWebSdk = fileSystem.directory(
751-
globals.artifacts!.getHostArtifact(HostArtifact.flutterWebSdk),
752-
);
753-
final File webSdkFile = fileSystem.file(flutterWebSdk.uri.resolve(path));
754+
final Directory flutterWebSdk = fileSystem.directory(
755+
globals.artifacts!.getHostArtifact(HostArtifact.flutterWebSdk),
756+
);
757+
final File webSdkFile = fileSystem.file(flutterWebSdk.uri.resolve(path));
758+
if (webSdkFile.existsSync()) {
759+
return webSdkFile;
760+
}
761+
}
754762

755-
return webSdkFile;
763+
return fileSystem.currentDirectory.childFile('.non_existent_file');
756764
}
757765

758766
File get _resolveDartSdkJsFile {

‎packages/flutter_tools/test/general.shard/web/web_asset_server_test.dart‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:dwds/dwds.dart';
66
import 'package:file/memory.dart';
7+
import 'package:flutter_tools/src/artifacts.dart';
78
import 'package:flutter_tools/src/base/file_system.dart';
89
import 'package:flutter_tools/src/base/io.dart';
910
import 'package:flutter_tools/src/base/logger.dart';
@@ -17,6 +18,7 @@ import 'package:flutter_tools/src/web/web_constants.dart';
1718
import 'package:shelf/shelf.dart';
1819

1920
import '../../src/common.dart';
21+
import '../../src/context.dart';
2022

2123
const kTransparentImage = <int>[
2224
0x89,
@@ -509,5 +511,141 @@ void main() {
509511

510512
expect(server.basePath, isEmpty);
511513
});
514+
515+
testUsingContext(
516+
'serves assets with exact key and does not serve non-source project files as assets',
517+
() async {
518+
final WebAssetServer server = await WebAssetServer.start(
519+
null,
520+
null,
521+
false,
522+
false,
523+
false,
524+
BuildInfo.debug,
525+
false,
526+
const DartDevelopmentServiceConfiguration(enable: false),
527+
Uri.base,
528+
null,
529+
crossOriginIsolation: false,
530+
webDevServerConfig: const WebDevServerConfig(host: 'localhost'),
531+
webRenderer: WebRendererMode.canvaskit,
532+
isWasm: false,
533+
useLocalCanvasKit: false,
534+
testMode: true,
535+
fileSystem: fileSystem,
536+
logger: BufferLogger.test(),
537+
platform: platform,
538+
);
539+
540+
// Project source file exists in assets/ directory.
541+
fileSystem.file('assets/my_asset.txt')
542+
..createSync(recursive: true)
543+
..writeAsStringSync('project file');
544+
545+
// Built asset exists in build/flutter_assets/assets/my_asset.txt.
546+
fileSystem.file('build/flutter_assets/assets/my_asset.txt')
547+
..createSync(recursive: true)
548+
..writeAsStringSync('built asset');
549+
550+
// Correct key 'assets/my_asset.txt' (URL /assets/assets/my_asset.txt) succeeds.
551+
final Response validResponse = await server.handleRequest(
552+
Request('GET', Uri.parse('http://localhost:8080/assets/assets/my_asset.txt')),
553+
);
554+
expect(validResponse.statusCode, HttpStatus.ok);
555+
expect(await validResponse.readAsString(), 'built asset');
556+
557+
// Incorrect key 'my_asset.txt' (URL /assets/my_asset.txt) must return 404.
558+
final Response invalidResponse = await server.handleRequest(
559+
Request('GET', Uri.parse('http://localhost:8080/assets/my_asset.txt')),
560+
);
561+
expect(invalidResponse.statusCode, HttpStatus.notFound);
562+
563+
// Legitimate source files (e.g. lib/main.dart) are served for debugging.
564+
fileSystem.file('lib/main.dart').writeAsStringSync('void main() {}');
565+
final Response dartSourceResponse = await server.handleRequest(
566+
Request('GET', Uri.parse('http://localhost:8080/lib/main.dart')),
567+
);
568+
expect(dartSourceResponse.statusCode, HttpStatus.ok);
569+
expect(await dartSourceResponse.readAsString(), 'void main() {}');
570+
},
571+
overrides: <Type, Generator>{
572+
Artifacts: () => Artifacts.test(),
573+
FileSystem: () => fileSystem,
574+
ProcessManager: () => FakeProcessManager.any(),
575+
},
576+
);
577+
578+
group('on Windows', () {
579+
late FileSystem windowsFileSystem;
580+
late Platform windowsPlatform;
581+
582+
setUp(() {
583+
windowsFileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
584+
windowsPlatform = FakePlatform(
585+
operatingSystem: 'windows',
586+
environment: <String, String>{'HOME': r'C:\Users\test'},
587+
);
588+
windowsFileSystem.file('lib/main.dart').createSync(recursive: true);
589+
windowsFileSystem.file('web/index.html')
590+
..createSync(recursive: true)
591+
..writeAsStringSync('hello');
592+
});
593+
594+
testUsingContext(
595+
'serves assets with exact key on Windows filesystem',
596+
() async {
597+
final WebAssetServer server = await WebAssetServer.start(
598+
null,
599+
null,
600+
false,
601+
false,
602+
false,
603+
BuildInfo.debug,
604+
false,
605+
const DartDevelopmentServiceConfiguration(enable: false),
606+
Uri.base,
607+
null,
608+
crossOriginIsolation: false,
609+
webDevServerConfig: const WebDevServerConfig(host: 'localhost'),
610+
webRenderer: WebRendererMode.canvaskit,
611+
isWasm: false,
612+
useLocalCanvasKit: false,
613+
testMode: true,
614+
fileSystem: windowsFileSystem,
615+
logger: BufferLogger.test(),
616+
platform: windowsPlatform,
617+
);
618+
619+
// Project source file exists in assets/ directory.
620+
windowsFileSystem.file(r'assets\my_asset.txt')
621+
..createSync(recursive: true)
622+
..writeAsStringSync('project file');
623+
624+
// Built asset exists in build/flutter_assets/assets/my_asset.txt.
625+
windowsFileSystem.file(r'build\flutter_assets\assets\my_asset.txt')
626+
..createSync(recursive: true)
627+
..writeAsStringSync('built asset');
628+
629+
// Correct key 'assets/my_asset.txt' (URL /assets/assets/my_asset.txt) succeeds.
630+
final Response validResponse = await server.handleRequest(
631+
Request('GET', Uri.parse('http://localhost:8080/assets/assets/my_asset.txt')),
632+
);
633+
expect(validResponse.statusCode, HttpStatus.ok);
634+
expect(await validResponse.readAsString(), 'built asset');
635+
636+
// Incorrect key 'my_asset.txt' (URL /assets/my_asset.txt) must return 404.
637+
final Response invalidResponse = await server.handleRequest(
638+
Request('GET', Uri.parse('http://localhost:8080/assets/my_asset.txt')),
639+
);
640+
expect(invalidResponse.statusCode, HttpStatus.notFound);
641+
},
642+
overrides: <Type, Generator>{
643+
Artifacts: () => Artifacts.test(),
644+
FileSystem: () => windowsFileSystem,
645+
Platform: () => windowsPlatform,
646+
ProcessManager: () => FakeProcessManager.any(),
647+
},
648+
);
649+
});
512650
});
513651
}

0 commit comments

Comments
 (0)