url-sanitizer 0.8.0 → 0.8.3

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
@@ -7,6 +7,8 @@
7
7
  [![license](https://img.shields.io/github/license/asamuzaK/urlSanitizer)](LICENSE)
8
8
 
9
9
  URL sanitizer for Node.js (>=18), browsers and web sites.
10
+ Sanitize not only regular URLs, but also data URLs and blob URLs.
11
+ It also has the ability to parse URLs and verify URIs.
10
12
  **Experimental**
11
13
 
12
14
 
@@ -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,42 +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
- const blob = await fetch(url).then((r) => r.blob());
2136
- const data = await createDataURLFromBlob(blob);
2137
- if (data) {
2138
- if (Array.isArray(only)) {
2139
- if (!only.includes("data")) {
2140
- only.push("data");
2141
- }
2142
- } else if (Array.isArray(allow)) {
2143
- if (!allow.includes("data")) {
2144
- allow.push("data");
2145
- }
2146
- if (Array.isArray(deny) && deny.includes("data")) {
2147
- const i = deny.indexOf("data");
2148
- 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
+ }
2149
2165
  }
2166
+ res = urlSanitizer.sanitize(data, opt);
2150
2167
  }
2151
- res = urlSanitizer.sanitize(data, opt);
2152
2168
  }
2169
+ URL.revokeObjectURL(url);
2170
+ } else {
2171
+ res = urlSanitizer.sanitize(url, opt);
2153
2172
  }
2154
- } else {
2155
- res = urlSanitizer.sanitize(url, opt);
2156
2173
  }
2157
2174
  return res || null;
2158
2175
  };