|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+import punycode from 'punycode';
|
|
|
2
|
+
|
|
1
|
3
|
/**
|
|
2
|
4
|
* Returns the field value in a platform generic way.
|
|
3
|
5
|
*
|
|
|
@@ -7,3 +9,41 @@
|
|
7
|
9
|
export function getFieldValue(fieldParameter: { target: { value: string; }; } | string) {
|
|
8
|
10
|
return typeof fieldParameter === 'string' ? fieldParameter : fieldParameter?.target?.value;
|
|
9
|
11
|
}
|
|
|
12
|
+
|
|
|
13
|
+/**
|
|
|
14
|
+ * Formats the URL text for react-linkify.
|
|
|
15
|
+ *
|
|
|
16
|
+ * @param {string} text - The URL text.
|
|
|
17
|
+ * @returns {string} - The formatted text.
|
|
|
18
|
+ */
|
|
|
19
|
+export function formatURLText(text = '') {
|
|
|
20
|
+ let result;
|
|
|
21
|
+
|
|
|
22
|
+ // In order to prevent homograph attacks we need to use punycode. Reference
|
|
|
23
|
+ // https://github.com/tasti/react-linkify/issues/84. In the same time it seems PunycodeJS will treat the URL
|
|
|
24
|
+ // as an email when there is '@' and will erase parts of it. This is problematic if there is a URL like
|
|
|
25
|
+ // https://example.com/@test@@@123/test@test, punycode will truncate this to https://example.com/@test which
|
|
|
26
|
+ // is security issue because parts of the URL are actually missing from the text that we display. That's why
|
|
|
27
|
+ // we use punycode on valid URLs(that don't have '@' as part of the host) only for the host part of the URL.
|
|
|
28
|
+ try {
|
|
|
29
|
+ const url = new URL(text);
|
|
|
30
|
+ const { host } = url;
|
|
|
31
|
+
|
|
|
32
|
+ if (host) {
|
|
|
33
|
+ url.host = punycode.toASCII(host);
|
|
|
34
|
+ result = url.toString();
|
|
|
35
|
+ }
|
|
|
36
|
+ } catch (e) {
|
|
|
37
|
+ // Not a valid URL
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ if (!result) {
|
|
|
41
|
+ // This will be the case for invalid URLs or URLs without a host (emails for example). In this case beacuse
|
|
|
42
|
+ // of the issue with PunycodeJS that truncates parts of the text when there is '@' we split the text by '@'
|
|
|
43
|
+ // and use punycode for every separate part to prevent homograph attacks.
|
|
|
44
|
+ result = text.split('@').map(punycode.toASCII)
|
|
|
45
|
+ .join('@');
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ return result;
|
|
|
49
|
+}
|