Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
TonPC64 authored Jul 27, 2022
2 parents 7fcb176 + 1bb14e8 commit 57a90cf
Show file tree
Hide file tree
Showing 26 changed files with 2,015 additions and 157 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '38 10 * * 4'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ yarn.lock
/index.js
validator.js
validator.min.js

39 changes: 26 additions & 13 deletions README.md

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Security Policy

## Supported Versions

In the case of a confirmed security issue, only the current version of validator is guaranteed to be patched.

## Reporting a Vulnerability

**Please don't disclose security-related issues publicly.**

If you discover a vulnerability within validator, please use [huntr.dev disclosure form](https://huntr.dev/bounties/disclose/?target=https://github.com/validatorjs/validator.js). We will try to validate and respond to reports in a reasonable time. if the issue is confirmed, we will create a security advisory and a patch as soon as possible.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import isBefore from './lib/isBefore';

import isIn from './lib/isIn';

import isLuhnValid from './lib/isLuhnValid';
import isCreditCard from './lib/isCreditCard';
import isIdentityCard from './lib/isIdentityCard';

Expand All @@ -86,6 +87,7 @@ import isCurrency from './lib/isCurrency';

import isBtcAddress from './lib/isBtcAddress';

import isISO6391 from './lib/isISO6391';
import isISO8601 from './lib/isISO8601';
import isRFC3339 from './lib/isRFC3339';
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
Expand Down Expand Up @@ -182,6 +184,7 @@ const validator = {
isAfter,
isBefore,
isIn,
isLuhnValid,
isCreditCard,
isIdentityCard,
isEAN,
Expand All @@ -195,6 +198,7 @@ const validator = {
isEthereumAddress,
isCurrency,
isBtcAddress,
isISO6391,
isISO8601,
isRFC3339,
isISO31661Alpha2,
Expand Down
25 changes: 17 additions & 8 deletions src/lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const alpha = {
ar: /^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
he: /^[א-ת]+$/,
fa: /^['آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی']+$/i,
bn: /^['ি']+$/,
'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i,
'si-LK': /^[\u0D80-\u0DFF]+$/,
};

export const alphanumeric = {
Expand Down Expand Up @@ -66,7 +68,9 @@ export const alphanumeric = {
ar: /^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
he: /^[0-9א-ת]+$/,
fa: /^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$/i,
bn: /^['ি']+$/,
'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i,
'si-LK': /^[0-9\u0D80-\u0DFF]+$/,
};

export const decimal = {
Expand All @@ -85,10 +89,8 @@ for (let locale, i = 0; i < englishLocales.length; i++) {
}

// Source: http://www.localeplanet.com/java/
export const arabicLocales = [
'AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY',
'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE',
];
export const arabicLocales = ['AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY',
'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE'];

for (let locale, i = 0; i < arabicLocales.length; i++) {
locale = `ar-${arabicLocales[i]}`;
Expand All @@ -97,22 +99,29 @@ for (let locale, i = 0; i < arabicLocales.length; i++) {
decimal[locale] = decimal.ar;
}

export const farsiLocales = [
'IR', 'AF',
];
export const farsiLocales = ['IR', 'AF'];

for (let locale, i = 0; i < farsiLocales.length; i++) {
locale = `fa-${farsiLocales[i]}`;
alphanumeric[locale] = alphanumeric.fa;
decimal[locale] = decimal.ar;
}

export const bengaliLocales = ['BD', 'IN'];

for (let locale, i = 0; i < bengaliLocales.length; i++) {
locale = `bn-${bengaliLocales[i]}`;
alpha[locale] = alpha.bn;
alphanumeric[locale] = alphanumeric.bn;
decimal[locale] = decimal['en-US'];
}

// Source: https://en.wikipedia.org/wiki/Decimal_mark
export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
export const commaDecimal = [
'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR',
'id-ID', 'it-IT', 'ku-IQ', 'hi-IN', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT',
'ru-RU', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN',
'ru-RU', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN',
];

for (let i = 0; i < dotDecimal.length; i++) {
Expand Down
14 changes: 13 additions & 1 deletion src/lib/isBase32.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import assertString from './util/assertString';
import merge from './util/merge';

const base32 = /^[A-Z2-7]+=*$/;
const crockfordBase32 = /^[A-HJKMNP-TV-Z0-9]+$/;

export default function isBase32(str) {
const defaultBase32Options = {
crockford: false,
};

export default function isBase32(str, options) {
assertString(str);
options = merge(options, defaultBase32Options);

if (options.crockford) {
return crockfordBase32.test(str);
}

const len = str.length;
if (len % 8 === 0 && base32.test(str)) {
return true;
Expand Down
22 changes: 2 additions & 20 deletions src/lib/isCreditCard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import isLuhnValid from './isLuhnValid';

/* eslint-disable max-len */
const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
Expand All @@ -10,24 +11,5 @@ export default function isCreditCard(str) {
if (!creditCard.test(sanitized)) {
return false;
}
let sum = 0;
let digit;
let tmpNum;
let shouldDouble;
for (let i = sanitized.length - 1; i >= 0; i--) {
digit = sanitized.substring(i, (i + 1));
tmpNum = parseInt(digit, 10);
if (shouldDouble) {
tmpNum *= 2;
if (tmpNum >= 10) {
sum += ((tmpNum % 10) + 1);
} else {
sum += tmpNum;
}
} else {
sum += tmpNum;
}
shouldDouble = !shouldDouble;
}
return !!((sum % 10) === 0 ? sanitized : false);
return isLuhnValid(str);
}
6 changes: 3 additions & 3 deletions src/lib/isDataURI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assertString from './util/assertString';

const validMediaType = /^[a-z]+\/[a-z0-9\-\+]+$/i;
const validMediaType = /^[a-z]+\/[a-z0-9\-\+\.]+$/i;

const validAttribute = /^[a-z\-]+=[a-z0-9\-]+$/i;

Expand All @@ -14,10 +14,10 @@ export default function isDataURI(str) {
}
const attributes = data.shift().trim().split(';');
const schemeAndMediaType = attributes.shift();
if (schemeAndMediaType.substr(0, 5) !== 'data:') {
if (schemeAndMediaType.slice(0, 5) !== 'data:') {
return false;
}
const mediaType = schemeAndMediaType.substr(5);
const mediaType = schemeAndMediaType.slice(5);
if (mediaType !== '' && !validMediaType.test(mediaType)) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function isEmail(str, options) {
// eg. myname <[email protected]>
// the display name is `myname` instead of `myname `, so need to trim the last space
if (display_name.endsWith(' ')) {
display_name = display_name.substr(0, display_name.length - 1);
display_name = display_name.slice(0, -1);
}

if (!validateDisplayName(display_name)) {
Expand Down Expand Up @@ -144,7 +144,7 @@ export default function isEmail(str, options) {
return false;
}

let noBracketdomain = domain.substr(1, domain.length - 2);
let noBracketdomain = domain.slice(1, -1);

if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/isFQDN.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function isFQDN(str, options) {
return false;
}

if (!/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
if (!options.allow_numeric_tld && !/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
return false;
}

Expand Down
8 changes: 2 additions & 6 deletions src/lib/isIP.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ export default function isIP(str, version = '') {
return isIP(str, 4) || isIP(str, 6);
}
if (version === '4') {
if (!IPv4AddressRegExp.test(str)) {
return false;
}
const parts = str.split('.').sort((a, b) => a - b);
return parts[3] <= 255;
return IPv4AddressRegExp.test(str);
}
if (version === '6') {
return !!IPv6AddressRegExp.test(str);
return IPv6AddressRegExp.test(str);
}
return false;
}
35 changes: 35 additions & 0 deletions src/lib/isISO6391.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assertString from './util/assertString';

const isISO6391Set = new Set([
'aa', 'ab', 'ae', 'af', 'ak', 'am', 'an', 'ar', 'as', 'av', 'ay', 'az', 'az',
'ba', 'be', 'bg', 'bh', 'bi', 'bm', 'bn', 'bo', 'br', 'bs',
'ca', 'ce', 'ch', 'co', 'cr', 'cs', 'cu', 'cv', 'cy',
'da', 'de', 'dv', 'dz',
'ee', 'el', 'en', 'eo', 'es', 'et', 'eu',
'fa', 'ff', 'fi', 'fj', 'fo', 'fr', 'fy',
'ga', 'gd', 'gl', 'gn', 'gu', 'gv',
'ha', 'he', 'hi', 'ho', 'hr', 'ht', 'hu', 'hy', 'hz',
'ia', 'id', 'ie', 'ig', 'ii', 'ik', 'io', 'is', 'it', 'iu',
'ja', 'jv',
'ka', 'kg', 'ki', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'kr', 'ks', 'ku', 'kv', 'kw', 'ky',
'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu', 'lv',
'mg', 'mh', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my',
'na', 'nb', 'nd', 'ne', 'ng', 'nl', 'nn', 'no', 'nr', 'nv', 'ny',
'oc', 'oj', 'om', 'or', 'os',
'pa', 'pi', 'pl', 'ps', 'pt',
'qu',
'rm', 'rn', 'ro', 'ru', 'rw',
'sa', 'sc', 'sd', 'se', 'sg', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv', 'sw',
'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'ty',
'ug', 'uk', 'ur', 'uz',
've', 'vi', 'vo',
'wa', 'wo',
'xh',
'yi', 'yo',
'za', 'zh', 'zu',
]);

export default function isISO6391(str) {
assertString(str);
return isISO6391Set.has(str);
}
8 changes: 4 additions & 4 deletions src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ const validators = {
},
IR: (str) => {
if (!str.match(/^\d{10}$/)) return false;
str = (`0000${str}`).substr(str.length - 6);
str = (`0000${str}`).slice(str.length - 6);

if (parseInt(str.substr(3, 6), 10) === 0) return false;
if (parseInt(str.slice(3, 9), 10) === 0) return false;

const lastNumber = parseInt(str.substr(9, 1), 10);
const lastNumber = parseInt(str.slice(9, 10), 10);
let sum = 0;

for (let i = 0; i < 9; i++) {
sum += parseInt(str.substr(i, 1), 10) * (10 - i);
sum += parseInt(str.slice(i, i + 1), 10) * (10 - i);
}

sum %= 11;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/isLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function isLength(str, options) {
min = arguments[1] || 0;
max = arguments[2];
}
const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || [];
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
const len = str.length - surrogatePairs.length;
const len = str.length - presentationSequences.length - surrogatePairs.length;
return len >= min && (typeof max === 'undefined' || len <= max);
}
Loading

0 comments on commit 57a90cf

Please sign in to comment.