![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
url-sanitizer
Advanced tools
URL sanitizer for Node.js, browsers and web sites. Sanitize not only regular URLs, but also data URLs and blob URLs. It also has the ability to parse URLs and verify URIs.
npm i url-sanitizer
For browsers and web sites, standalone ESM builds are available in dist/
directory.
Or, download them from Releases.
NOTE: url-sanitizer-wo-dompurify.min.js
is built without DOMPurify.
If you use it, make sure DOMPurify is exposed globally, e.g. window.DOMPurify
.
import urlSanitizer, {
isURI, isURISync, parseURL, parseURLSync, sanitizeURL, sanitizeURLSync
} from 'url-sanitizer';
Sanitize the given URL.
blob
, data
and file
schemes must be explicitly allowed.blob
URL, returns a sanitized data
URL.url
string URL input.opt
object? Options.
opt.allow
Array<string>? Array of allowed schemes, e.g. ['data']
.opt.deny
Array<string>? Array of denied schemes, e.g. ['web+foo']
.opt.only
Array<string>? Array of specific schemes to allow, e.g. ['git', 'https']
.
only
takes precedence over allow
and deny
.opt.remove
boolean? Remove tag or quote and the rest following it.Returns Promise<string?> Sanitized URL, null
able.
// Sanitize tags and quotes
const res1 = await sanitizeURL('http://example.com/"onmouseover="alert(1)"?<script>alert(\'XSS\');</script>');
// => 'http://example.com/%26quot;onmouseover=%26quot;alert(1)%26quot;?%26lt;script%26gt;alert(%26%2339;XSS%26%2339;);%26lt;/script%26gt;'
console.log(decodeURIComponent(res1));
// => 'http://example.com/"onmouseover="alert(1)"?<script>alert('XSS');</script>'
// Parse and purify data URL
const res2 = await sanitizeURL('data:text/html,<div><script>alert(1);</script></div><p onclick="alert(2)"></p>', {
allow: ['data']
})
// => 'data:text/html,%3Cdiv%3E%3C/div%3E%3Cp%3E%3C/p%3E'
console.log(decodeURIComponent(res2));
// => 'data:text/html,<div></div><p></p>'
// Can parse and purify base64 encoded data
const base64data3 = btoa('<div><script>alert(1);</script></div>');
const res3 = await sanitizeURL(`data:text/html;base64,${base64data3}`, {
allow: ['data']
})
// => 'data:text/html,%3Cdiv%3E%3C/div%3E'
console.log(decodeURIComponent(res3));
// => 'data:text/html,<div></div>'
const base64data3_2 = btoa('<div><img src="javascript:alert(1)"></div>');
const res3_2 = await sanitizeURL(`data:text/html;base64,${base64data3_2}`);
// => 'data:text/html,%3Cdiv%3E%3Cimg%3E%3C/div%3E'
console.log(decodeURIComponent(res3_2));
// => 'data:text/html,<div><img></div>'
// Can parse and sanitize blob URL
const blob4 = new Blob(['<svg><g onload="alert(1)"/></svg>'], {
type: 'image/svg+xml'
});
const url4 = URL.createObjectURL(blob4);
const res4 = await sanitizeURL(url4, {
allow: ['blob']
});
// => 'data:image/svg+xml,%3Csvg%3E%3Cg%3E%3C/g%3E%3C/svg%3E'
console.log(decodeURIComponent(res4));
// => 'data:image/svg+xml,<svg><g></g></svg>'
// Deny if the scheme matches the `deny` list
const res5 = await sanitizeURL('web+foo://example.com', {
deny: ['web+foo']
});
// => null
// Allow only if the scheme matches the `only` list
const res6 = await sanitizeURL('http://example.com', {
only: ['data', 'git', 'https']
});
// => null
const res6_2 = await sanitizeURL('https://example.com/"onmouseover="alert(1)"', {
only: ['data', 'git', 'https']
});
// => 'https://example.com/%26quot;onmouseover=%26quot;alert(1)%26quot;'
console.log(decodeURIComponent(res6_2));
// => 'https://example.com/"onmouseover="alert(1)"'
// `only` also allows combination of the schemes in the list
const res7 = await sanitizeURL('git+https://example.com/foo.git?<script>alert(1)</script>', {
only: ['data', 'git', 'https']
});
// => 'git+https://example.com/foo.git?%26lt;script%26gt;alert(1)%26lt;/script%26gt;'
console.log(decodeURIComponent(res7));
// => 'git+https://example.com/foo.git?<script>alert(1)</script>'
// Remove the tag or quote and the rest of the URL following it.
const res8 = await sanitizeURL('https://example.com/" onclick="alert(1)"', {
remove: true
});
// => 'https://example.com/'
const res8_2 = await sanitizeURL('https://example.com/?<script>alert(1)</script>', {
remove: true
});
// => 'https://example.com/?'
Synchronous version of the sanitizeURL().
data
and file
schemes must be explicitly allowed.blob
scheme is not supported, returns null
.
Use async sanitizeURL() for blob
.Parse the given URL.
url
string URL input.Returns Promise<ParsedURL> Result.
Object with additional properties based on URL API.
Type: object
input
string URL input.valid
boolean Is valid URI.data
object? Parsed result of data URL, null
able.
href
string? Sanitized URL input.origin
string? Scheme, domain and port of the sanitized URL.protocol
string? Protocol scheme of the sanitized URL.username
string? Username specified before the domain name.password
string? Password specified before the domain name.host
string? Domain and port of the sanitized URL.hostname
string? Domain of the sanitized URL.port
string? Port number of the sanitized URL.pathname
string? Path of the sanitized URL.search
string? Query string of the sanitized URL.hash
string? Fragment identifier of the sanitized URL.const res1 = await parseURL('javascript:alert(1)');
/* => {
input: 'javascript:alert(1)',
valid: false
} */
const res2 = await parseURL('https://www.example.com/?foo=bar#baz');
/* => {
input: 'https://www.example.com/?foo=bar#baz',
valid: true,
data: null,
href: 'https://www.example.com/?foo=bar#baz',
origin: 'https://www.example.com',
protocol: 'https:',
hostname: 'www.example.com',
pathname: '/',
search: '?foo=bar',
hash: '#baz',
...
} */
// base64 encoded SVG '<svg><g onclick="alert(1)"/></svg>'
const res3 = await parseURL('data:image/svg+xml;base64,PHN2Zz48ZyBvbmNsaWNrPSJhbGVydCgxKSIvPjwvc3ZnPg==');
/* => {
input: 'data:image/svg+xml;base64,PHN2Zz48ZyBvbmNsaWNrPSJhbGVydCgxKSIvPjwvc3ZnPg==',
valid: true,
data: {
mime: 'image/svg+xml',
base64: false,
data: '%3Csvg%3E%3Cg%3E%3C/g%3E%3C/svg%3E'
},
href: 'data:image/svg+xml,%3Csvg%3E%3Cg%3E%3C/g%3E%3C/svg%3E',
origin: 'null',
protocol: 'data:',
pathname: 'image/svg+xml,%3Csvg%3E%3Cg%3E%3C/g%3E%3C/svg%3E',
...
} */
// base64 encoded PNG
const res4 = await parseURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==');
/* => {
input: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
valid: true,
data: {
mime: 'image/png',
base64: true,
data: 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
},
href: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
origin: 'null',
protocol: 'data:',
pathname: 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
...
} */
// Note that blob URLs are parsed but not yet sanitized
const blob5 = new Blob(['<svg><g onload="alert(1)"/></svg>'], {
type: 'image/svg+xml'
});
const url5 = URL.createObjectURL(blob5);
const res5 = await parseURL(url5);
/* => {
input: 'blob:nodedata:82ecc5a4-aea8-48d7-a407-64e2ef0913da',
valid: true,
data: null,
href: 'blob:nodedata:82ecc5a4-aea8-48d7-a407-64e2ef0913da',
origin: 'null',
protocol: 'blob:',
pathname: 'nodedata:82ecc5a4-aea8-48d7-a407-64e2ef0913da',
...
} */
Synchronous version of the parseURL().
Verify if the given URI is valid and registered.
uri
string URI input.Returns Promise<boolean> Result.
true
for web+*
and ext+*
schemes, except web+javascript
, web+vbscript
, ext+javascript
, ext+vbscript
.false
for javascript
and vbscript
schemes.const res1 = await isURI('https://example.com/foo');
// => true
const res2 = await isURI('javascript:alert(1)');
// => false
const res3 = await isURI('mailto:foo@example.com');
// => true
const res4 = await isURI('foo:bar');
// => false
const res5 = await isURI('web+foo:bar');
// => true
const res6 = await isURI('web+javascript:alert(1)');
// => false
Synchronous version of the isURI().
Instance of the sanitizer.
Get a list of registered URI schemes.
Returns Array<string> Array of registered URI schemes.
moz-extension
scheme added.const schemes = urlSanitizer.get();
// => ['aaa', 'aaas', 'about', 'acap', 'acct', ...]
Check if the given scheme is registered.
scheme
string Scheme.Returns boolean Result.
const res1 = urlSanitizer.has('https');
// => true
const res2 = urlSanitizer.has('foo');
// => false
Add a scheme to the list of registered URI schemes.
javascript
and vbscript
schemes can not be registered. It throws.scheme
string Scheme.Returns Array<string> Array of registered URI schemes.
console.log(urlSanitizer.has('foo'));
// => false
const res = urlSanitizer.add('foo');
// => ['aaa', 'aaas', 'about', 'acap', ... 'foo', ...]
console.log(urlSanitizer.has('foo'));
// => true
Remove a scheme from the list of registered URI schemes.
scheme
string Scheme.Returns boolean Result.
true
if the scheme is successfully removed, false
otherwise.console.log(urlSanitizer.has('aaa'));
// => true
const res1 = urlSanitizer.remove('aaa');
// => true
console.log(urlSanitizer.has('aaa'));
// => false
const res2 = urlSanitizer.remove('foo');
// => false
Reset sanitizer.
Returns void
The following resources have been of great help in the development of the URL Sanitizer.
Copyright (c) 2023 asamuzaK (Kazz)
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.