filtering protocols in the url field

This commit is contained in:
antelle 2020-04-17 20:45:41 +02:00
parent 38c3a46395
commit 2f214b25c7
No known key found for this signature in database
GPG Key ID: 094A2F2D6136A4EE
1 changed files with 21 additions and 8 deletions

View File

@ -1,22 +1,35 @@
import { FieldViewText } from 'views/fields/field-view-text';
import { escape } from 'util/fn';
const AllowedProtocols = ['http:', 'https:', 'ftp:', 'ftps:', 'mailto:'];
class FieldViewUrl extends FieldViewText {
displayUrlRegex = /^https:\/\//i;
cssClass = 'url';
renderValue(value) {
return value
? '<a href="' +
escape(this.fixUrl(value)) +
'" rel="noreferrer noopener" target="_blank">' +
escape(this.displayUrl(value)) +
'</a>'
: '';
try {
return value
? '<a href="' +
escape(this.fixUrl(value)) +
'" rel="noreferrer noopener" target="_blank">' +
escape(this.displayUrl(value)) +
'</a>'
: '';
} catch (e) {
return escape(value);
}
}
fixUrl(url) {
return url.indexOf(':') < 0 ? 'https://' + url : url;
const proto = new URL(url, 'dummy://').protocol;
if (proto === 'dummy:') {
return 'https://' + url;
}
if (!AllowedProtocols.includes(proto)) {
throw new Error('Bad url');
}
return url;
}
displayUrl(url) {