Skip to content

Commit 91ffada

Browse files
molchanoviamdebbar
andauthored
[flutter_tools] Fix local CanvasKit asset paths on Windows (flutter#193776)
On Windows, converting a CanvasKit request URI to a filesystem path before matching the `canvaskit/` prefix causes valid asset requests to return HTTP 404. Match the URI path segments first, then build the asset path with the filesystem's path context. Adds hermetic regression coverage for POSIX and Windows paths, including root and Chromium JavaScript/Wasm assets, response bodies and headers, and rejection of invalid CanvasKit paths. Fixes flutter#193505. ## 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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: Mouad Debbar <[email protected]>
1 parent 6e16887 commit 91ffada

2 files changed

Lines changed: 168 additions & 13 deletions

File tree

‎packages/flutter_tools/lib/src/test/flutter_web_platform.dart‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,10 @@ class FlutterWebPlatform extends PlatformPlugin {
276276
File get _dartSdkSourcemaps =>
277277
_fileSystem.file(_artifacts.getHostArtifact(kDDCSdkSourcemapsArtifactMap[webRenderer]!));
278278

279-
File _canvasKitFile(String relativePath) {
280-
final String canvasKitPath = _fileSystem.path.join(
281-
_artifacts.getHostArtifact(HostArtifact.flutterWebSdk).path,
282-
'canvaskit',
283-
);
284-
final File canvasKitFile = _fileSystem.file(_fileSystem.path.join(canvasKitPath, relativePath));
285-
return canvasKitFile;
286-
}
279+
String get _canvasKitPath => _fileSystem.path.join(
280+
_artifacts.getHostArtifact(HostArtifact.flutterWebSdk).path,
281+
'canvaskit',
282+
);
287283

288284
Future<shelf.Response> _handleTestRequest(shelf.Request request) async {
289285
if (request.url.path.endsWith('main.dart.browser_test.dart.js')) {
@@ -475,13 +471,19 @@ window.\$dartLoader.loader.nextAttempt();
475471
/// Serves a local build of CanvasKit, replacing the CDN build, which can
476472
/// cause test flakiness due to reliance on network.
477473
shelf.Response _localCanvasKitHandler(shelf.Request request) {
478-
final String fullPath = _fileSystem.path.fromUri(request.url);
479-
if (!fullPath.startsWith('canvaskit/')) {
474+
final List<String> pathSegments = request.url.pathSegments;
475+
final String canvasKitPath = _canvasKitPath;
476+
final String filePath = _fileSystem.path.joinAll(<String>[
477+
canvasKitPath,
478+
...pathSegments.skip(1),
479+
]);
480+
if (pathSegments.firstOrNull != 'canvaskit' ||
481+
!_fileSystem.path.isWithin(canvasKitPath, filePath) ||
482+
!_fileSystem.isFileSync(filePath)) {
480483
return shelf.Response.notFound('Not a CanvasKit file request');
481484
}
482485

483-
final String relativePath = fullPath.replaceFirst('canvaskit/', '');
484-
final String extension = _fileSystem.path.extension(relativePath);
486+
final String extension = _fileSystem.path.extension(filePath);
485487
String contentType;
486488
switch (extension) {
487489
case '.js':
@@ -494,7 +496,7 @@ window.\$dartLoader.loader.nextAttempt();
494496
return shelf.Response.internalServerError(body: error);
495497
}
496498

497-
final File canvasKitFile = _canvasKitFile(relativePath);
499+
final File canvasKitFile = _fileSystem.file(filePath);
498500
return shelf.Response.ok(
499501
canvasKitFile.openRead(),
500502
headers: <String, Object>{
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:file/file.dart';
6+
import 'package:file/memory.dart';
7+
import 'package:flutter_tools/src/artifacts.dart';
8+
import 'package:flutter_tools/src/base/io.dart';
9+
import 'package:flutter_tools/src/base/logger.dart';
10+
import 'package:flutter_tools/src/base/platform.dart';
11+
import 'package:flutter_tools/src/build_info.dart';
12+
import 'package:flutter_tools/src/project.dart';
13+
import 'package:flutter_tools/src/test/flutter_web_platform.dart';
14+
import 'package:flutter_tools/src/web/chrome.dart';
15+
import 'package:flutter_tools/src/web/compile.dart';
16+
import 'package:flutter_tools/src/web/memory_fs.dart';
17+
import 'package:shelf/shelf.dart' as shelf;
18+
19+
import '../../src/common.dart';
20+
import '../../src/context.dart';
21+
import '../../src/fakes.dart';
22+
23+
void main() {
24+
for (final style in <FileSystemStyle>[FileSystemStyle.posix, FileSystemStyle.windows]) {
25+
final styleName = style == FileSystemStyle.windows ? 'Windows' : 'POSIX';
26+
testUsingContext('serves local CanvasKit assets with $styleName paths', () async {
27+
final FileSystem fileSystem = MemoryFileSystem.test(style: style);
28+
final logger = BufferLogger.test();
29+
final platform = FakePlatform(
30+
operatingSystem: style == FileSystemStyle.windows ? 'windows' : 'linux',
31+
);
32+
final processManager = FakeProcessManager.empty();
33+
final artifacts = Artifacts.test(fileSystem: fileSystem);
34+
final Directory projectDirectory = fileSystem.systemTempDirectory.createTempSync(
35+
'canvaskit_test.',
36+
);
37+
final server = _FakeServer();
38+
final chromiumLauncher = ChromiumLauncher(
39+
fileSystem: fileSystem,
40+
platform: platform,
41+
processManager: processManager,
42+
operatingSystemUtils: FakeOperatingSystemUtils(),
43+
browserFinder: (Platform platform, FileSystem fileSystem) => 'chrome',
44+
logger: logger,
45+
);
46+
final FlutterWebPlatform webPlatform = await FlutterWebPlatform.start(
47+
projectDirectory.path,
48+
buildDirectory: projectDirectory.childDirectory('build'),
49+
buildInfo: const BuildInfo(
50+
BuildMode.debug,
51+
'',
52+
packageConfigPath: '.dart_tool/package_config.json',
53+
treeShakeIcons: false,
54+
),
55+
chromiumLauncher: chromiumLauncher,
56+
crossOriginIsolation: false,
57+
flutterProject: FlutterProject.fromDirectoryTest(projectDirectory),
58+
flutterTesterBinPath: artifacts.getArtifactPath(Artifact.flutterTester),
59+
toolContext: FakeToolContext(
60+
artifacts: artifacts,
61+
fs: fileSystem,
62+
logger: logger,
63+
processManager: processManager,
64+
),
65+
useWasm: false,
66+
webMemoryFS: WebMemoryFS(),
67+
webRenderer: WebRendererMode.canvaskit,
68+
serverFactory: () async => server,
69+
testPackageUri: Uri.parse('test'),
70+
);
71+
try {
72+
for (final relativePath in <String>[
73+
'canvaskit.js',
74+
'canvaskit.wasm',
75+
'chromium/canvaskit.js',
76+
'chromium/canvaskit.wasm',
77+
]) {
78+
final contents = 'contents of $relativePath';
79+
final File asset =
80+
fileSystem.file(
81+
fileSystem.path.joinAll(<String>[
82+
artifacts.getHostArtifact(HostArtifact.flutterWebSdk).path,
83+
'canvaskit',
84+
...relativePath.split('/'),
85+
]),
86+
)
87+
..createSync(recursive: true)
88+
..writeAsStringSync(contents);
89+
90+
final shelf.Response response = await server.handler!(
91+
shelf.Request('GET', Uri.parse('http://localhost/canvaskit/$relativePath')),
92+
);
93+
expect(response.statusCode, HttpStatus.ok);
94+
expect(
95+
response.headers[HttpHeaders.contentTypeHeader],
96+
relativePath.endsWith('.js') ? 'text/javascript' : 'application/wasm',
97+
);
98+
expect(response.headers[HttpHeaders.contentLengthHeader], asset.lengthSync().toString());
99+
expect(response.headers[HttpHeaders.cacheControlHeader], 'public, max-age=3600');
100+
expect(await response.readAsString(), contents);
101+
}
102+
103+
fileSystem
104+
.directory(artifacts.getHostArtifact(HostArtifact.flutterWebSdk).path)
105+
.childFile('canvaskit.js')
106+
.writeAsStringSync('outside the CanvasKit directory');
107+
final File rootAsset = fileSystem.file(
108+
fileSystem.path.join(fileSystem.path.rootPrefix(projectDirectory.path), 'canvaskit.js'),
109+
)..writeAsStringSync('outside the web SDK directory');
110+
final encodedSeparators = style == FileSystemStyle.windows
111+
? <String>['%5c', '%2f']
112+
: <String>['%2f', '%5c'];
113+
114+
for (final path in <String>[
115+
'canvaskit',
116+
'canvaskit/',
117+
'canvaskit/chromium',
118+
'canvaskit/missing.js',
119+
'canvaskit-other/canvaskit.js',
120+
'canvaskit/../canvaskit.js',
121+
'canvaskit/%2e%2e/canvaskit.js',
122+
for (final encodedSeparator in encodedSeparators) ...<String>[
123+
'canvaskit/%2e%2e${encodedSeparator}canvaskit.js',
124+
'canvaskit/chromium/%2e%2e$encodedSeparator%2e%2e${encodedSeparator}canvaskit.js',
125+
],
126+
'canvaskit/${Uri.encodeComponent(rootAsset.path)}',
127+
]) {
128+
final shelf.Response response = await server.handler!(
129+
shelf.Request('GET', Uri.parse('http://localhost/$path')),
130+
);
131+
expect(response.statusCode, HttpStatus.notFound, reason: path);
132+
}
133+
} finally {
134+
await webPlatform.close();
135+
}
136+
});
137+
}
138+
}
139+
140+
class _FakeServer implements shelf.Server {
141+
shelf.Handler? handler;
142+
143+
@override
144+
Future<void> close() async {}
145+
146+
@override
147+
void mount(shelf.Handler handler) {
148+
this.handler = handler;
149+
}
150+
151+
@override
152+
Uri get url => Uri.parse('http://localhost');
153+
}

0 commit comments

Comments
 (0)