Skip to content

Commit

Permalink
feat(emptyhostname): allow empty hostnames in api
Browse files Browse the repository at this point in the history
BREAKING CHANGE: lockfile-lint-api internal method API has changed its function
signature to allow receiving a value, and then an options object in a second
argument.

Relevant issues:
- #23
- #25
  • Loading branch information
lirantal committed Nov 22, 2019
1 parent aa0474c commit 1e81165
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
41 changes: 41 additions & 0 deletions packages/lockfile-lint-api/__tests__/validators.host.test.js
Expand Up @@ -154,4 +154,45 @@ describe('Validator: Host', () => {

expect(() => validator.validate(['npm'])).toThrow(PackageError)
})

it('validator should not throw if emptyHostnames are allowed', () => {
const mockedPackages = {
'@babel/code-frame': {
resolved: 'github:XhmikosR/metalsmith-permalinks#master'
}
}
const validator = new ValidatorHost({packages: mockedPackages})

expect(
validator.validate(['npm'], {
emptyHostname: true
})
).toEqual({
type: 'success',
errors: []
})
})

it('validator should return errors if emptyHostnames are not allowed', () => {
const mockedPackages = {
'@babel/code-frame': {
resolved: 'github:XhmikosR/metalsmith-permalinks#master'
}
}
const validator = new ValidatorHost({packages: mockedPackages})

expect(
validator.validate(['npm'], {
emptyHostname: false
})
).toEqual({
type: 'error',
errors: [
{
message: `detected invalid host(s) for package: @babel/code-frame\n expected: registry.npmjs.org\n actual: \n`,
package: '@babel/code-frame'
}
]
})
})
})
20 changes: 12 additions & 8 deletions packages/lockfile-lint-api/src/validators/ValidateHost.js
@@ -1,6 +1,7 @@
'use strict'

const {URL} = require('url')
const debug = require('debug')('lockfile-lint-api')
const PackageError = require('../common/PackageError')
const {REGISTRY} = require('../common/constants')

Expand All @@ -13,7 +14,7 @@ module.exports = class ValidateHost {
this.packages = packages
}

validate (hosts) {
validate (hosts, options) {
if (!Array.isArray(hosts)) {
throw new Error('validate method requires an array')
}
Expand All @@ -37,13 +38,16 @@ module.exports = class ValidateHost {
})

if (allowedHosts.indexOf(packageResolvedURL.host) === -1) {
// throw new Error(`detected invalid origin for package: ${packageName}`)
validationResult.errors.push({
message: `detected invalid host(s) for package: ${packageName}\n expected: ${allowedHosts}\n actual: ${
packageResolvedURL.host
}\n`,
package: packageName
})
if (!packageResolvedURL.host && options && options.emptyHostname) {
debug(`detected empty hostname but allowing because emptyHostname is not false`)
} else {
validationResult.errors.push({
message: `detected invalid host(s) for package: ${packageName}\n expected: ${allowedHosts}\n actual: ${
packageResolvedURL.host
}\n`,
package: packageName
})
}
}
}

Expand Down

0 comments on commit 1e81165

Please sign in to comment.