Skip to content

Commit

Permalink
Fix domain validation in relative uri. Closes #2316
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Mar 14, 2020
1 parent 1d1fd3f commit ed5990a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,11 @@ module.exports = Any.extend({

const match = regex.exec(value);
if (match) {
if (domain &&
!Domain.isValid(match[1], domain)) {

return helpers.error('string.domain', { value: match[1] });
if (domain) {
const matched = match[1] || match[2];
if (!Domain.isValid(matched, domain)) {
return helpers.error('string.domain', { value: matched });
}
}

return value;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"validation"
],
"dependencies": {
"@hapi/address": "^4.0.0",
"@hapi/address": "^4.0.1",
"@hapi/formula": "^2.0.0",
"@hapi/hoek": "^9.0.0",
"@hapi/pinpoint": "^2.0.0",
Expand Down
19 changes: 19 additions & 0 deletions test/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -8464,6 +8464,25 @@ describe('string', () => {
]);
});

it('validates relative only uri with domain', () => {

const schema = Joi.string().uri({ relativeOnly: true, domain: { minDomainSegments: 3, tlds: { allow: ['com'] } } });
Helper.validate(schema, [
['foo://example.com:8042/over/there?name=ferret#nose', false, '"value" must be a valid relative uri'],
['//test.example.com:8042/over/there?name=ferret#nose', true],
['//example.com:8042/over/there?name=ferret#nose', false, '"value" must contain a valid domain name']
]);
});

it('validates allowed relative uri with domain', () => {

const schema = Joi.string().uri({ allowRelative: true, domain: { minDomainSegments: 3, tlds: { allow: ['com'] } } });
Helper.validate(schema, [
['//test.example.com:8042/over/there?name=ferret#nose', true],
['//example.com:8042/over/there?name=ferret#nose', false, '"value" must contain a valid domain name']
]);
});

it('validates uri with square brackets allowed', () => {

const schema = Joi.string().uri({ allowQuerySquareBrackets: true });
Expand Down

0 comments on commit ed5990a

Please sign in to comment.