Skip to content

Commit 2f01b98

Browse files
committed
[webview_flutter_wkwebview] Follow RFC 6265 guideline for getCookies domain matching
1 parent 09a5802 commit 2f01b98

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_cookie_manager.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ class WebKitWebViewCookieManager extends PlatformWebViewCookieManager {
9292
}
9393

9494
final domain = props[HttpCookiePropertyKey.domain].toString();
95-
if (!domain.contains(url.host)) {
95+
96+
// Follow RFC 6265 guideline for domain matching
97+
var cookieDomain = domain;
98+
if (domain.startsWith('.')) {
99+
cookieDomain = cookieDomain.substring(1);
100+
}
101+
102+
if (url.host != cookieDomain && !url.host.endsWith('.$cookieDomain')) {
96103
return null;
97104
}
98105

packages/webview_flutter/webview_flutter_wkwebview/test/webkit_webview_cookie_manager_test.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ void main() {
115115
// Mock cookies returned by the cookie store
116116
final mockCookie1 = MockHTTPCookie();
117117
final mockCookie2 = MockHTTPCookie();
118+
final mockCookie3 = MockHTTPCookie();
118119

119120
when(mockCookie1.getProperties()).thenAnswer(
120121
(_) async => <HttpCookiePropertyKey, Object>{
@@ -134,9 +135,18 @@ void main() {
134135
},
135136
);
136137

138+
when(mockCookie3.getProperties()).thenAnswer(
139+
(_) async => <HttpCookiePropertyKey, Object>{
140+
HttpCookiePropertyKey.name: 'cookie3',
141+
HttpCookiePropertyKey.value: 'value3',
142+
HttpCookiePropertyKey.domain: '.flutter.dev',
143+
HttpCookiePropertyKey.path: '/path',
144+
},
145+
);
146+
137147
when(
138148
mockCookieStore.getAllCookies(),
139-
).thenAnswer((_) async => [mockCookie1, mockCookie2]);
149+
).thenAnswer((_) async => [mockCookie1, mockCookie2, mockCookie3]);
140150

141151
PigeonOverrides.wKWebsiteDataStore_defaultDataStore =
142152
mockWKWebsiteDataStore;
@@ -149,7 +159,7 @@ void main() {
149159
Uri.parse('https://flutter.dev'),
150160
);
151161

152-
expect(cookies.length, 2);
162+
expect(cookies.length, 3);
153163

154164
expect(cookies[0].name, 'cookie1');
155165
expect(cookies[0].value, 'value1');
@@ -160,6 +170,17 @@ void main() {
160170
expect(cookies[1].value, 'value2');
161171
expect(cookies[1].domain, 'flutter.dev');
162172
expect(cookies[1].path, '/path');
173+
174+
final List<WebViewCookie> cookiesWww = await manager.getCookies(
175+
Uri.parse('https://www.flutter.dev'),
176+
);
177+
178+
expect(cookiesWww.length, 3);
179+
180+
expect(cookiesWww[2].name, 'cookie3');
181+
expect(cookiesWww[2].value, 'value3');
182+
expect(cookiesWww[2].domain, '.flutter.dev');
183+
expect(cookiesWww[2].path, '/path');
163184
});
164185
});
165186
}

0 commit comments

Comments
 (0)