url-sanitizer 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "url-sanitizer",
3
- "description": "Sanitize URL",
3
+ "description": "URL sanitizer for Node.js (>=18), browsers and web sites.",
4
4
  "author": "asamuzaK",
5
5
  "homepage": "https://github.com/asamuzaK/urlSanitizer",
6
- "bugs": "https://github.com/asamuzaK/urlSanitizer",
6
+ "bugs": "https://github.com/asamuzaK/urlSanitizer/issues",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "https://github.com/asamuzaK/urlSanitizer.git"
@@ -25,7 +25,9 @@
25
25
  "bundle-src": "esbuild --format=esm --platform=browser --outfile=./dist/url-sanitizer.js --bundle --sourcemap ./bundle/index.js",
26
26
  "bundle-min": "esbuild --format=esm --platform=browser --outfile=./dist/url-sanitizer.min.js --bundle --minify --sourcemap ./bundle/index.js",
27
27
  "char": "node ./modules/index char -i",
28
- "copy": "copyfiles --up=1 --verbose src/**/* --exclude=src/mjs/dompurify.js bundle",
28
+ "copy": "npm-run-all -s copy-*",
29
+ "copy-src": "copyfiles --up=1 --verbose src/**/* --exclude=src/mjs/dompurify.js bundle",
30
+ "copy-repl": "copyfiles --up=1 --verbose repl/dompurify.js bundle/mjs",
29
31
  "include": "node ./modules/index include --dir=iana -i",
30
32
  "lint": "eslint --fix .",
31
33
  "test": "c8 --reporter=text mocha --exit test/*.test.js"
@@ -46,7 +48,7 @@
46
48
  "eslint": "^8.33.0",
47
49
  "eslint-config-standard": "^17.0.0",
48
50
  "eslint-plugin-import": "^2.27.5",
49
- "eslint-plugin-jsdoc": "^39.7.4",
51
+ "eslint-plugin-jsdoc": "^39.7.5",
50
52
  "eslint-plugin-n": "^15.6.1",
51
53
  "eslint-plugin-node": "^11.1.0",
52
54
  "eslint-plugin-promise": "^6.1.1",
@@ -56,5 +58,5 @@
56
58
  "sinon": "^15.0.1",
57
59
  "undici": "^5.16.0"
58
60
  },
59
- "version": "0.7.0"
61
+ "version": "0.7.2"
60
62
  }
@@ -13,10 +13,11 @@ const HEX = 16;
13
13
  const REG_BASE64 = /^[\da-z+/\-_=]+$/i;
