Skip to content

Commit

Permalink
fix(hostvalidator): support full urls for host (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Apr 30, 2020
1 parent 3ff6eb8 commit d0002e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/lockfile-lint-api/__tests__/validators.host.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ describe('Validator: Host', () => {
}
}

const validator = new ValidatorHost({packages: mockedPackages})
expect(validator.validate(['registry.verdaccio.org'])).toEqual({
type: 'success',
errors: []
})
})

it('validator should succeed if all resources are matching a host address but input is a full URL', () => {
const mockedPackages = {
'@babel/code-frame': {
resolved: 'https://registry.verdaccio.org/@babel/code-frame/-/code-frame-7.0.0.tgz'
},
meow: {
resolved: 'https://registry.verdaccio.org/meow/-/meow-4.0.1.tgz'
},
'@babel/generator': {
resolved: 'https://registry.verdaccio.org/@babel/generator/-/generator-7.4.4.tgz'
}
}

const validator = new ValidatorHost({packages: mockedPackages})
expect(validator.validate(['https://registry.verdaccio.org'])).toEqual({
type: 'success',
Expand Down
13 changes: 11 additions & 2 deletions packages/lockfile-lint-api/src/validators/ValidateHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ module.exports = class ValidateHost {

try {
const packageResolvedURL = new URL(packageMetadata.resolved)
const allowedHosts = hosts.map(hostValue => {
const allowedHosts = hosts.map(allowedHost => {
// eslint-disable-next-line security/detect-object-injection
return REGISTRY[hostValue] ? REGISTRY[hostValue] : hostValue
const host = REGISTRY[allowedHost] ? REGISTRY[allowedHost] : allowedHost
let hostValue
try {
const parsedHost = new URL(host)
hostValue = parsedHost.host
} catch (error) {
hostValue = host
}

return hostValue
})
const isPassing = allowedHosts.includes(packageResolvedURL.host)
if (!isPassing) {
Expand Down

0 comments on commit d0002e7

Please sign in to comment.