url-sanitizer 5.3.6 → 6.0.0
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/README.md +12 -25
- package/dist/url-sanitizer.min.js +4 -3
- package/dist/url-sanitizer.min.js.map +4 -4
- package/package.json +8 -15
- package/src/index.js +3 -10
- package/src/lib/iana/uri-schemes.json +1 -1
- package/src/main/dompurify.js +11 -0
- package/src/{mjs → main}/filter.js +86 -34
- package/src/{mjs → main}/sanitize.js +13 -10
- package/types/index.d.ts +1 -2
- package/types/main/dompurify.d.ts +1 -0
- package/types/{mjs → main}/filter.d.ts +6 -2
- package/dist/url-sanitizer-wo-dompurify.min.js +0 -9
- package/dist/url-sanitizer-wo-dompurify.min.js.map +0 -7
- package/src/mjs/dompurify.js +0 -15
- package/types/mjs/dompurify.d.ts +0 -1
- /package/src/{mjs → main}/common.js +0 -0
- /package/src/{mjs → main}/constant.js +0 -0
- /package/src/{mjs → main}/regexp.js +0 -0
- /package/src/{mjs → main}/scheme.js +0 -0
- /package/src/{mjs → main}/text-chars.js +0 -0
- /package/src/{mjs → main}/utility.js +0 -0
- /package/types/{mjs → main}/common.d.ts +0 -0
- /package/types/{mjs → main}/constant.d.ts +0 -0
- /package/types/{mjs → main}/regexp.d.ts +0 -0
- /package/types/{mjs → main}/sanitize.d.ts +0 -0
- /package/types/{mjs → main}/scheme.d.ts +0 -0
- /package/types/{mjs → main}/text-chars.d.ts +0 -0
- /package/types/{mjs → main}/utility.d.ts +0 -0
package/README.md
CHANGED
|
@@ -54,7 +54,6 @@ CDN-based ESM imports (e.g., `esm.sh`, `jsdelivr`, or `unpkg`) may work, but are
|
|
|
54
54
|
Standalone ESM builds are available in the `dist/` directory:
|
|
55
55
|
|
|
56
56
|
* node_modules/url-sanitizer/dist/url-sanitizer.min.js
|
|
57
|
-
* node_modules/url-sanitizer/dist/url-sanitizer-wo-dompurify.min.js
|
|
58
57
|
|
|
59
58
|
Alternatively, download them from [Releases](https://github.com/asamuzaK/urlSanitizer/releases).
|
|
60
59
|
``` html
|
|
@@ -63,15 +62,6 @@ Alternatively, download them from [Releases](https://github.com/asamuzaK/urlSani
|
|
|
63
62
|
</script>
|
|
64
63
|
```
|
|
65
64
|
|
|
66
|
-
**Note:** `url-sanitizer-wo-dompurify.min.js` is a lightweight build without [DOMPurify](https://www.npmjs.com/package/dompurify) bundled.
|
|
67
|
-
If you use this build, ensure DOMPurify is exposed globally (e.g., `window.DOMPurify`), otherwise the sanitizer will throw an error at runtime.
|
|
68
|
-
``` html
|
|
69
|
-
<script src="https://unpkg.com/dompurify/dist/purify.min.js"></script>
|
|
70
|
-
<script type="module">
|
|
71
|
-
import urlSanitizer from 'path/to/url-sanitizer-wo-dompurify.min.js';
|
|
72
|
-
</script>
|
|
73
|
-
```
|
|
74
|
-
|
|
75
65
|
## Usage
|
|
76
66
|
|
|
77
67
|
``` javascript
|
|
@@ -96,7 +86,6 @@ Sanitizes the given URL asynchronously.
|
|
|
96
86
|
* You can restrict the allowed blob size using the `opt.maxBlobSize` option.
|
|
97
87
|
Default is `16_777_216` (16MB).
|
|
98
88
|
While 16MB may be suitable for client-side processing, it is highly recommended to reduce this limit to `1_048_576` (1MB) or `2_097_152` (2MB) in server-side applications.
|
|
99
|
-
Parsing large HTML/SVG contents inside Data/Blob URLs via JSDOM can consume massive amounts of V8 heap memory.
|
|
100
89
|
|
|
101
90
|
#### Parameters
|
|
102
91
|
|
|
@@ -210,10 +199,10 @@ const res7_2 = await sanitizeURL('https://example.com/"onmouseover="alert(1)"',
|
|
|
210
199
|
|
|
211
200
|
Synchronous version of `sanitizeURL()`.
|
|
212
201
|
|
|
213
|
-
* `
|
|
202
|
+
* `file` schemes must be explicitly allowed.
|
|
214
203
|
* **File URLs:** Allowing the `file` scheme can be extremely dangerous in web applications. [See above](#about-file-scheme)
|
|
215
|
-
* **Blob URLs:** `blob`
|
|
216
|
-
Use the async version for `blob
|
|
204
|
+
* **Blob URLs and Data URLs:** `blob` and `data` schemes are **not supported** and will return `null`.
|
|
205
|
+
Use the async version for `blob` and `data` URLs.
|
|
217
206
|
However, to prevent memory leaks, it is highly recommended to set the `opt.revokeObjectURL` option to `true` so that the unsupported blob URL is properly revoked before returning `null`.
|
|
218
207
|
|
|
219
208
|
### inspectURL(url, opt)
|
|
@@ -343,8 +332,6 @@ Specifically, it verifies that the input has a correct URI syntax, is not a deni
|
|
|
343
332
|
* The scheme is registered in the default list (e.g., IANA registered schemes, `https`, `mailto`).
|
|
344
333
|
* The scheme has a custom prefix like `web+` or `ext+` (which are always allowed for web applications).
|
|
345
334
|
|
|
346
|
-
**Note:** `isURISync()` is deprecated. Please use `isValidURI()` instead.
|
|
347
|
-
|
|
348
335
|
#### Parameters
|
|
349
336
|
|
|
350
337
|
* uri **string** URI input.
|
|
@@ -445,22 +432,22 @@ Execution times were measured using [mitata](https://github.com/evanwashere/mita
|
|
|
445
432
|
|
|
446
433
|
| URL Type | `url-sanitizer` | [@braintree/sanitize-url](https://www.npmjs.com/package/@braintree/sanitize-url) | [strict-url-sanitise](https://www.npmjs.com/package/strict-url-sanitise) |
|
|
447
434
|
| :--- | :--- | :--- | :--- |
|
|
448
|
-
| **Normal HTTPS URL** |
|
|
449
|
-
| **HTTPS URL
|
|
450
|
-
| **XSS Scheme URL** |
|
|
451
|
-
| **
|
|
452
|
-
| **
|
|
453
|
-
| **Invalid URL** |
|
|
435
|
+
| **Normal HTTPS URL** | 2.06 µs/iter | 4.23 µs/iter | 4.30 µs/iter |
|
|
436
|
+
| **HTTPS XSS URL** | 3.08 µs/iter<br>returns sanitized URL | 5.13 µs/iter<br>returns encoded URL (not fully sanitized against XSS) | 7.06 µs/iter<br>returns encoded URL (not fully sanitized against XSS) |
|
|
437
|
+
| **XSS Scheme URL** | 2.14 µs/iter<br>returns `null` | 1.69 µs/iter<br>returns `about:blank` | 7.67 µs/iter<br>throws `Error` |
|
|
438
|
+
| **Data URL** | 197.24 µs/iter<br>returns sanitized data URL | 2.45 µs/iter<br>returns `about:blank` | 7.62 µs/iter<br>throws `Error` |
|
|
439
|
+
| **Blob URL** | 319.98 µs/iter<br>returns sanitized data URL | 2.31 µs/iter<br>returns blob URL as-is | 7.59 µs/iter<br>throws `Error` |
|
|
440
|
+
| **Invalid URL** | 0.71 µs/iter<br>returns `null` | 1.36 µs/iter<br>returns invalid URL as-is | 16.70 µs/iter<br>throws `Error` |
|
|
454
441
|
|
|
455
442
|
### Characteristics & Trade-offs
|
|
456
443
|
|
|
457
444
|
* **Optimized for Standard Routing & XSS Rejection**
|
|
458
|
-
* For **normal HTTP(S) URLs** and URLs containing common XSS patterns, `url-sanitizer` achieves low latency by leveraging
|
|
445
|
+
* For **normal HTTP(S) URLs** and URLs containing common XSS patterns, `url-sanitizer` achieves low latency by leveraging native `URL` parsing and applying optimized early-return paths when strict inspection is unnecessary.
|
|
459
446
|
* **Exceptional Invalid URL Handling (Fail-Fast)**
|
|
460
|
-
* For **invalid URLs** (such as malformed schemes or unparseable formats), `url-sanitizer`
|
|
447
|
+
* For **invalid URLs** (such as malformed schemes or unparseable formats), `url-sanitizer` achieves sub-microsecond performance (~719.54 ns) via a fail-fast architecture based on native parsing, avoiding unnecessary sanitization steps after validation failure.
|
|
461
448
|
* **Deep Inspection for High-Risk Payloads**
|
|
462
449
|
* For **Data URLs** and **Blob URLs**, `url-sanitizer` prioritizes security over raw execution speed. The following inspection pipeline is applied:
|
|
463
|
-
1. If Blob URL is given, convert it to a Data URL.
|
|
450
|
+
1. If a Blob URL is given, convert it to a Data URL.
|
|
464
451
|
2. Parses and normalizes the Data URL structure.
|
|
465
452
|
3. Decodes Base64 payloads when present.
|
|
466
453
|
4. Decodes URL-encoded content and numeric character references.
|