diff --git a/core/lib/presentation/utils/html_transformer/dom/sanitize_hyper_link_tag_in_html_transformers.dart b/core/lib/presentation/utils/html_transformer/dom/sanitize_hyper_link_tag_in_html_transformers.dart
index 8c1a13737..8d2fc5d2a 100644
--- a/core/lib/presentation/utils/html_transformer/dom/sanitize_hyper_link_tag_in_html_transformers.dart
+++ b/core/lib/presentation/utils/html_transformer/dom/sanitize_hyper_link_tag_in_html_transformers.dart
@@ -2,6 +2,7 @@ import 'package:core/data/network/dio_client.dart';
import 'package:core/presentation/utils/html_transformer/base/dom_transformer.dart';
import 'package:core/presentation/utils/html_transformer/sanitize_url.dart';
import 'package:core/utils/app_logger.dart';
+import 'package:core/utils/external_link_policy.dart';
import 'package:html/dom.dart';
class SanitizeHyperLinkTagInHtmlTransformer extends DomTransformer {
@@ -49,29 +50,48 @@ class SanitizeHyperLinkTagInHtmlTransformer extends DomTransformer {
// SanitizeUrl returns its input when decoding fails, so that is a
// rejection too.
element.attributes.remove('href');
- } else if (urlSanitized.isNotEmpty) {
- element.attributes['href'] = urlSanitized;
+ return;
+ }
+
+ final keptUrl = urlSanitized.isNotEmpty ? urlSanitized : url;
+ if (ExternalLinkPolicy.canKeepInContent(_trimBrowserWhitespace(keptUrl))) {
+ element.attributes['href'] = keptUrl;
+ } else {
+ element.attributes.remove('href');
}
}
/// Only scheme-less hrefs that unambiguously name a host get `https`.
/// A "looks like a domain" check would turn `setup.zip` into a real host.
String _withDefaultScheme(String url) {
- final trimmedUrl = url.trim();
+ final trimmedUrl = _trimBrowserWhitespace(url);
if (trimmedUrl.startsWith('//')) return 'https:$trimmedUrl';
if (trimmedUrl.toLowerCase().startsWith('www.')) return 'https://$trimmedUrl';
return url;
}
bool _isRelativeUrl(String url) {
- final trimmedUrl = url.trim();
- if (trimmedUrl.isEmpty || trimmedUrl.startsWith('#')) {
- return false;
- }
- final uri = Uri.tryParse(_tryDecode(trimmedUrl) ?? trimmedUrl);
+ final trimmedUrl = _trimBrowserWhitespace(url);
+ if (trimmedUrl.startsWith('#')) return false;
+ // The browser reads the raw value: `https%3A//x` and an empty href are relative to the app page.
+ final uri = Uri.tryParse(trimmedUrl);
return uri == null || !uri.hasScheme;
}
+ /// Strips only what the browser URL parser strips (C0 controls and space).
+ /// `String.trim` also drops U+00A0 and U+FEFF, which the browser keeps, making the href relative.
+ String _trimBrowserWhitespace(String url) {
+ var start = 0;
+ var end = url.length;
+ while (start < end && url.codeUnitAt(start) <= 0x20) {
+ start++;
+ }
+ while (end > start && url.codeUnitAt(end - 1) <= 0x20) {
+ end--;
+ }
+ return url.substring(start, end);
+ }
+
String? _tryDecode(String url) {
try {
return Uri.decodeFull(url);
diff --git a/core/lib/utils/external_link_policy.dart b/core/lib/utils/external_link_policy.dart
index 34790d333..e6f95eb5a 100644
--- a/core/lib/utils/external_link_policy.dart
+++ b/core/lib/utils/external_link_policy.dart
@@ -22,6 +22,10 @@ class ExternalLinkPolicy {
final uri = Uri.tryParse(href);
if (uri == null) return false;
if (!uri.hasScheme) return uri.host.isEmpty;
- return canLaunchFromContent(uri);
+ return canLaunchFromContent(uri) && !_isWebLinkWithoutHost(uri);
}
+
+ // `https:%2f/x` or `https://\@x` has no host for Dart but one for the browser, or a relative target.
+ static bool _isWebLinkWithoutHost(Uri uri) =>
+ (uri.isScheme('http') || uri.isScheme('https')) && uri.host.isEmpty;
}
diff --git a/core/test/utils/external_link_policy_test.dart b/core/test/utils/external_link_policy_test.dart
index 631ce94fa..05ad38a6a 100644
--- a/core/test/utils/external_link_policy_test.dart
+++ b/core/test/utils/external_link_policy_test.dart
@@ -35,6 +35,8 @@ void main() {
group('ExternalLinkPolicy.canKeepInContent', () {
for (final href in const [
'https://a.b',
+ 'http://localhost:8080/x',
+ r'http://intranet.example.com/share\doc.pdf',
'tel:+33123456789',
'geo:1,2',
'webcal://x/f.ics',
@@ -53,6 +55,10 @@ void main() {
'file:///etc/hosts',
'//evil.example',
'http://[::1',
+ 'https://',
+ r'https://\@[::1]',
+ 'https:%2f/[::1]',
+ 'javascript:alert(1)',
]) {
test('SHOULD drop $href', () {
expect(ExternalLinkPolicy.canKeepInContent(href), isFalse);
diff --git a/core/test/utils/html_transform_text_html_test.dart b/core/test/utils/html_transform_text_html_test.dart
index 968ea0907..0f5cb1c03 100644
--- a/core/test/utils/html_transform_text_html_test.dart
+++ b/core/test/utils/html_transform_text_html_test.dart
@@ -469,6 +469,70 @@ void main() {
expect(out, contains('padded'));
});
+ test('SHOULD drop href with a leading non-ASCII space WHEN only SanitizeHyperLinkTagInHtmlTransformer is used', () async {
+ const input = 'nbsp'
+ 'bom'
+ 'nbsp-bad-escape';
+ final out = await transformWith(input, [SanitizeHyperLinkTagInHtmlTransformer()]);
+ expect(out, allOf(
+ contains('nbsp'),
+ contains('bom'),
+ contains('nbsp-bad-escape'),
+ ));
+ });
+
+ test('SHOULD drop href that is relative before percent-decoding or empty WHEN only SanitizeHyperLinkTagInHtmlTransformer is used', () async {
+ const input = 'encoded-https'
+ 'encoded-javascript'
+ 'encoded-host'
+ 'empty'
+ 'blank';
+ final out = await transformWith(input, [SanitizeHyperLinkTagInHtmlTransformer()]);
+ expect(out, allOf(
+ contains('encoded-https'),
+ contains('encoded-javascript'),
+ contains('encoded-host'),
+ contains('empty'),
+ contains('blank'),
+ ));
+ });
+
+ test('SHOULD drop http(s) href without a host WHEN only SanitizeHyperLinkTagInHtmlTransformer is used', () async {
+ const input = 'no-host'
+ 'backslash-userinfo'
+ 'encoded-slash';
+ final out = await transformWith(input, [SanitizeHyperLinkTagInHtmlTransformer()]);
+ expect(out, allOf(
+ contains('no-host'),
+ contains('backslash-userinfo'),
+ contains('encoded-slash'),
+ ));
+ });
+
+ test('SHOULD drop href with a scheme the app may not open WHEN only SanitizeHyperLinkTagInHtmlTransformer is used', () async {
+ const input = 'js'
+ 'deep-link';
+ final out = await transformWith(input, [SanitizeHyperLinkTagInHtmlTransformer()]);
+ expect(out, allOf(
+ contains('js'),
+ contains('deep-link'),
+ ));
+ });
+
+ test('SHOULD keep tel, http host without TLD, encoded backslash and badly percent-encoded absolute hrefs WHEN only SanitizeHyperLinkTagInHtmlTransformer is used', () async {
+ const input = 'tel'
+ 'local'
+ 'encoded-backslash'
+ 'bad-escape-abs';
+ final out = await transformWith(input, [SanitizeHyperLinkTagInHtmlTransformer()]);
+ expect(out, allOf(
+ contains('tel'),
+ contains('local'),
+ contains('encoded-backslash'),
+ contains('bad-escape-abs'),
+ ));
+ });
+
test('SHOULD drop unparsable and badly percent-encoded href WHEN only SanitizeHyperLinkTagInHtmlTransformer is used', () async {
const input = 'unparsable'
'bad-escape';
diff --git a/core/test/utils/sanitize_html_test.dart b/core/test/utils/sanitize_html_test.dart
new file mode 100644
index 000000000..924c56f59
--- /dev/null
+++ b/core/test/utils/sanitize_html_test.dart
@@ -0,0 +1,20 @@
+import 'package:core/presentation/utils/html_transformer/sanitize_html.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ group('SanitizeHtml.process', () {
+ for (final href in ['https://', 'https:%2f/[::1]']) {
+ test('should drop the hostless href "$href" and keep the link text', () {
+ final output = SanitizeHtml().process(inputHtml: 'link');
+
+ expect(output, 'link');
+ });
+ }
+
+ test('should keep an absolute https href', () {
+ final output = SanitizeHtml().process(inputHtml: 'link');
+
+ expect(output, 'link');
+ });
+ });
+}