Skip to content

Commit 6b9f04b

Browse files
apinilabs-pascalmdebbarflutter-zl
authored andcommitted
[web] Stop percent-decoding URLs written to browser history (flutter#189835)
When the framework reports new route information via the `routeInformationUpdated` navigation message, the web engine rebuilds the URL and runs `Uri.decodeComponent` over the whole string before handing it to the browser history (introduced in flutter-team-archive/engine#40250). This strips one level of percent-encoding from the address bar on every report: | Framework reports | Address bar before | Address bar after this PR | |---|---|---| | `/item/foo%2Fbar` | `/item/foo/bar` — refresh/share resolves to a different route | `/item/foo%2Fbar` | | `?q=x%26y` | `?q=x&y` — value splits into two query parameters | `?q=x%26y` | | `?color=%23ffffff` | `?color=#ffffff` — rest of the URL becomes a fragment | `?color=%23ffffff` | Because routers decode path parameters exactly once, this also made `GoRouterState.pathParameters` differ between in-app navigation and a subsequent browser refresh of the same URL: each round trip through the engine silently dropped one encoding level. ## Fix `handleNavigationMessage` keeps the scheme/authority stripping, but now reassembles the URL from the raw (still encoded) `path`, `query`, and `fragment` components instead of decoding the result. The browser's address bar now reflects exactly what the framework reported. This supersedes flutter#176029, which was closed for inactivity (missing tests). Unlike that attempt, this PR also avoids reconstructing the query via `Uri(queryParameters:)`, which re-encoded values differently than reported (e.g. `%20` in a query value came back as `+`), and it adds a regression test. ## Tests Adds a regression test to `lib/web_ui/test/engine/routing_test.dart` asserting that `%20`, `%2F`, and `%26` in path, query, and fragment survive the round trip into browser history unchanged. The existing `routeInformationUpdated can handle uri` test (scheme/authority stripping) is unaffected and still passes. Fixes flutter#180373 Fixes flutter#171757 Fixes flutter#147857 Fixes flutter#155992 cc @mdebbar (you triaged flutter#176029 and invited a re-open — this picks that up with tests) ## 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. 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 [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]> Co-authored-by: zhongliugo <[email protected]>
1 parent e770220 commit 6b9f04b

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/window.dart‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,14 +715,17 @@ final class EngineFlutterWindow extends EngineFlutterView implements ui.Singleto
715715
final String path;
716716
if (uriString != null) {
717717
final Uri uri = Uri.parse(uriString);
718-
// Need to remove scheme and authority.
719-
path = Uri.decodeComponent(
720-
Uri(
721-
path: uri.path.isEmpty ? '/' : uri.path,
722-
queryParameters: uri.queryParametersAll.isEmpty ? null : uri.queryParametersAll,
723-
fragment: uri.fragment.isEmpty ? null : uri.fragment,
724-
).toString(),
725-
);
718+
// Need to remove scheme and authority while preserving the
719+
// percent-encoding of the path, query, and fragment exactly as
720+
// reported by the framework. Decoding the URL here would make the
721+
// browser's address bar diverge from the route the app is on
722+
// (e.g. `/a%2Fb` would turn into `/a/b`), so a refresh or a
723+
// shared link would no longer round-trip to the same route.
724+
// See https://github.com/flutter/flutter/issues/180373.
725+
path =
726+
'${uri.path.isEmpty ? '/' : uri.path}'
727+
'${uri.hasQuery ? '?${uri.query}' : ''}'
728+
'${uri.hasFragment ? '#${uri.fragment}' : ''}';
726729
} else {
727730
path = arguments.tryString('location')!;
728731
}

‎engine/src/flutter/lib/web_ui/test/engine/routing_test.dart‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,34 @@ void testMain() {
323323
expect(myWindow.browserHistory.urlStrategy!.getPath(), '/baz?abc=def#fragment');
324324
});
325325

326+
test('routeInformationUpdated preserves percent-encoding in uri', () async {
327+
// Regression test for https://github.com/flutter/flutter/issues/180373.
328+
await myWindow.debugInitializeHistory(
329+
TestUrlStrategy.fromEntry(const TestHistoryEntry('initial state', null, '/initial')),
330+
useSingle: false,
331+
);
332+
expect(myWindow.browserHistory, isA<MultiEntriesBrowserHistory>());
333+
334+
final callback = Completer<void>();
335+
myWindow.sendPlatformMessage(
336+
'flutter/navigation',
337+
const JSONMethodCodec().encodeMethodCall(
338+
const MethodCall('routeInformationUpdated', <String, dynamic>{
339+
'uri': 'http://myhostname.com/item/foo%20bar%2Fbaz?abc=d%26ef#frag%20ment',
340+
}),
341+
),
342+
(_) {
343+
callback.complete();
344+
},
345+
);
346+
await callback.future;
347+
expect(myWindow.browserHistory, isA<MultiEntriesBrowserHistory>());
348+
expect(
349+
myWindow.browserHistory.urlStrategy!.getPath(),
350+
'/item/foo%20bar%2Fbaz?abc=d%26ef#frag%20ment',
351+
);
352+
});
353+
326354
test('can replace in MultiEntriesBrowserHistory', () async {
327355
await myWindow.debugInitializeHistory(
328356
TestUrlStrategy.fromEntry(const TestHistoryEntry('initial state', null, '/initial')),

0 commit comments

Comments
 (0)