![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 (>=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 them from Releases.
import urlSanitizer, {
isURI, isURISync, parseURL, parseURLSync, sanitizeURL, sanitizeURLSync
} from 'url-sanitizer';
Sanitize the given URL.
data
and file
schemes must be explicitly 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>'
const res4 = await sanitizeURL('web+foo://example.com', {
deny: ['web+foo']
});
// => null
const res5 = await sanitizeURL('http://example.com', {
only: ['data', 'git', 'https']
});
// => null
const res6 = await sanitizeURL('https://example.com/"onmouseover="alert(1)"', {
only: ['data', 'git', 'https']
}).then(res => decodeURIComponent(res));
// => 'https://example.com/"onmouseover="alert(1)"'
const res7 = await sanitizeURL('data:text/html,<script>alert(1);</script>', {
only: ['data', 'git', 'https']
}).then(res => decodeURIComponent(res));
// => 'data:text/html,<script>alert(1);</script>'
// `only` option also allows combinations of the specified schemes
const res8 = await sanitizeURL('git+https://example.com', {
only: ['data', 'git', 'https']
}).then(res => decodeURIComponent(res));;
// => 'git+https://example.com'
Synchronous version of the sanitizeURL().
Parse the given URL.
url
string URL input.Returns Promise<ParsedURL> Result.
Object with extended 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 Same as URL API.origin
string Same as URL API.protocol
string Same as URL API.username
string Same as URL API.password
string Same as URL API.host
string Same as URL API.hostname
string Same as URL API.port
string Same as URL API.pathname
string Same as URL API.search
string Same as URL API.searchParams
object Same as URL API.hash
string Same as URL API.const res1 = await parseURL('javascript:alert(1)');
/* => {
input: 'javascript:alert(1)',
valid: false
} */
const res2 = await parseURL('https://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 onload="alert(1)"/></svg>'
const res3 = await parseURL('data:image/svg+xml;base64,PHN2Zz48ZyBvbmxvYWQ9ImFsZXJ0KDEpIi8+PC9zdmc+');
/* => {
input: 'data:image/svg+xml;base64,PHN2Zz48ZyBvbmxvYWQ9ImFsZXJ0KDEpIi8+PC9zdmc+',
valid: true,
data: {
mime: 'image/svg+xml',
base64: false,
data: '%26lt;svg%26gt;%26lt;g%20onload=%26quot;alert(1)%26quot;/%26gt;%26lt;/svg%26gt;'
},
href: 'data:image/svg+xml,%26lt;svg%26gt;%26lt;g%20onload=%26quot;alert(1)%26quot;/%26gt;%26lt;/svg%26gt;',
protocol: 'data:',
pathname: 'image/svg+xml,%26lt;svg%26gt;%26lt;g%20onload=%26quot;alert(1)%26quot;/%26gt;%26lt;/svg%26gt;',
...
} */
// 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==',
protocol: 'data:',
pathname: 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==',
...
} */
Synchronous version of the parseURL().
Determines whether the given URI is valid.
uri
string URI input.Returns Promise<boolean> Result.
true
for web+*
and ext+*
schemes, except web+javascript
, web+vbscript
, ext+javascript
, ext+vbscript
.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
const res5 = await isURI('web+javascript:alert(1)');
// => false
Synchronous version of the isURI().
Instance of the sanitizer.
Get an array of URI schemes registered at iana.org.
moz-extension
scheme added.Returns Array<string> Array of registered URI schemes.
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 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(isURISync('foo'));
// => false;
const res = urlSanitizer.add('foo');
// => ['aaa', 'aaas', 'about', 'acap', ... 'foo', ...]
console.log(isURISync('foo'));
// => true;
Remove a scheme from the list of URI schemes.
scheme
string Scheme.Returns boolean Result.
true
if the scheme is successfully removed, false
otherwise.console.log(isURISync('aaa'));
// => true;
const res1 = urlSanitizer.remove('aaa');
// => true
console.log(isURISync('aaa'));
// => false;
const res2 = urlSanitizer.remove('foo');
// => false
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.