url-sanitizer 0.8.1 → 0.8.4

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 CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  URL sanitizer for Node.js (>=18), browsers and web sites.
10
10
  Sanitize not only regular URLs, but also data URLs and blob URLs.
11
- Also ability to parse URLs and verify URIs.
11
+ It also has the ability to parse URLs and verify URIs.
12
12
  **Experimental**
13
13
 
14
14
 
@@ -21,9 +21,14 @@ npm i url-sanitizer
21
21
  For browsers and web sites, standalone ESM builds are available in `dist/` directory.
22
22
  * node_modules/url-sanitizer/dist/url-sanitizer.js
23
23
  * node_modules/url-sanitizer/dist/url-sanitizer.min.js
24
+ * node_modules/url-sanitizer/dist/url-sanitizer.wo.dompurify.js
25
+ * node_modules/url-sanitizer/dist/url-sanitizer.wo.dompurify.min.js
24
26
 
25
27
  Or, download them from [Releases](https://github.com/asamuzaK/urlSanitizer/releases).
26
28
 
29
+ NOTE: `url-sanitizer.wo.dompurify.js` and `url-sanitizer.wo.dompurify.min.js` are built without [DOMPurify](https://www.npmjs.com/package/dompurify).
30
+ Use them if DOMPurify is exposed as `window.DOMPurify`.
31
+
27
32
 
28
33
  ## Usage
29
34
 
@@ -94,7 +99,7 @@ const blob4 = new Blob(['<svg><g onload="alert(1)"/></svg>'], {
94
99
  type: 'image/svg+xml'
95
100
  });
96
101
  const url4 = URL.createObjectURL(blob4);
97
- const res4 = await func(url, {
102
+ const res4 = await func(url4, {
98
103
  allow: ['blob']
99
104
  });
100
105
  // => 'data:image/svg+xml,%3Csvg%3E%3Cg%3E%3C/g%3E%3C/svg%3E'
@@ -384,7 +389,7 @@ const res2 = urlSanitizer.remove('foo');
384
389
 
385
390
  The following resources have been of great help in the development of the URL Sanitizer.
386
391
 
387
- * [DOMPurify](https://www.npmjs.com/package/dompurify?activeTab=readme)
392
+ * [DOMPurify](https://www.npmjs.com/package/dompurify)
388
393
  * [Uniform Resource Identifier (URI) Schemes - IANA](https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml)
389
394
  * [Encoding -- determine the character encoding of a text file. - file/file](https://github.com/file/file/blob/master/src/encoding.c)
390
395
 
@@ -999,7 +999,7 @@ var require_purify = __commonJS({
999
999
  });
1000
1000
 
1001
1001
  // bundle/mjs/file-reader.js
1002
- var FileReader = window.FileReader;
1002
+ var { FileReader } = window;
1003
1003
  var fileReader = new FileReader();
1004
1004
 
1005
1005
  // bundle/mjs/common.js
@@ -1976,8 +1976,8 @@ var URLSanitizer = class extends URISchemes {
1976
1976
  parsedData = parseBase64(data);
1977
1977
  }
1978
1978
  try {
1979
- const decodedData = parseURLEncodedNumCharRef(parsedData);
1980
- const { protocol: dataScheme } = new URL(decodedData.trim());
1979
+ const decodedData = parseURLEncodedNumCharRef(parsedData).trim();
1980
+ const { protocol: dataScheme } = new URL(decodedData);
1981
1981
  const dataSchemeParts = dataScheme.replace(REG_END_COLON, "").split("+");
1982
1982
  if (dataSchemeParts.some((s) => REG_SCRIPT_BLOB.test(s))) {
1983
1983
  urlToSanitize = "";
@@ -2117,46 +2117,59 @@ var parseURL = async (url) => {
2117
2117
  const res = urlSanitizer.parse(url);
2118
2118
  return res;
2119
2119
  };
2120
- var sanitizeURLSync = (url, opt) => urlSanitizer.sanitize(url, opt ?? {
2121
- allow: [],
2122
- deny: [],
2123
- only: []
2124
- });
2120
+ var sanitizeURLSync = (url, opt) => {
2121
+ let res;
2122
+ if (urlSanitizer.verify(url)) {
2123
+ const { protocol } = new URL(url);
2124
+ if (protocol === "blob:") {
2125
+ URL.revokeObjectURL(url);
2126
+ } else {
2127
+ res = urlSanitizer.sanitize(url, opt ?? {
2128
+ allow: [],
2129
+ deny: [],
2130
+ only: []
2131
+ });
2132
+ }
2133
+ }
2134
+ return res || null;
2135
+ };
2125
2136
  var sanitizeURL = async (url, opt = {
2126
2137
  allow: [],
2127
2138
  deny: [],
2128
2139
  only: []
2129
2140
  }) => {
2130
- const { protocol } = new URL(url);
2131
2141
  let res;
2132
- if (protocol === "blob:") {
2133
- const { allow, deny, only } = opt;
2134
- if (Array.isArray(allow) && allow.includes("blob") && !(Array.isArray(deny) && deny.includes("blob")) || Array.isArray(only) && only.includes("blob")) {
2135
- let data;
2136
- try {
2137
- data = await fetch(url).then((r) => r.blob()).then(createDataURLFromBlob);
2138
- URL.revokeObjectURL(url);
2139
- } catch (e) {
2140
- }
2141
- if (data) {
2142
- if (Array.isArray(only)) {
2143
- if (!only.includes("data")) {
2144
- only.push("data");
2145
- }
2146
- } else if (Array.isArray(allow)) {
2147
- if (!allow.includes("data")) {
2148
- allow.push("data");
2149
- }
2150
- if (Array.isArray(deny) && deny.includes("data")) {
2151
- const i = deny.indexOf("data");
2152
- deny.splice(i, 1);
2142
+ if (urlSanitizer.verify(url)) {
2143
+ const { protocol } = new URL(url);
2144
+ if (protocol === "blob:") {
2145
+ const { allow, deny, only } = opt;
2146
+ if (Array.isArray(allow) && allow.includes("blob") && !(Array.isArray(deny) && deny.includes("blob")) || Array.isArray(only) && only.includes("blob")) {
2147
+ let data;
2148
+ try {
2149
+ data = await fetch(url).then((r) => r.blob()).then(createDataURLFromBlob);
2150
+ } catch (e) {
2151
+ }
2152
+ if (data) {
2153
+ if (Array.isArray(only)) {
2154
+ if (!only.includes("data")) {
2155
+ only.push("data");
2156
+ }
2157
+ } else if (Array.isArray(allow)) {
2158
+ if (!allow.includes("data")) {
2159
+ allow.push("data");
2160
+ }
2161
+ if (Array.isArray(deny) && deny.includes("data")) {
2162
+ const i = deny.indexOf("data");
2163
+ deny.splice(i, 1);
2164
+ }
2153
2165
  }
2166
+ res = urlSanitizer.sanitize(data, opt);
2154
2167
  }
2155
- res = urlSanitizer.sanitize(data, opt);
2156
2168
  }
2169
+ URL.revokeObjectURL(url);
2170
+ } else {
2171
+ res = urlSanitizer.sanitize(url, opt);
2157
2172
  }
2158
- } else {
2159
- res = urlSanitizer.sanitize(url, opt);
2160
2173
  }
2161
2174
  return res || null;
2162
2175
  };
@@ -2171,6 +2184,7 @@ export {
2171
2184
  };
2172
2185
  /*!
2173
2186
  * URL Sanitizer
2187
+ *
2174
2188
  * @license MIT
2175
2189
  * @copyright asamuzaK (Kazz)
2176
2190
  * @see {@link https://github.com/asamuzaK/urlSanitizer/blob/main/LICENSE}