14
14
  const REG_DATA_URL = /data:[^,]*,[^"]+/g;
15
15
  const REG_DATA_URL_BASE64 = /data:[^,]*;?base64,[\da-z+/\-_=]+/i;
16
- const REG_MIME_DOM =
17
- /^(?:text\/(?:ht|x)ml|application\/(?:xhtml\+)?xml|image\/svg\+xml)/;
18
16
  const REG_HTML_SP = /[<>"'\s]/g;
19
17
  const REG_HTML_SP_URL_ENC = /%(?:2(?:2|7)|3(?:C|E))/g;
18
+ const REG_HTML_SP_URL_ENC_SHORT = /%(?:2(?:2|7)|3(?:C|E))+?/;
19
+ const REG_MIME_DOM =
20
+ /^(?:text\/(?:ht|x)ml|application\/(?:xhtml\+)?xml|image\/svg\+xml)/;
20
21
  const REG_NUM_REF = /&#(x(?:00)?[\dA-F]{2}|0?\d{1,3});?/ig;
21
22
  const REG_SCHEME = /^[a-z][\da-z+\-.]*$/;
22
23
  const REG_SCHEME_CUSTOM = /^(?:ext|web)\+[a-z]+$/;
@@ -31,7 +32,7 @@ const REG_URL_ENC_HTML_SP = /%26(?:(?:l|g|quo)t|%2339);?/g;
31
32
  * @param {string} str - string
32
33
  * @returns {string} - URL encoded string
33
34
  */
34
- export const getUrlEncodedString = str => {
35
+ export const getURLEncodedString = str => {
35
36
  if (!isString(str)) {
36
37
  throw new TypeError(`Expected String but got ${getType(str)}.`);
37
38
  }
@@ -48,18 +49,12 @@ export const getUrlEncodedString = str => {
48
49
  * @param {string} ch - URL encoded (percent encoded) char
49
50
  * @returns {string} - escaped URL encoded HTML special char / URL encoded char
50
51
  */
51
- export const escapeUrlEncodedHtmlChars = ch => {
52
- if (isString(ch)) {
53
- if (REG_URL_ENC.test(ch)) {
54
- ch = ch.toUpperCase();
55
- } else {
56
- throw new Error(`Invalid URL encoded character: ${ch}`);
57
- }
58
- } else {
59
- throw new TypeError(`Expected String but got ${getType(ch)}.`);
52
+ export const escapeURLEncodedHTMLChars = ch => {
53
+ if (isString(ch) && REG_URL_ENC.test(ch)) {
54
+ ch = ch.toUpperCase();
60
55
  }
61
56
  const [amp, num, lt, gt, quot, apos] =
62
- ['&', '#', '<', '>', '"', "'"].map(getUrlEncodedString);
57
+ ['&', '#', '<', '>', '"', "'"].map(getURLEncodedString);
63
58
  let escapedChar;
64
59
  if (ch === amp) {
65
60
  escapedChar = `${amp}amp;`;
@@ -83,7 +78,7 @@ export const escapeUrlEncodedHtmlChars = ch => {
83
78
  * @param {string} ch - URL encoded char
84
79
  * @returns {string} - unescaped HTML special char
85
80
  */
86
- export const unescapeUrlEncodedHtmlChars = ch => {
81
+ export const unescapeURLEncodedHTMLChars = ch => {
87
82
  let unescapedChar;
88
83
  if (/%26lt;?/.test(ch)) {
89
84
  unescapedChar = '<';
@@ -116,7 +111,7 @@ export const parseBase64 = data => {
116
111
  const textCharCodes = new Set(textChars);
117
112
  let parsedData;
118
113
  if (uint8arr.every(c => textCharCodes.has(c))) {
119
- parsedData = bin.replace(/\s/g, getUrlEncodedString);
114
+ parsedData = bin.replace(/\s/g, getURLEncodedString);
120
115
  } else {
121
116
  parsedData = data;
122
117
  }
@@ -130,7 +125,7 @@ export const parseBase64 = data => {
130
125
  * @param {number} nest - nest level
131
126
  * @returns {string} - parsed string
132
127
  */
133
- export const parseUrlEncodedNumCharRef = (str, nest = 0) => {
128
+ export const parseURLEncodedNumCharRef = (str, nest = 0) => {
134
129
  if (!isString(str)) {
135
130
  throw new TypeError(`Expected String but got ${getType(str)}.`);
136
131
  }
@@ -160,7 +155,7 @@ export const parseUrlEncodedNumCharRef = (str, nest = 0) => {
160
155
  if (textCharCodes.has(num)) {
161
156
  res = `${preNum}${String.fromCharCode(num)}${postNum}`;
162
157
  if (/#x?$/.test(preNum) || /^#(?:x(?:00)?[2-7]|\d)/.test(postNum)) {
163
- res = parseUrlEncodedNumCharRef(res, ++nest);
158
+ res = parseURLEncodedNumCharRef(res, ++nest);
164
159
  }
165
160
  } else if (num < HEX * HEX) {
166
161
  res = `${preNum}${postNum}`;
@@ -177,7 +172,7 @@ export const parseUrlEncodedNumCharRef = (str, nest = 0) => {
177
172
  * @param {string} dom - DOM input
178
173
  * @returns {string} - purified DOM
179
174
  */
180
- export const purifyUrlEncodedDom = dom => {
175
+ export const purifyURLEncodedDOM = dom => {
181
176
  if (!isString(dom)) {
182
177
  throw new TypeError(`Expected String but got ${getType(dom)}.`);
183
178
  }
@@ -249,12 +244,12 @@ export class URISchemes {
249
244
  }
250
245
 
251
246
  /**
252
- * is URI
247
+ * verify URI
253
248
  *
254
249
  * @param {string} uri - URI input
255
250
  * @returns {boolean} - result
256
251
  */
257
- isURI(uri) {
252
+ verify(uri) {
258
253
  let res;
259
254
  if (isString(uri)) {
260
255
  try {
@@ -296,6 +291,7 @@ export class URLSanitizer extends URISchemes {
296
291
  * @param {Array.<string>} opt.allow - array of allowed schemes
297
292
  * @param {Array.<string>} opt.deny - array of denied schemes
298
293
  * @param {Array.<string>} opt.only - array of specific schemes to allow
294
+ * @param {boolean} opt.truncate - truncate tag and/or quote and the rest
299
295
  * @returns {?string} - sanitized URL
300
296
  */
301
297
  sanitize(url, opt = { allow: [], deny: [], only: [] }) {
@@ -303,7 +299,7 @@ export class URLSanitizer extends URISchemes {
303
299
  this.#nest = 0;
304
300
  throw new Error('Data URLs nested too deeply.');
305
301
  }
306
- const { allow, deny, only } = opt ?? {};
302
+ const { allow, deny, only, truncate } = opt ?? {};
307
303
  const schemeMap = new Map([
308
304
  ['data', false],
309
305
  ['file', false],
@@ -375,7 +371,7 @@ export class URLSanitizer extends URISchemes {
375
371
  }
376
372
  }
377
373
  let sanitizedUrl;
378
- if (super.isURI(url)) {
374
+ if (super.verify(url)) {
379
375
  const { hash, href, pathname, protocol, search } = new URL(url);
380
376
  const scheme = protocol.replace(/:$/, '');
381
377
  const schemeParts = scheme.split('+');
@@ -403,7 +399,7 @@ export class URLSanitizer extends URISchemes {
403
399
  parsedData = parseBase64(data);
404
400
  } else {
405
401
  try {
406
- const decodedData = parseUrlEncodedNumCharRef(parsedData);
402
+ const decodedData = parseURLEncodedNumCharRef(parsedData);
407
403
  const { protocol: dataScheme } = new URL(decodedData.trim());
408
404
  const dataSchemeParts = dataScheme.replace(/:$/, '').split('+');
409
405
  if (dataSchemeParts.some(s => REG_SCRIPT.test(s))) {
@@ -456,13 +452,19 @@ export class URLSanitizer extends URISchemes {
456
452
  } else {
457
453
  escapeHtml = true;
458
454
  }
455
+ if (!schemeParts.includes('data') && truncate &&
456
+ REG_HTML_SP_URL_ENC_SHORT.test(urlToSanitize)) {
457
+ const item = REG_HTML_SP_URL_ENC_SHORT.exec(urlToSanitize);
458
+ const { index } = item;
459
+ urlToSanitize = urlToSanitize.substring(0, index);
460
+ }
459
461
  if (urlToSanitize) {
460
462
  sanitizedUrl = urlToSanitize
461
- .replace(REG_HTML_SP, getUrlEncodedString)
462
- .replace(REG_URL_ENC_AMP, escapeUrlEncodedHtmlChars);
463
+ .replace(REG_HTML_SP, getURLEncodedString)
464
+ .replace(REG_URL_ENC_AMP, escapeURLEncodedHTMLChars);
463
465
  if (escapeHtml) {
464
466
  sanitizedUrl = sanitizedUrl
465
- .replace(REG_HTML_SP_URL_ENC, escapeUrlEncodedHtmlChars);
467
+ .replace(REG_HTML_SP_URL_ENC, escapeURLEncodedHTMLChars);
466
468
  this.#nest = 0;
467
469
  }
468
470
  } else {
@@ -475,7 +477,7 @@ export class URLSanitizer extends URISchemes {
475
477
  }
476
478
 
477
479
  /**
478
- * object with extended props based on URL API
480
+ * object with additional properties based on URL API
479
481
  *
480
482
  * @typedef {object} ParsedURL
481
483
  * @property {string} input - URL input
@@ -503,7 +505,7 @@ export class URLSanitizer extends URISchemes {
503
505
  *
504
506
  * @param {string} url - URL input
505
507
  * @param {object} opt - options
506
- * @returns {ParsedURL} - result with extended props based on URL API
508
+ * @returns {ParsedURL} - result with enhanced props based on URL API
507
509
  */
508
510
  parse(url, opt) {
509
511
  if (!isString(url)) {
@@ -543,10 +545,10 @@ export class URLSanitizer extends URISchemes {
543
545
  parsedData.substring(0, index),
544
546
  parsedData.substring(index + htmlChar.length)
545
547
  ];
546
- const unescapedHTMLChar = unescapeUrlEncodedHtmlChars(htmlChar);
547
- parsedData = `${preHtmlChar}${unescapedHTMLChar}${postHtmlChar}`;
548
+ const unescapedHtmlChar = unescapeURLEncodedHTMLChars(htmlChar);
549
+ parsedData = `${preHtmlChar}${unescapedHtmlChar}${postHtmlChar}`;
548
550
  }
549
- purifiedDom = purifyUrlEncodedDom(parsedData);
551
+ purifiedDom = purifyURLEncodedDOM(parsedData);
550
552
  dataUrl.set('data', purifiedDom);
551
553
  } else {
552
554
  dataUrl.set('data', data);
@@ -595,7 +597,7 @@ const urlSanitizer = new URLSanitizer();
595
597
  * @param {string} uri - URI input
596
598
  * @returns {boolean} - result
597
599
  */
598
- const isUri = uri => urlSanitizer.isURI(uri);
600
+ export const isURISync = uri => urlSanitizer.verify(uri);
599
601
 
600
602
  /**
601
603
  * is URI async
@@ -604,7 +606,26 @@ const isUri = uri => urlSanitizer.isURI(uri);
604
606
  * @returns {Promise.<boolean>} - result
605
607
  */
606
608
  export const isURI = async uri => {
607
- const res = await isUri(uri);
609
+ const res = await isURISync(uri);
610
+ return res;
611
+ };
612
+
613
+ /**
614
+ * parse URL sync
615
+ *
616
+ * @param {string} url - URL input
617
+ * @returns {ParsedURL} - result with extended props based on URL API
618
+ */
619
+ export const parseURLSync = url => urlSanitizer.parse(url);
620
+
621
+ /**
622
+ * parse URL async
623
+ *
624
+ * @param {string} url - URL input
625
+ * @returns {Promise.<ParsedURL>} - result with extended props based on URL API
626
+ */
627
+ export const parseURL = async url => {
628
+ const res = await parseURLSync(url);
608
629
  return res;
609
630
  };
610
631
 
@@ -615,7 +636,7 @@ export const isURI = async uri => {
615
636
  * @param {object} opt - options
616
637
  * @returns {?string} - sanitized URL
617
638
  */
618
- const sanitizeUrl = (url, opt) => {
639
+ export const sanitizeURLSync = (url, opt) => {
619
640
  const parsedUrl = urlSanitizer.parse(url, opt ?? {
620
641
  allow: [],
621
642
  deny: [],
@@ -637,33 +658,11 @@ const sanitizeUrl = (url, opt) => {
637
658
  * @returns {Promise.<?string>} - sanitized URL
638
659
  */
639
660
  export const sanitizeURL = async (url, opt) => {
640
- const res = await sanitizeUrl(url, opt);
641
- return res;
642
- };
643
-
644
- /**
645
- * parse URL sync
646
- *
647
- * @param {string} url - URL input
648
- * @returns {ParsedURL} - result with extended props based on URL API
649
- */
650
- const parseUrl = url => urlSanitizer.parse(url);
651
-
652
- /**
653
- * parse URL async
654
- *
655
- * @param {string} url - URL input
656
- * @returns {Promise.<ParsedURL>} - result with extended props based on URL API
657
- */
658
- export const parseURL = async url => {
659
- const res = await parseUrl(url);
661
+ const res = await sanitizeURLSync(url, opt);
660
662
  return res;
661
663
  };
662
664
 
663
- /* export instance and aliases */
665
+ /* export instance */
664
666
  export {
665
- urlSanitizer as default,
666
- isUri as isURISync,
667
- parseUrl as parseURLSync,
668
- sanitizeUrl as sanitizeURLSync
667
+ urlSanitizer as default
669
668
  };