Skip to content

Commit 637b7ac

Browse files
authored
validation accept subdomains level up to 5, url whitelist validation is no longer required.
* fixes #88 and #89 * Validator should accept url with multiple subdomain level. * Whitelist validation on code hosting URL is no longer required. * Adding comment to validator function Co-Authored-By: libremente <surf@libremente.eu>
1 parent 7610956 commit 637b7ac

3 files changed

Lines changed: 5 additions & 71 deletions

File tree

src/validation-error-message.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {VALIDATION_OK, VALIDATION_NOT_WHITELIST,
1+
const {VALIDATION_OK,
22
VALIDATION_INVALID_URL, VALIDATION_PHONE} = require('./validator-result.js');
33

44

@@ -22,9 +22,6 @@ function getErrorMessage(validatorResult) {
2222
case VALIDATION_INVALID_URL:
2323
message = 'Indirizzo URL invalido: ricompila il form';
2424
break;
25-
case VALIDATION_NOT_WHITELIST:
26-
message = 'Indirizzo URL non presente nella whitelist: ricompila il form';
27-
break;
2825
default:
2926
throw new Error('Url non valido');
3027
}

src/validator-result.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const VALIDATION_OK = 0;
22
const VALIDATION_INVALID_URL = 1;
3-
const VALIDATION_NOT_WHITELIST = 2;
43
const VALIDATION_PHONE = 3;
54

65
module.exports = {VALIDATION_OK, VALIDATION_INVALID_URL,
7-
VALIDATION_NOT_WHITELIST, VALIDATION_PHONE };
6+
VALIDATION_PHONE };

src/validator.js

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
'use strict';
2-
const { URL } = require('url');
3-
const parseDomain = require('parse-domain');
4-
const { VALIDATION_OK, VALIDATION_NOT_WHITELIST,
2+
const { VALIDATION_OK,
53
VALIDATION_INVALID_URL, VALIDATION_PHONE } = require('./validator-result.js');
64

75
/**
8-
* Controlla se l'URL e' ben formata e se il nome del dominio rientra in una whitelist di url.
6+
* Controlla se l'URL e' ben formata. Sono supportati fino a 5 livelli di dominio.
97
*
108
* @param url rappresenta l'url da controllare
119
*
1210
* @return Un oggetto di tipo ValidatorResult
1311
*/
1412
function validateUrl(url) {
15-
const generalRegex = /^(https?):\/\/(www\.)?([a-z]+)(\.([\da-zA-Z-]+)){1,2}(\/[\da-zA-Z-_]+)*\/?$/;
13+
const generalRegex = /^(https?):\/\/(www\.)?([a-z]+)(\.([\da-zA-Z-]+)){1,5}(\/[\da-zA-Z-_]+)*\/?$/;
1614
if (!generalRegex.test(url)) {
1715
return VALIDATION_INVALID_URL;
1816
}
19-
if (!isInWhiteList(url)) {
20-
return VALIDATION_NOT_WHITELIST;
21-
}
2217
return VALIDATION_OK;
2318
}
2419

@@ -36,63 +31,6 @@ function validatePhoneNumber(phone) {
3631
return VALIDATION_OK;
3732
}
3833

39-
/**
40-
* Controlla se il dominio della URL passata rientra in una whitelist di domini
41-
*
42-
* @param url rappresenta l'url da controllare
43-
*
44-
* @return true se l'url e' presente nella whitelist, false altrimenti
45-
*/
46-
function isInWhiteList(url) {
47-
let result = false;
48-
const dictionary = {
49-
'github': /^(https?):\/\/(github\.com)\/([\da-zA-Z-_])*\/?$/,
50-
'bitbucket': /^(https?):\/\/(bitbucket\.org)\/([\da-zA-Z-_])*\/?$/,
51-
'gitlab': /^(https?):\/\/(gitlab\.com)\/([\da-zA-Z-_]+)\/?$/,
52-
'phabricator': /^(https?):\/\/(secure\.phabricator\.com)\/(p)\/([\da-zA-Z-_]+)\/?$/,
53-
'gitea': /^(https?):\/\/(try\.gitea\.io)\/([\da-zA-Z-_]+)\/?$/,
54-
'gogs': /^(https?):\/\/(try\.gogs\.io)\/([\da-zA-Z-_]+)\/?$/
55-
};
56-
57-
const arrayUrl = [
58-
'https://github.com/',
59-
'https://bitbucket.org/',
60-
'https://gitlab.com/',
61-
'https://phabricator.com/',
62-
'https://gitea.io/',
63-
'https://gogs.io/'
64-
];
65-
66-
const urlParsed = new URL(url);
67-
const protocol = urlParsed.protocol;
68-
const hostname = urlParsed.hostname;
69-
70-
const domainParsed = parseDomain(url);
71-
if (domainParsed != null) {
72-
const domain = domainParsed.domain;
73-
74-
const regex = dictionary[domain];
75-
const isValid = regex && regex.test(url);
76-
77-
let baseUrl = '';
78-
79-
/* Gli url nella forma https://try.gogs.io/<username>/<projectname>,
80-
https://try.gitea.io/<username>/<projectname>,
81-
https://secure.phabricator.com/project/view/395/
82-
devono diventare https://gogs.io/<username>/<projectname>,
83-
https://gitea.io/<username>/<projectname>,
84-
https://phabricator.com/project/view/<numProject>/
85-
*/
86-
if (['gitea', 'gogs', 'phabricator'].includes(domain)) {
87-
baseUrl = `${protocol}//${domain}.${domainParsed.tld}/`;
88-
} else {
89-
baseUrl = `${protocol}//${hostname}/`;
90-
}
91-
92-
result = isValid && arrayUrl.includes(baseUrl);
93-
}
94-
return result;
95-
}
9634

9735
module.exports = {
9836
url: validateUrl,

0 commit comments

Comments
 (0)