![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
url-sanitizer
Advanced tools
URL sanitizer for Node.js (>=18), browsers and web sites. Experimental
npm i url-sanitizer
For browsers and web sites, standalone ESM builds are available in dist/
directory.
Or, download the source code from Releases.
import urlSanitizer, {
isURI, isURISync, sanitizeURL, sanitizeURLSync
} from 'url-sanitizer';
Sanitize the given URL.
data
and/or file
schemes must be explicitly allowed.javascript
and/or vbscript
schemes can not be allowed.Returns Promise<string?> sanitized URL, null
able
const res1 = await sanitizeURL('http://example.com/?<script>alert(1);</script>')
.then(res => decodeURIComponent(res));
// -> 'http://example.com/?<script>alert(1);</script>'
const res2 = await sanitizeURL('data:text/html,<script>alert(1);</script>', {
allow: ['data']
}).then(res => decodeURIComponent(res));
// -> 'data:text/html,<script>alert(1);</script>'
// Can parse and sanitize base64 encoded data
const base64data3 = btoa('<script>alert(1);</script>');
const res3 = await sanitizeURL(`data:text/html;base64,${base64data3}`, {
allow: ['data']
}).then(res => decodeURIComponent(res));
// -> 'data:text/html,<script>alert(1);</script>'
// Also an option if you don't want to escape tags and quotes in data URL
// But use it with care
const res4 = await sanitizeURL('data:image/svg+xml,%3Csvg%3E%3C/svg%3E', {
allow: ['data'],
escapeTags: false
}).then(res => decodeURIComponent(res));
// -> 'data:image/svg+xml,<svg></svg>'
const res5 = await sanitizeURL('data:text/html,%3Cscript%3Ealert(1);%3C/script%3E', {
allow: ['data'],
escapeTags: false
}).then(res => decodeURIComponent(res));
// WATCH OUT!!!
// -> 'data:text/html,<script>alert(1);</script>'
Synchronous version of the sanitizeURL().
Determines whether the given URI is valid.
uri
string URI inputReturns Promise<boolean?> result
const res1 = await isURI('https://example.com/foo');
// -> true
const res2 = await isURI('mailto:foo@example.com');
// -> true
const res3 = await isURI('foo:bar');
// -> false
const res4 = await isURI('web+foo:bar');
// -> true
Synchronous version of the isURI().
Get an array of URI schemes registered at iana.org.
moz-extension
scheme added by default.Returns Array<string> array of registered URI schemes
const schemes = urlSanitizer.get();
// -> ['aaa', 'aaas', 'about', 'acap', 'acct', 'acd', 'acr', ...];
Check if the given scheme is registered.
scheme
string schemeReturns boolean result
true
for web+*
and/or ext+*
schemesconst res1 = urlSanitizer.has('https');
// -> true
const res2 = urlSanitizer.has('foo');
// -> false
const res3 = uriSanitizer.has('web+foo');
// -> true
Add a scheme to the list of URI schemes.
javascript
and/or vbscript
schemes can not be registered. It throws.scheme
string schemeReturns Array<string> array of registered URI schemes
const res = urlSanitizer.add('foo');
// -> ['aaa', 'aaas', 'about', 'acap', 'acct', 'acd', ... 'foo', ...];
Remove a scheme from the list of URI schemes.
scheme
string schemeReturns boolean result
true
if the scheme is successfully removed, false
otherwise.const res1 = urlSanitizer.remove('aaa');
// -> true
const res2 = urlSanitizer.remove('foo');
// -> false
FAQs
URL sanitizer for Node.js, browsers and web sites.
The npm package url-sanitizer receives a total of 0 weekly downloads. As such, url-sanitizer popularity was classified as not popular.
We found that url-sanitizer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.