Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom protocols #153

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ export default function normalizeUrl(urlString, options) {

const hasRelativeProtocol = urlString.startsWith('//');
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
const hasCustomProtocol = /^[a-z.+-]*[.+-][a-z.+-]*:\/\//i.test(urlString);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const hasCustomProtocol = /^[a-z.+-]*[.+-][a-z.+-]*:\/\//i.test(urlString);
const hasCustomProtocol = /^[a-z][a-z.+-]*?[.+-][a-z.+-]*?:/i.test(urlString);

a protocol does not need // and it has to start with a-z: https://stackoverflow.com/questions/3641722/valid-characters-for-uri-schemes

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a sensible length limit to prevent ReDoS abuse.


// Prepend protocol
if (!isRelativeUrl) {
if (!isRelativeUrl & !hasCustomProtocol) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isRelativeUrl & !hasCustomProtocol) {
if (!isRelativeUrl && !hasCustomProtocol) {

urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
}

Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,10 @@ test('does not have exponential performance for data URLs', t => {
t.true(difference < 100, `Execution time: ${difference}`);
}
});

test('supports custom protocol', t => {
t.is(normalizeUrl('custom://sindresorhus.com'), 'custom://sindresorhus.com');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test with custom:sindresorhus.com (no slashes)?

t.is(normalizeUrl('custom+with+plusses://sindresorhus.com'), 'custom+with+plusses://sindresorhus.com');
t.is(normalizeUrl('custom.with.periods://sindresorhus.com'), 'custom.with.periods://sindresorhus.com');
t.is(normalizeUrl('custom-with-hyphens://sindresorhus.com'), 'custom-with-hyphens://sindresorhus.com');
});