Add support for custom protocols#153
Closed
brandon93s wants to merge 1 commit intosindresorhus:mainfrom
Closed
Conversation
sindresorhus
requested changes
Sep 7, 2021
| }); | ||
|
|
||
| test('supports custom protocol', t => { | ||
| t.is(normalizeUrl('custom://sindresorhus.com'), 'custom://sindresorhus.com'); |
Owner
There was a problem hiding this comment.
Can you also add a test with custom:sindresorhus.com (no slashes)?
|
|
||
| // Prepend protocol | ||
| if (!isRelativeUrl) { | ||
| if (!isRelativeUrl & !hasCustomProtocol) { |
Owner
There was a problem hiding this comment.
Suggested change
| if (!isRelativeUrl & !hasCustomProtocol) { | |
| if (!isRelativeUrl && !hasCustomProtocol) { |
Owner
|
From the issue:
Did you? |
dink42
approved these changes
Sep 24, 2021
Owner
|
Friendly bump :) |
jsmarketingrus
approved these changes
Mar 14, 2022
dink42
approved these changes
Mar 14, 2022
sindresorhus
reviewed
Sep 1, 2022
|
|
||
| const hasRelativeProtocol = urlString.startsWith('//'); | ||
| const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString); | ||
| const hasCustomProtocol = /^[a-z.+-]*[.+-][a-z.+-]*:\/\//i.test(urlString); |
Owner
There was a problem hiding this comment.
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
Owner
There was a problem hiding this comment.
Also add a sensible length limit to prevent ReDoS abuse.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.