Skip to content

Commit 9a7ce90

Browse files
AtkinsSJkalenikaliaksandr
authored andcommitted
LibURL: Gracefully handle a host having no public suffix
Specifically, after implementing some recent spec changes to navigables, we end up calling `get_public_suffix("localhost")` here, which returns OptionalNone. This would previously crash. Our get_public_suffix() seems a little incorrect. From the spec: > If no rules match, the prevailing rule is "*". > https://github.com/publicsuffix/list/wiki/Format#algorithm However, ours returns an empty Optional in that case. To avoid breaking other users of it, this patch modifies Host's uses of it, rather than the function itself.
1 parent c073127 commit 9a7ce90

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Libraries/LibURL/Host.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,13 @@ Optional<String> Host::public_suffix() const
191191
auto trailing_dot = host_string.ends_with('.') ? "."sv : ""sv;
192192

193193
// 3. Let publicSuffix be the public suffix determined by running the Public Suffix List algorithm with host as domain. [PSL]
194-
auto public_suffix = get_public_suffix(host_string.bytes_as_string_view());
195-
196-
// NOTE: get_public_suffix() returns Optional, but this algorithm assumes a value. Is that OK?
197-
VERIFY(public_suffix.has_value());
194+
// NOTE: The spec algorithm for the public suffix returns "*" by default, but get_public_suffix() returns an empty Optional.
195+
// Remove the `value_or()` if and when we update it.
196+
auto public_suffix = get_public_suffix(host_string.bytes_as_string_view()).value_or("*"_string);
198197

199198
// 4. Assert: publicSuffix is an ASCII string that does not end with ".".
200-
VERIFY(all_of(public_suffix->code_points(), is_ascii));
201-
VERIFY(!public_suffix->ends_with('.'));
199+
VERIFY(all_of(public_suffix.code_points(), is_ascii));
200+
VERIFY(!public_suffix.ends_with('.'));
202201

203202
// 5. Return publicSuffix and trailingDot concatenated.
204203
return MUST(String::formatted("{}{}", public_suffix, trailing_dot));
@@ -219,14 +218,13 @@ Optional<String> Host::registrable_domain() const
219218
auto trailing_dot = host_string.ends_with('.') ? "."sv : ""sv;
220219

221220
// 3. Let registrableDomain be the registrable domain determined by running the Public Suffix List algorithm with host as domain. [PSL]
222-
auto registrable_domain = get_public_suffix(host_string);
223-
224-
// NOTE: get_public_suffix() returns Optional, but this algorithm assumes a value. Is that OK?
225-
VERIFY(registrable_domain.has_value());
221+
// NOTE: The spec algorithm for the public suffix returns "*" by default, but get_public_suffix() returns an empty Optional.
222+
// Remove the `value_or()` if and when we update it.
223+
auto registrable_domain = get_public_suffix(host_string).value_or("*"_string);
226224

227225
// 4. Assert: registrableDomain is an ASCII string that does not end with ".".
228-
VERIFY(all_of(registrable_domain->code_points(), is_ascii));
229-
VERIFY(!registrable_domain->ends_with('.'));
226+
VERIFY(all_of(registrable_domain.code_points(), is_ascii));
227+
VERIFY(!registrable_domain.ends_with('.'));
230228

231229
// 5. Return registrableDomain and trailingDot concatenated.
232230
return MUST(String::formatted("{}{}", registrable_domain, trailing_dot));

0 commit comments

Comments
 (0)