-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Validate hostname in uri #2846
Open
trizotti
wants to merge
16
commits into
hapijs:master
Choose a base branch
from
trizotti:validate-hostname-in-uri
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Validate hostname in uri #2846
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4234132
Add hostname option to domain obj
trizotti b23ce51
Add hostname validation + tests
trizotti f1f0322
Remove unnecessary import
trizotti 25777d5
Fix lint issues
trizotti 3cb8376
Add support for array of hostnames + tests
trizotti 9c83bd5
Merge branch 'master' into validate-hostname-in-uri
trizotti a90120b
Add support to mixed arrays rx and strings
trizotti 4d29bab
Remove unecessary operator
trizotti 2e754a3
Add docs for string.uri.hostname
trizotti 06483be
Refactorarray check
trizotti 4053a1b
Merge branch 'hapijs:master' into validate-hostname-in-uri
trizotti 5c2a000
Add test and clean up the code
trizotti 45c32ee
Fix typo
trizotti 1a9f9a5
Remove unnecessary test
trizotti 53de422
Refactoring
trizotti d74362c
Remove unnecessary type conversion
trizotti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -639,7 +639,7 @@ module.exports = Any.extend({ | |
Common.assertOptions(options, ['allowRelative', 'allowQuerySquareBrackets', 'domain', 'relativeOnly', 'scheme']); | ||
|
||
if (options.domain) { | ||
Common.assertOptions(options.domain, ['allowFullyQualified', 'allowUnicode', 'maxDomainSegments', 'minDomainSegments', 'tlds']); | ||
Common.assertOptions(options.domain, ['allowFullyQualified', 'allowUnicode', 'maxDomainSegments', 'minDomainSegments', 'tlds', 'hostname']); | ||
} | ||
|
||
const { regex, scheme } = Uri.regex(options); | ||
|
@@ -655,11 +655,40 @@ module.exports = Any.extend({ | |
const match = regex.exec(value); | ||
if (match) { | ||
const matched = match[1] || match[2]; | ||
if (domain && | ||
(!options.allowRelative || matched) && | ||
!Domain.isValid(matched, domain)) { | ||
|
||
return helpers.error('string.domain', { value: matched }); | ||
if (domain) { | ||
|
||
if ((!options.allowRelative || matched) && | ||
!Domain.isValid(matched, domain)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call to isValid should be avoided? @Marsup |
||
return helpers.error('string.domain', { value: matched }); | ||
} | ||
|
||
if (domain.hostname) { | ||
|
||
const isMatch = (matcher) => { | ||
|
||
if (matcher instanceof RegExp) { | ||
return matcher.test(matched); | ||
} | ||
|
||
return matcher === matched; | ||
|
||
}; | ||
|
||
if (Array.isArray(domain.hostname)) { | ||
|
||
if (!domain.hostname.some(isMatch)) { | ||
return helpers.error('string.uri.hostname', { value: matched, expected: 'any of the hostname items' }); | ||
} | ||
} | ||
else { | ||
|
||
if (!isMatch(domain.hostname)) { | ||
return helpers.error('string.uri.hostname', { value: matched, expected: domain.hostname }); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
return value; | ||
|
@@ -721,6 +750,7 @@ module.exports = Any.extend({ | |
'string.pattern.invert.name': '{{#label}} with value {:[.]} matches the inverted {{#name}} pattern', | ||
'string.trim': '{{#label}} must not have leading or trailing whitespace', | ||
'string.uri': '{{#label}} must be a valid uri', | ||
'string.uri.hostname': '{{#label}} does not match {{#expected}}', | ||
'string.uriCustomScheme': '{{#label}} must be a valid uri with a scheme matching the {{#scheme}} pattern', | ||
'string.uriRelativeOnly': '{{#label}} must be a valid relative uri', | ||
'string.uppercase': '{{#label}} must only contain uppercase characters' | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is going to be part of
domain
, you should probably exclude it from the call toisValid
to avoid any possible side-effect from an evolution of that module.