Skip to content

Commit 63978ed

Browse files
committed
[flutter_tools] Resolve /packages/ requests below the package root
`_resolveDartFile` no longer builds a `package:` URI from the request segments. It looks the package up by name and resolves the rest of the path below that package's `packageUriRoot` with `resolveRequestPathUnder`, like the other search roots. An absolute path after the package name (`packages/<package>//<path>`) can no longer replace the package root, and a request with nothing after the package name (`packages/foo.dart`) is answered instead of throwing an `ArgumentError` out of `PackageConfig.resolve`. This replaces the dot-segment check: containment handles dot segments here the same way it does for the other roots. `ReleaseAssetServer` now checks a root's allowed extensions against the request path before resolving it, so regular asset requests skip resolution for the source-map-only roots.
1 parent b1149ce commit 63978ed

3 files changed

Lines changed: 41 additions & 20 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@ class ReleaseAssetServer {
8585
if (request.url.toString() == 'main.dart') {
8686
fileUri = entrypoint;
8787
} else {
88+
final String requestExtension = _fileSystem.path.extension(requestPath);
8889
for (final (Uri uri, Set<String> allowedExtensions) in _searchPaths()) {
90+
if (allowedExtensions.isNotEmpty && !allowedExtensions.contains(requestExtension)) {
91+
// This root only serves source-map related files; skip anything else
92+
// so unrelated project or SDK files are not exposed. A dot segment
93+
// cannot change the extension: resolution keeps the last segment
94+
// unless it is `.` or `..`, which have no extension.
95+
continue;
96+
}
8997
final Uri? potential = resolveRequestPathUnder(
9098
uri,
9199
requestPath,
@@ -97,12 +105,6 @@ class ReleaseAssetServer {
97105
continue;
98106
}
99107
final String potentialPath = potential.toFilePath(windows: _platform.isWindows);
100-
if (allowedExtensions.isNotEmpty &&
101-
!allowedExtensions.contains(_fileSystem.path.extension(potentialPath))) {
102-
// This root only serves source-map related files; skip anything else
103-
// so unrelated project or SDK files are not exposed.
104-
continue;
105-
}
106108
if (_fileSystem.isFileSync(potentialPath)) {
107109
fileUri = potential;
108110
break;

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -744,20 +744,23 @@ _flutter.buildConfig = ${jsonEncode(buildConfig)};
744744
}
745745

746746
// The file might have been a package file which is signaled by a
747-
// `/packages/<package>/<path>` request.
748-
// A dot segment that climbs past the package name makes
749-
// `PackageConfig.resolve` throw.
750-
final bool hasDotSegment = segments.any(
751-
(String segment) => segment == '.' || segment == '..',
752-
);
753-
if (segments.first == 'packages' && !hasDotSegment) {
754-
final Uri? filePath = _packages.resolve(
755-
Uri(scheme: 'package', pathSegments: segments.skip(1)),
756-
);
757-
if (filePath != null) {
758-
final File packageFile = fs.file(filePath);
759-
if (packageFile.existsSync()) {
760-
return packageFile;
747+
// `/packages/<package>/<path>` request. The rest of the path is resolved
748+
// below the package's root like any other search root, rather than
749+
// through a `package:` URI built from the request segments.
750+
if (segments.length >= 3 && segments.first == 'packages') {
751+
final Package? package = _packages[segments[1]];
752+
if (package != null) {
753+
final Uri? packageFileUri = resolveRequestPathUnder(
754+
package.packageUriRoot,
755+
segments.skip(2).join('/'),
756+
fileSystem: fs,
757+
windows: platform.isWindows,
758+
);
759+
if (packageFileUri != null) {
760+
final File packageFile = fs.file(packageFileUri);
761+
if (packageFile.existsSync()) {
762+
return packageFile;
763+
}
761764
}
762765
}
763766
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,12 @@ void main() {
853853
),
854854
);
855855
expect((await response.read().toList()).first, source.readAsBytesSync());
856+
857+
// A dot segment that stays inside the package root still resolves.
858+
expect(
859+
await webAssetServer.dartSourceContents('packages/flutter_tools/src/../foo.dart'),
860+
source.readAsStringSync(),
861+
);
856862
},
857863
);
858864

@@ -1752,6 +1758,11 @@ void main() {
17521758
// Encoded separators, which do not name a file path once decoded.
17531759
'..%2F..%2Fsecret.txt',
17541760
'assets/..%2F..%2F..%2Fhome%2Fuser%2Fsecret.txt',
1761+
// An absolute path after the package name, which a package URI resolves
1762+
// against the package root as a root-relative reference.
1763+
'packages/flutter_tools//home/user/secret.dart',
1764+
// No path after the package name, which is not a valid package URI.
1765+
'packages/secret.dart',
17551766
];
17561767

17571768
for (final path in requestPaths) {
@@ -1777,6 +1788,11 @@ void main() {
17771788
await webAssetServer.dartSourceContents('packages/flutter_tools/../../secret.dart'),
17781789
isNull,
17791790
);
1791+
expect(
1792+
await webAssetServer.dartSourceContents('packages/flutter_tools//home/user/secret.dart'),
1793+
isNull,
1794+
);
1795+
expect(await webAssetServer.dartSourceContents('packages/secret.dart'), isNull);
17801796

17811797
// Again behind a base path, which leaves a colon in a later segment
17821798
// unencoded on its way to the handler.

0 commit comments

Comments
 (0)