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

Conversation

brandon93s
Copy link

@brandon93s brandon93s commented Sep 1, 2021

I may be oversimplifying here, but this feels like a bug fix to not apply the protocol prefix when a custom prefix is present. Retains existing functionality and adds support for custom protocols. Closes #152.

@@ -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)?


// 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) {

@sindresorhus
Copy link
Owner

From the issue:

See #132 and the feedback given there.

Did you?

@sindresorhus sindresorhus changed the title Support Custom Protocol Add support for custom protocols Sep 7, 2021
@sindresorhus
Copy link
Owner

Friendly bump :)

Copy link

@dink42 dink42 left a comment

Choose a reason for hiding this comment

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

Nice thanks :)!

Copy link

@dink42 dink42 left a comment

Choose a reason for hiding this comment

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

Approve

@@ -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.

sindresorhus added a commit that referenced this pull request Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support custom protocols
4 participants