-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new validator: isMailtoURI, validate the mailto link URI format (#2188)
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import trim from './trim'; | ||
import isEmail from './isEmail'; | ||
import assertString from './util/assertString'; | ||
|
||
function parseMailtoQueryString(queryString) { | ||
const allowedParams = new Set(['subject', 'body', 'cc', 'bcc']), | ||
query = { cc: '', bcc: '' }; | ||
let isParseFailed = false; | ||
|
||
const queryParams = queryString.split('&'); | ||
|
||
if (queryParams.length > 4) { | ||
return false; | ||
} | ||
|
||
for (const q of queryParams) { | ||
const [key, value] = q.split('='); | ||
|
||
// checked for invalid and duplicated query params | ||
if (key && !allowedParams.has(key)) { | ||
isParseFailed = true; | ||
break; | ||
} | ||
|
||
if (value && (key === 'cc' || key === 'bcc')) { | ||
query[key] = value; | ||
} | ||
|
||
if (key) { | ||
allowedParams.delete(key); | ||
} | ||
} | ||
|
||
return isParseFailed ? false : query; | ||
} | ||
|
||
export default function isMailtoURI(url, options) { | ||
assertString(url); | ||
|
||
if (url.indexOf('mailto:') !== 0) { | ||
return false; | ||
} | ||
|
||
const [to = '', queryString = ''] = url.replace('mailto:', '').split('?'); | ||
|
||
if (!to && !queryString) { | ||
return true; | ||
} | ||
|
||
const query = parseMailtoQueryString(queryString); | ||
|
||
if (!query) { | ||
return false; | ||
} | ||
|
||
return `${to},${query.cc},${query.bcc}` | ||
.split(',') | ||
.every((email) => { | ||
email = trim(email, ' '); | ||
|
||
if (email) { | ||
return isEmail(email, options); | ||
} | ||
|
||
return true; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14253,4 +14253,55 @@ describe('Validators', () => { | |
], | ||
}); | ||
}); | ||
it('should validate mailto URI', () => { | ||
test({ | ||
validator: 'isMailtoURI', | ||
valid: [ | ||
'mailto:?subject=something&[email protected]', | ||
'mailto:?subject=something&[email protected],[email protected],', | ||
'mailto:?subject=something&[email protected]', | ||
'mailto:?subject=something&[email protected],[email protected]', | ||
'mailto:[email protected],[email protected]', | ||
'mailto:[email protected],[email protected]', | ||
'mailto:[email protected]', | ||
'mailto:[email protected]', | ||
'mailto:?subject=something&body=something else', | ||
'mailto:?subject=something&body=something else&[email protected],[email protected]', | ||
'mailto:?subject=something&body=something else&[email protected],[email protected]', | ||
'mailto:?subject=something&body=something else&[email protected]&[email protected],[email protected]', | ||
'mailto:[email protected]', | ||
'mailto:[email protected]?', | ||
'mailto:[email protected]?subject=something', | ||
'mailto:[email protected]?subject=something&[email protected]', | ||
'mailto:[email protected]?subject=something&[email protected],[email protected],', | ||
'mailto:[email protected]?subject=something&[email protected]', | ||
'mailto:[email protected]?subject=something&[email protected],[email protected]', | ||
'mailto:[email protected][email protected],[email protected]', | ||
'mailto:[email protected][email protected],[email protected]', | ||
'mailto:[email protected][email protected]', | ||
'mailto:[email protected][email protected]&', | ||
'mailto:[email protected]?subject=something&body=something else', | ||
'mailto:[email protected]?subject=something&body=something else&[email protected],[email protected]', | ||
'mailto:[email protected]?subject=something&body=something else&[email protected],[email protected]', | ||
'mailto:[email protected]?subject=something&body=something else&[email protected]&[email protected],[email protected]', | ||
'mailto:', | ||
], | ||
invalid: [ | ||
'', | ||
'somthing', | ||
'[email protected]', | ||
'mailto:?subject=okay&subject=444', | ||
'mailto:?subject=something&wrong=888', | ||
'mailto:somename@gmail.com', | ||
'mailto:[email protected]?cc=somename@gmail.com', | ||
'mailto:[email protected]?bcc=somename@gmail.com', | ||
'mailto:[email protected]?bcc=somename@gmail.com&bcc', | ||
'mailto:[email protected]?subject=anything&body=nothing&cc=&bcc=&key=', | ||
'mailto:[email protected]?cc=somename', | ||
'mailto:somename', | ||
'mailto:[email protected]?subject=something&body=something else&[email protected]&[email protected],[email protected]&', | ||
'mailto:?subject=something&body=something else&[email protected]&[email protected],[email protected]&', | ||
], | ||
}); | ||
}); | ||
}); |