Add checks for mixed numerics, sequences of non-spacing marks, and initial blacklist

This commit is contained in:
Matthew Lilley 2018-05-10 10:22:45 +01:00
parent 990631d91d
commit bc0b2241de
5 changed files with 79 additions and 6 deletions

View File

@ -48,9 +48,15 @@ describe('test', () => {
expect(validate('\u0369')).toEqual(true);
});
it('should return false when sequence of combining marks are present', () => {
expect(validate('\u0369\u0369\u0369')).toEqual(false);
expect(validate('\u0369\u0369')).toEqual(false);
});
it('shoud return false', () => {
expect(validate('googlé.com')).toEqual(false);
it('should return false when sequence of kana combining marks are present', () => {
expect(validate('\u3099\u3099')).toEqual(false);
});
it('should return true when a single combining mark is present', () => {
expect(validate('\u0369')).toEqual(true);
});
it('should return true when a single kana combining mark is present', () => {
expect(validate('\u3099')).toEqual(true);
});
});

View File

@ -9,6 +9,8 @@ import {
kanaCharacterException,
latinGreekCyrillicAscii,
nonAsciiLatin,
nonSpacingMark,
numeric,
pureAscii,
removed,
scripts,
@ -16,6 +18,8 @@ import {
import { cyrillic } from './regexes/scripts';
// const blacklist = require('./domains.json');
class UrlFormatter {
public readonly url: string;
public readonly isTldAscii: boolean;
@ -30,7 +34,31 @@ class UrlFormatter {
}
}
export function detectScripts(validationString: string): object {
function detectScript(character: string): string {
let s = '';
for (const [scriptName, script] of Object.entries(scripts)) {
if (script.test(character)) {
s = scriptName;
break;
}
}
return s;
}
function mixedNumerics(validationString: string) {
const result: string[] = [];
for (const character of validationString) {
if (numeric.test(character)) {
const script: string = detectScript(character);
if (!result.includes(script)) {
result.push(script);
}
}
}
return result.length > 1;
}
function detectScripts(validationString: string): object {
return Object.entries(scripts)
.filter(script => {
return script[1].test(validationString);
@ -93,6 +121,22 @@ function singleScript(detectedScripts: object): boolean {
);
}
// //Only do Levenshtein if it's not blacklisted
// //Levenshtein - @sogoiii
// var blHolisticStatus = false;
// if(isBlacklisted === false && arrWhitelistedDomains.indexOf(strCurrentTab) < 0) {
// var strCurrentTab = punycode.toUnicode(strCurrentTab);
// var source = strCurrentTab.replace(/\./g, '');
// var intHolisticMetric = levenshtein(source, 'myetherwallet');
// var intHolisticLimit = 5; // How different can the word be?
// blHolisticStatus = (intHolisticMetric > 0 && intHolisticMetric < intHolisticLimit) ? true : false;
// if(blHolisticStatus === false) {
// //Do edit distance against mycrypto
// var intHolisticMetric = levenshtein(source, 'mycrypto');
// blHolisticStatus = (intHolisticMetric > 0 && intHolisticMetric < 3) ? true : false;
// }
// }
// function lookupMatchInTopDomains(labels: string[]): boolean {
// return labels.every(l => {
// if(found) return true
@ -107,9 +151,27 @@ function inAllowedSets(validationString: string) {
);
}
function sequenceOfCombiningMarks(validationString: string) {
const characters: string[] = Array.from(validationString);
for (let i = 0; i < characters.length; i++) {
if (
nonSpacingMark.test(characters[i]) &&
nonSpacingMark.test(characters[i + 1])
) {
return true;
}
continue;
}
}
export function validate(validationString: string): boolean {
try {
if (!inAllowedSets(validationString)) {
if (
// blacklist.indexOf(validationString) > -1 ||
!inAllowedSets(validationString) ||
mixedNumerics(validationString) ||
sequenceOfCombiningMarks(validationString)
) {
return false;
}
// scripts

View File

@ -36,6 +36,7 @@ export const dangerous = [
// for(let i = 0; i < dangerous.length; i++) console.log('dangerous' + i, new RegExp(dangerous[i]).test('abc'))
export const nonSpacingMark = X('\\p{Non_Spacing_Mark}');
export const numeric = X('\\p{Number}');
export const decimalDigitNumber = X('\\p{Nd}');
export const allowed = X(

View File

@ -0,0 +1,4 @@
declare module '*.json' {
const value: string;
export default value;
}

View File

@ -21,7 +21,7 @@
"no-reference": true,
"unified-signatures": true,
"await-promise": true,
"cyclomatic-complexity": [true, 11],
"cyclomatic-complexity": [true, 15],
"prefer-const": true,
"no-boolean-literal-compare": true,
"no-unnecessary-callback-wrapper": true,