From 28f5b9845dfb7c9f56e731c4e0d0593d26561fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tina=20M=C3=BCller?= Date: Wed, 11 Sep 2024 13:29:55 +0200 Subject: [PATCH] Fix linkify to only link to proper URLs This doesn't check for really well formed URLs, but it limits the matching to only allowed characters in URIs. It makes up for escapeForHtml() being already called, so it matches `&` separately. Tested with: # '"> should be excluded 'https://github.com/os-autoinst/openQA' "https://github.com/os-autoinst/openQA.git" # ) should be included https://de.wikipedia.org/wiki/Queen_(Band) # query strings should be included https://github.com/os-autoinst/openQA?foo=1%20&bar=*2 --- assets/javascripts/anser-import.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/anser-import.js b/assets/javascripts/anser-import.js index bfd18012b9c..5132be6c68b 100644 --- a/assets/javascripts/anser-import.js +++ b/assets/javascripts/anser-import.js @@ -1,7 +1,14 @@ const module = {}; +// https://datatracker.ietf.org/doc/html/rfc3986#appendix-A +function linkify(txt) { + const re = /(https?:\/\/(?:[A-Za-z0-9#;/?:@=+$,_.!~*()[\]-]|&|%[A-Ea-a0-9]{2})+)/gm; + return txt.replace(re, function (str) { + return '' + str + ''; + }); +} function ansiToHtml(data) { - return Anser.linkify(Anser.ansiToHtml(Anser.escapeForHtml(data), {use_classes: true})); + return linkify(Anser.ansiToHtml(Anser.escapeForHtml(data), {use_classes: true})); } function ansiToText(data) { return Anser.ansiToText(data);