npq 3.20.0 → 3.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -3
- package/bin/npq-hero.js +28 -14
- package/bin/npq.js +25 -8
- package/lib/cli.js +20 -3
- package/lib/helpers/notEvaluated.js +11 -0
- package/lib/helpers/npmRegistry.js +3 -66
- package/lib/helpers/packageRepoUtils.js +11 -21
- package/lib/helpers/registryClient.js +206 -0
- package/lib/helpers/registryConfig.js +79 -0
- package/lib/helpers/registryErrors.js +32 -0
- package/lib/helpers/reportResults.js +46 -3
- package/lib/marshall.js +12 -3
- package/lib/marshalls/baseMarshall.js +27 -7
- package/lib/marshalls/expiredDomains.marshall.js +9 -0
- package/lib/marshalls/index.js +9 -2
- package/lib/marshalls/provenance.marshall.js +72 -105
- package/lib/marshalls/signatures.marshall.js +36 -75
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -78,6 +78,16 @@ brew install npq
|
|
|
78
78
|
npq install express
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
+
### Custom registries
|
|
82
|
+
|
|
83
|
+
NPQ reads standard npm registry configuration, including default and scoped registries, authentication, proxies, and TLS settings. You can also select a registry on the command line; the option is shared with the package manager used for installation:
|
|
84
|
+
|
|
85
|
+
```sh
|
|
86
|
+
npq install @company/tool --registry=https://artifactory.example.com/artifactory/api/npm/npm-virtual/
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
See [Custom registries](docs/feature/custom-registry.md) for `.npmrc` examples, configuration precedence, private CA setup, skipped capability behavior, and NPQ's no-fallback privacy guarantee.
|
|
90
|
+
|
|
81
91
|
### Embed in your day to day
|
|
82
92
|
|
|
83
93
|
Since `npq` is a pre-step to ensure that the npm package you're installing is safe, you can safely embed it in your day-to-day `npm` usage so there's no need to remember to run `npq` explicitly.
|
|
@@ -130,15 +140,15 @@ Note: `npq` by default will offload all commands and their arguments to the `npm
|
|
|
130
140
|
| --- | --- | ---
|
|
131
141
|
| age | Will show a warning for a package if its age on npm is less than 22 days | Checks a package creation date, not a specific version
|
|
132
142
|
| author | Validates the resolved version’s publisher (`_npmUser`), flags a **new** maintainer on the package (first publish by that email within 21 days), **dormant maintainer** gaps (warning if over ~6 months since their last publish on that package, error if over ~9 months), and very **recent** publishes | See [docs/feature/author-marshall.md](docs/feature/author-marshall.md)
|
|
133
|
-
| downloads | Will show a warning for a package if its download count in the last month is less than 20
|
|
143
|
+
| downloads | Will show a warning for a package if its download count in the last month is less than 20 | Reports `not evaluated` for custom-registry packages instead of querying the public npm downloads service
|
|
134
144
|
| readme | Will show a warning if a package has no README or it has been detected as a security placeholder package by npm staff
|
|
135
145
|
| repo | Will show a warning if a package has been found without a valid and working repository URL | Checks the latest version for a repository URL
|
|
136
146
|
| scripts | Will show a warning if a package has a pre/post install script which could potentially be malicious
|
|
137
147
|
| snyk | Will show a warning if a package has been found with vulnerabilities in Snyk's database | For Snyk to work you need to either have the `snyk` npm package installed with a valid API token, or make the token available in the `SNYK_TOKEN` environment variable, and npq will use it
|
|
138
148
|
| license | Will show a warning if a package has been found without a license field | Checks the latest version for a license
|
|
139
149
|
| expired domains | Will show a warning if a package has been found with one of its maintainers having an email address that includes an expired domain | Checks a dependency version for a maintainer with an expired domain
|
|
140
|
-
| signatures | Will compare the package's signature as it shows on the registry's
|
|
141
|
-
| provenance | Will verify the package's attestations of provenance metadata for the published package, and **error** on [provenance regression](docs/feature/provenance.md) (an older semver had registry provenance metadata but the version you install does not)
|
|
150
|
+
| signatures | Will compare the package's signature as it shows on the registry's packument with signing keys published by the selected registry | Reports `not evaluated` if the selected registry does not expose signing keys
|
|
151
|
+
| provenance | Will verify the package's attestations of provenance metadata for the published package, and **error** on [provenance regression](docs/feature/provenance.md) (an older semver had registry provenance metadata but the version you install does not) | Reports `not evaluated` if the selected registry does not expose signing keys or attestations
|
|
142
152
|
| version-maturity | Will show a warning if the specific version being installed was published less than 7 days ago | Helps identify recently published versions that may not have been reviewed by the community yet
|
|
143
153
|
| newBin | Will show a warning if the package version being installed introduces a new command-line binary (via the `bin` field in `package.json`) that was not present in its previous version. | Helps identify potentially unexpected new executables being added to your `node_modules/.bin/` directory.
|
|
144
154
|
| typosquatting | Will show a warning if the package name is similar to a popular package name, which could indicate a potential typosquatting attack. | Helps identify packages that may be trying to trick users into installing them by mimicking popular package names.
|
package/bin/npq-hero.js
CHANGED
|
@@ -12,6 +12,8 @@ const cliPrompt = require('../lib/helpers/cliPrompt.js')
|
|
|
12
12
|
const { reportResults } = require('../lib/helpers/reportResults')
|
|
13
13
|
const { Spinner } = require('../lib/helpers/cliSpinner')
|
|
14
14
|
const { promiseThrottleHelper } = require('../lib/helpers/promiseThrottler')
|
|
15
|
+
const RegistryConfig = require('../lib/helpers/registryConfig')
|
|
16
|
+
const RegistryClient = require('../lib/helpers/registryClient')
|
|
15
17
|
|
|
16
18
|
const PACKAGE_MANAGER_TOOL = process.env.NPQ_PKG_MGR
|
|
17
19
|
const DISABLE_AUTO_CONTINUE = process.env.NPQ_DISABLE_AUTO_CONTINUE === 'true'
|
|
@@ -27,14 +29,17 @@ if (spinner) {
|
|
|
27
29
|
spinner.start()
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
RegistryConfig.load({ argv: cliArgs.registryConfigArgs || [] })
|
|
33
|
+
.then((registryConfig) => {
|
|
34
|
+
const registryClient = new RegistryClient(registryConfig)
|
|
35
|
+
const marshall = new Marshall({
|
|
36
|
+
pkgs: cliArgs.packages,
|
|
37
|
+
registryClient,
|
|
38
|
+
progressManager: spinner,
|
|
39
|
+
promiseThrottleHelper
|
|
40
|
+
})
|
|
41
|
+
return marshall.process()
|
|
42
|
+
})
|
|
38
43
|
.then((marshallResults) => {
|
|
39
44
|
if (spinner) {
|
|
40
45
|
spinner.stop()
|
|
@@ -46,12 +51,20 @@ marshall
|
|
|
46
51
|
Object.hasOwn(results, 'countErrors') &&
|
|
47
52
|
Object.hasOwn(results, 'countWarnings')
|
|
48
53
|
) {
|
|
49
|
-
const {
|
|
50
|
-
|
|
54
|
+
const {
|
|
55
|
+
countErrors,
|
|
56
|
+
countWarnings,
|
|
57
|
+
countNotEvaluated = 0,
|
|
58
|
+
useRichFormatting
|
|
59
|
+
} = results
|
|
60
|
+
const hasFindings = countErrors > 0 || countWarnings > 0
|
|
61
|
+
const hasReportableResults = hasFindings || countNotEvaluated > 0
|
|
51
62
|
|
|
52
|
-
if (
|
|
63
|
+
if (hasReportableResults) {
|
|
53
64
|
console.log()
|
|
54
|
-
console.log(
|
|
65
|
+
console.log(
|
|
66
|
+
hasFindings ? 'Packages with issues found:' : 'Package checks not evaluated:'
|
|
67
|
+
)
|
|
55
68
|
|
|
56
69
|
if (useRichFormatting) {
|
|
57
70
|
console.log(results.resultsForPrettyPrint)
|
|
@@ -63,9 +76,10 @@ marshall
|
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
return {
|
|
66
|
-
anyIssues:
|
|
79
|
+
anyIssues: hasFindings,
|
|
67
80
|
countErrors,
|
|
68
|
-
countWarnings
|
|
81
|
+
countWarnings,
|
|
82
|
+
countNotEvaluated
|
|
69
83
|
}
|
|
70
84
|
}
|
|
71
85
|
return undefined
|
package/bin/npq.js
CHANGED
|
@@ -15,6 +15,8 @@ const cliPrompt = require('../lib/helpers/cliPrompt.js')
|
|
|
15
15
|
const { reportResults } = require('../lib/helpers/reportResults')
|
|
16
16
|
const { Spinner } = require('../lib/helpers/cliSpinner')
|
|
17
17
|
const { promiseThrottleHelper } = require('../lib/helpers/promiseThrottler')
|
|
18
|
+
const RegistryConfig = require('../lib/helpers/registryConfig')
|
|
19
|
+
const RegistryClient = require('../lib/helpers/registryClient')
|
|
18
20
|
|
|
19
21
|
const debug = util.debuglog('npq')
|
|
20
22
|
|
|
@@ -27,7 +29,12 @@ if (spinner) {
|
|
|
27
29
|
spinner.start()
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
let registryClient
|
|
33
|
+
|
|
34
|
+
RegistryConfig.load({ argv: cliArgs.registryConfigArgs || [] })
|
|
35
|
+
.then((registryConfig) => {
|
|
36
|
+
registryClient = new RegistryClient(registryConfig)
|
|
37
|
+
})
|
|
31
38
|
.then(() => {
|
|
32
39
|
if (cliArgs.packages.length === 0) {
|
|
33
40
|
debug('\nNo packages specified, using project packages from package.json')
|
|
@@ -48,6 +55,7 @@ Promise.resolve()
|
|
|
48
55
|
|
|
49
56
|
const marshall = new Marshall({
|
|
50
57
|
pkgs: packages,
|
|
58
|
+
registryClient,
|
|
51
59
|
progressManager: spinner,
|
|
52
60
|
promiseThrottleHelper
|
|
53
61
|
})
|
|
@@ -65,12 +73,20 @@ Promise.resolve()
|
|
|
65
73
|
Object.hasOwn(results, 'countErrors') &&
|
|
66
74
|
Object.hasOwn(results, 'countWarnings')
|
|
67
75
|
) {
|
|
68
|
-
const {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
const {
|
|
77
|
+
countErrors,
|
|
78
|
+
countWarnings,
|
|
79
|
+
countNotEvaluated = 0,
|
|
80
|
+
useRichFormatting
|
|
81
|
+
} = results
|
|
82
|
+
const hasFindings = countErrors > 0 || countWarnings > 0
|
|
83
|
+
const hasReportableResults = hasFindings || countNotEvaluated > 0
|
|
84
|
+
|
|
85
|
+
if (hasReportableResults) {
|
|
72
86
|
console.log()
|
|
73
|
-
console.log(
|
|
87
|
+
console.log(
|
|
88
|
+
hasFindings ? 'Packages with issues found:' : 'Package checks not evaluated:'
|
|
89
|
+
)
|
|
74
90
|
|
|
75
91
|
if (useRichFormatting) {
|
|
76
92
|
console.log(results.resultsForPrettyPrint)
|
|
@@ -82,9 +98,10 @@ Promise.resolve()
|
|
|
82
98
|
}
|
|
83
99
|
|
|
84
100
|
return {
|
|
85
|
-
anyIssues:
|
|
101
|
+
anyIssues: hasFindings,
|
|
86
102
|
countErrors,
|
|
87
|
-
countWarnings
|
|
103
|
+
countWarnings,
|
|
104
|
+
countNotEvaluated
|
|
88
105
|
}
|
|
89
106
|
}
|
|
90
107
|
return undefined
|
package/lib/cli.js
CHANGED
|
@@ -20,6 +20,12 @@ const INSTALL_SUBCOMMANDS = new Set([
|
|
|
20
20
|
])
|
|
21
21
|
|
|
22
22
|
class CliParser {
|
|
23
|
+
static registryConfigArgs(values = {}) {
|
|
24
|
+
return ['registry', 'userconfig', 'globalconfig'].flatMap((key) =>
|
|
25
|
+
values[key] ? [`--${key}=${values[key]}`] : []
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
static isInstallSubcommand(token) {
|
|
24
30
|
return INSTALL_SUBCOMMANDS.has(token)
|
|
25
31
|
}
|
|
@@ -67,6 +73,9 @@ class CliParser {
|
|
|
67
73
|
packageManager: { type: 'string' },
|
|
68
74
|
pkgMgr: { type: 'string' },
|
|
69
75
|
'disable-auto-continue': { type: 'boolean' },
|
|
76
|
+
registry: { type: 'string' },
|
|
77
|
+
userconfig: { type: 'string' },
|
|
78
|
+
globalconfig: { type: 'string' },
|
|
70
79
|
help: { type: 'boolean', short: 'h' },
|
|
71
80
|
version: { type: 'boolean', short: 'v' }
|
|
72
81
|
}
|
|
@@ -95,6 +104,9 @@ Options:
|
|
|
95
104
|
--packageManager Package Manager to use (default: npm)
|
|
96
105
|
--pkgMgr Alias for packageManager
|
|
97
106
|
--disable-auto-continue Disable auto-continue countdown, always prompt
|
|
107
|
+
--registry npm registry used for audit and install
|
|
108
|
+
--userconfig Path to the user npm configuration file
|
|
109
|
+
--globalconfig Path to the global npm configuration file
|
|
98
110
|
-h, --help Show help
|
|
99
111
|
-v, --version Show version
|
|
100
112
|
|
|
@@ -127,6 +139,7 @@ curated by Liran Tal at https://github.com/lirantal/npq`)
|
|
|
127
139
|
plain: values.plain || false,
|
|
128
140
|
disableAutoContinue:
|
|
129
141
|
values['disable-auto-continue'] || process.env.NPQ_DISABLE_AUTO_CONTINUE === 'true',
|
|
142
|
+
registryConfigArgs: this.registryConfigArgs(values),
|
|
130
143
|
installSubcommandExplicit
|
|
131
144
|
}
|
|
132
145
|
}
|
|
@@ -140,17 +153,21 @@ curated by Liran Tal at https://github.com/lirantal/npq`)
|
|
|
140
153
|
type: 'string',
|
|
141
154
|
short: 'i',
|
|
142
155
|
default: 'install'
|
|
143
|
-
}
|
|
156
|
+
},
|
|
157
|
+
registry: { type: 'string' },
|
|
158
|
+
userconfig: { type: 'string' },
|
|
159
|
+
globalconfig: { type: 'string' }
|
|
144
160
|
}
|
|
145
161
|
}
|
|
146
162
|
|
|
147
|
-
const { positionals } = parseArgs(config)
|
|
163
|
+
const { values, positionals } = parseArgs(config)
|
|
148
164
|
|
|
149
165
|
const earlyExitNoInstall = true
|
|
150
166
|
const normalizedPackages = this._extractPackagesFromPositionals(positionals, earlyExitNoInstall)
|
|
151
167
|
|
|
152
168
|
return {
|
|
153
|
-
packages: normalizedPackages
|
|
169
|
+
packages: normalizedPackages,
|
|
170
|
+
registryConfigArgs: this.registryConfigArgs(values)
|
|
154
171
|
}
|
|
155
172
|
}
|
|
156
173
|
}
|
|
@@ -13,57 +13,6 @@ const MISSING_TIME_CUTOFF = '2015-01-01T00:00:00.000Z'
|
|
|
13
13
|
* This replaces pacote functionality for npq specific needs
|
|
14
14
|
*/
|
|
15
15
|
class NpmRegistry {
|
|
16
|
-
constructor(opts = {}) {
|
|
17
|
-
this.opts = opts
|
|
18
|
-
this.registry = opts.registry || 'https://registry.npmjs.org'
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Fetch package manifest from npm registry
|
|
23
|
-
* @param {string} packageSpec - Package name and version (e.g., 'express@4.18.2')
|
|
24
|
-
* @param {Object} options - Additional options
|
|
25
|
-
* @returns {Promise<Object>} Package manifest
|
|
26
|
-
*/
|
|
27
|
-
async getManifest(packageSpec, options = {}) {
|
|
28
|
-
const spec = npa(packageSpec)
|
|
29
|
-
const escapedName = spec.escapedName
|
|
30
|
-
const packumentUrl = `${this.registry}/${escapedName}`
|
|
31
|
-
|
|
32
|
-
// Fetch packument (package metadata)
|
|
33
|
-
const response = await fetch(packumentUrl, {
|
|
34
|
-
headers: {
|
|
35
|
-
accept: 'application/json',
|
|
36
|
-
'user-agent': 'npq-npm-registry-client',
|
|
37
|
-
...(options.headers || {})
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
if (!response.ok) {
|
|
42
|
-
throw new Error(`Failed to fetch package manifest: ${response.status} ${response.statusText}`)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const packument = await response.json()
|
|
46
|
-
|
|
47
|
-
// Pick the specific version
|
|
48
|
-
let version = spec.fetchSpec
|
|
49
|
-
if (version === 'latest' || !version || version === '*') {
|
|
50
|
-
version = packument['dist-tags']?.latest
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (!packument.versions || !packument.versions[version]) {
|
|
54
|
-
throw new Error(`Version ${version} not found for package ${spec.name}`)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const manifest = packument.versions[version]
|
|
58
|
-
|
|
59
|
-
// Add timing information if available
|
|
60
|
-
if (packument.time && packument.time[version]) {
|
|
61
|
-
manifest._time = packument.time[version]
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return manifest
|
|
65
|
-
}
|
|
66
|
-
|
|
67
16
|
/**
|
|
68
17
|
* Verify package signatures using npm registry public keys
|
|
69
18
|
* @param {Object} manifest - Package manifest
|
|
@@ -140,27 +89,15 @@ class NpmRegistry {
|
|
|
140
89
|
* @param {Array} registryKeys - Public keys from npm registry
|
|
141
90
|
* @returns {Promise<Object>} Verified manifest with _attestations
|
|
142
91
|
*/
|
|
143
|
-
async verifyAttestations(manifest, registryKeys) {
|
|
92
|
+
async verifyAttestations(manifest, registryKeys, attestations) {
|
|
144
93
|
if (!manifest.dist || !manifest.dist.attestations) {
|
|
145
94
|
throw new Error('Package has no attestations to verify')
|
|
146
95
|
}
|
|
147
96
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const response = await fetch(attestationsUrl, {
|
|
152
|
-
headers: {
|
|
153
|
-
accept: 'application/json',
|
|
154
|
-
'user-agent': 'npq-npm-registry-client'
|
|
155
|
-
}
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
if (!response.ok) {
|
|
159
|
-
throw new Error(`Failed to fetch attestations: ${response.status} ${response.statusText}`)
|
|
97
|
+
if (!Array.isArray(attestations)) {
|
|
98
|
+
throw new Error('Package attestations response is invalid')
|
|
160
99
|
}
|
|
161
100
|
|
|
162
|
-
const { attestations } = await response.json()
|
|
163
|
-
|
|
164
101
|
const bundles = attestations.map(({ predicateType, bundle }) => {
|
|
165
102
|
const statement = JSON.parse(
|
|
166
103
|
Buffer.from(bundle.dsseEnvelope.payload, 'base64').toString('utf8')
|
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const semver = require('semver')
|
|
4
|
-
const
|
|
5
|
-
const NPM_REGISTRY_API = 'https://api.npmjs.org'
|
|
4
|
+
const RegistryClient = require('./registryClient')
|
|
6
5
|
|
|
7
6
|
class PackageRepoUtils {
|
|
8
|
-
constructor(
|
|
9
|
-
this.
|
|
10
|
-
this.registryApiUrl = options.registryApiUrl ? options.registryApiUrl : NPM_REGISTRY_API
|
|
7
|
+
constructor({ registryClient = RegistryClient.public() } = {}) {
|
|
8
|
+
this.registryClient = registryClient
|
|
11
9
|
this.pkgInfoCache = {}
|
|
12
10
|
}
|
|
13
11
|
|
|
14
|
-
formatPackageForUrl(pkg) {
|
|
15
|
-
return pkg.replace(/\//g, '%2F')
|
|
16
|
-
}
|
|
17
|
-
|
|
18
12
|
getPackageInfo(pkg) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return fetch(`${this.registryUrl}/${this.formatPackageForUrl(pkg)}`)
|
|
23
|
-
.then((response) => response.json())
|
|
24
|
-
.then((data) => {
|
|
25
|
-
this.pkgInfoCache[pkg] = data
|
|
26
|
-
return data
|
|
27
|
-
})
|
|
13
|
+
const cacheKey = `${this.registryClient.registryFor(pkg)}|${pkg}`
|
|
14
|
+
if (this.pkgInfoCache[cacheKey]) {
|
|
15
|
+
return Promise.resolve(this.pkgInfoCache[cacheKey])
|
|
28
16
|
}
|
|
17
|
+
return this.registryClient.getPackageInfo(pkg).then((data) => {
|
|
18
|
+
this.pkgInfoCache[cacheKey] = data
|
|
19
|
+
return data
|
|
20
|
+
})
|
|
29
21
|
}
|
|
30
22
|
|
|
31
23
|
getLatestVersion(pkg) {
|
|
@@ -35,9 +27,7 @@ class PackageRepoUtils {
|
|
|
35
27
|
}
|
|
36
28
|
|
|
37
29
|
getDownloadInfo(pkg) {
|
|
38
|
-
return
|
|
39
|
-
.then((response) => response.json())
|
|
40
|
-
.then(({ downloads }) => downloads)
|
|
30
|
+
return this.registryClient.getDownloadInfo(pkg)
|
|
41
31
|
}
|
|
42
32
|
|
|
43
33
|
getReadmeInfo(pkg) {
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const npa = require('npm-package-arg')
|
|
4
|
+
const npmFetch = require('npm-registry-fetch')
|
|
5
|
+
const NotEvaluated = require('./notEvaluated')
|
|
6
|
+
const RegistryConfig = require('./registryConfig')
|
|
7
|
+
const { RegistryError } = require('./registryErrors')
|
|
8
|
+
|
|
9
|
+
const PUBLIC_REGISTRY = 'https://registry.npmjs.org/'
|
|
10
|
+
const OPTIONAL_STATUS = new Set([404, 405, 501])
|
|
11
|
+
const CAPABILITY = Object.freeze({
|
|
12
|
+
SIGNING_KEYS: 'signing-keys',
|
|
13
|
+
ATTESTATIONS: 'attestations',
|
|
14
|
+
DOWNLOAD_COUNTS: 'download-counts'
|
|
15
|
+
})
|
|
16
|
+
const CAPABILITY_MESSAGE = Object.freeze({
|
|
17
|
+
[CAPABILITY.SIGNING_KEYS]: 'configured registry does not expose signing keys',
|
|
18
|
+
[CAPABILITY.ATTESTATIONS]: 'configured registry does not expose attestations',
|
|
19
|
+
[CAPABILITY.DOWNLOAD_COUNTS]:
|
|
20
|
+
'download counts are available only for the public npm registry'
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
class RegistryClient {
|
|
24
|
+
constructor(registryConfig, { fetcher = npmFetch } = {}) {
|
|
25
|
+
this.registryConfig = registryConfig
|
|
26
|
+
this.fetcher = fetcher
|
|
27
|
+
this.keyCache = new Map()
|
|
28
|
+
this.capabilityCache = new Map()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static public() {
|
|
32
|
+
return new RegistryClient(RegistryConfig.defaults())
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
registryFor(packageSpec) {
|
|
36
|
+
return this.registryConfig.registryFor(packageSpec)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
requestOptions(packageSpec) {
|
|
40
|
+
return {
|
|
41
|
+
...this.registryConfig.requestOptions,
|
|
42
|
+
spec: packageSpec,
|
|
43
|
+
headers: {
|
|
44
|
+
accept: 'application/json',
|
|
45
|
+
'user-agent': 'npq-npm-registry-client'
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async requestJson(
|
|
51
|
+
requestPath,
|
|
52
|
+
packageSpec,
|
|
53
|
+
{ capability = null, notFoundAsData = false } = {}
|
|
54
|
+
) {
|
|
55
|
+
const registry = this.registryFor(packageSpec)
|
|
56
|
+
try {
|
|
57
|
+
return await this.fetcher.json(requestPath, this.requestOptions(packageSpec))
|
|
58
|
+
} catch (error) {
|
|
59
|
+
const statusCode = error.statusCode || error.status || null
|
|
60
|
+
if (notFoundAsData && statusCode === 404) {
|
|
61
|
+
return { error: 'Not found' }
|
|
62
|
+
}
|
|
63
|
+
if (capability && OPTIONAL_STATUS.has(statusCode)) {
|
|
64
|
+
this.capabilityCache.set(`${registry}|${capability}`, false)
|
|
65
|
+
throw new NotEvaluated(CAPABILITY_MESSAGE[capability], { capability })
|
|
66
|
+
}
|
|
67
|
+
const code =
|
|
68
|
+
statusCode === 401 || statusCode === 403
|
|
69
|
+
? 'EREGISTRYAUTH'
|
|
70
|
+
: statusCode
|
|
71
|
+
? 'EREGISTRYHTTP'
|
|
72
|
+
: 'EREGISTRYNETWORK'
|
|
73
|
+
throw new RegistryError(
|
|
74
|
+
statusCode === 401 || statusCode === 403
|
|
75
|
+
? 'Registry authentication or authorization failed'
|
|
76
|
+
: statusCode
|
|
77
|
+
? `Registry request failed with HTTP ${statusCode}`
|
|
78
|
+
: 'Registry network request failed',
|
|
79
|
+
{ registry, code, statusCode, cause: error }
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async getPackageInfo(packageName) {
|
|
85
|
+
const spec = npa(packageName)
|
|
86
|
+
const data = await this.requestJson(spec.escapedName, packageName, {
|
|
87
|
+
notFoundAsData: true
|
|
88
|
+
})
|
|
89
|
+
if (data && data.error === 'Not found') {
|
|
90
|
+
return data
|
|
91
|
+
}
|
|
92
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
93
|
+
throw new RegistryError('Registry package metadata is malformed', {
|
|
94
|
+
registry: this.registryFor(packageName),
|
|
95
|
+
code: 'EREGISTRYPROTOCOL'
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
return data
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async getManifest(packageSpec, packument = null) {
|
|
102
|
+
const spec = npa(packageSpec)
|
|
103
|
+
const data = packument || (await this.getPackageInfo(spec.name))
|
|
104
|
+
let version = spec.fetchSpec
|
|
105
|
+
if (!version || version === '*' || version === 'latest') {
|
|
106
|
+
version = data['dist-tags'] && data['dist-tags'].latest
|
|
107
|
+
}
|
|
108
|
+
if (!data.versions || !data.versions[version]) {
|
|
109
|
+
throw new Error(`Version ${version} not found for package ${spec.name}`)
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
...data.versions[version],
|
|
113
|
+
...(data.time && data.time[version] ? { _time: data.time[version] } : {})
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
unavailable(capability) {
|
|
118
|
+
throw new NotEvaluated(CAPABILITY_MESSAGE[capability], { capability })
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async getRegistryKeys(packageSpec) {
|
|
122
|
+
const registry = this.registryFor(packageSpec)
|
|
123
|
+
const cacheKey = `${registry}|${CAPABILITY.SIGNING_KEYS}`
|
|
124
|
+
if (this.keyCache.has(cacheKey)) {
|
|
125
|
+
return this.keyCache.get(cacheKey)
|
|
126
|
+
}
|
|
127
|
+
if (this.capabilityCache.get(cacheKey) === false) {
|
|
128
|
+
return this.unavailable(CAPABILITY.SIGNING_KEYS)
|
|
129
|
+
}
|
|
130
|
+
const response = await this.requestJson('-/npm/v1/keys', packageSpec, {
|
|
131
|
+
capability: CAPABILITY.SIGNING_KEYS
|
|
132
|
+
})
|
|
133
|
+
if (!response || !Array.isArray(response.keys)) {
|
|
134
|
+
throw new RegistryError('Registry signing-key response is malformed', {
|
|
135
|
+
registry,
|
|
136
|
+
code: 'EREGISTRYPROTOCOL'
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
if (response.keys.length === 0) {
|
|
140
|
+
this.capabilityCache.set(cacheKey, false)
|
|
141
|
+
return this.unavailable(CAPABILITY.SIGNING_KEYS)
|
|
142
|
+
}
|
|
143
|
+
const keys = response.keys.map((key) => ({
|
|
144
|
+
...key,
|
|
145
|
+
pemkey: `-----BEGIN PUBLIC KEY-----\n${key.key}\n-----END PUBLIC KEY-----`
|
|
146
|
+
}))
|
|
147
|
+
this.keyCache.set(cacheKey, keys)
|
|
148
|
+
return keys
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getAttestations(packageSpec, manifest) {
|
|
152
|
+
const registry = this.registryFor(packageSpec)
|
|
153
|
+
const cacheKey = `${registry}|${CAPABILITY.ATTESTATIONS}`
|
|
154
|
+
if (this.capabilityCache.get(cacheKey) === false) {
|
|
155
|
+
return this.unavailable(CAPABILITY.ATTESTATIONS)
|
|
156
|
+
}
|
|
157
|
+
let requestPath
|
|
158
|
+
try {
|
|
159
|
+
const advertisedPath = new URL(manifest.dist.attestations.url).pathname
|
|
160
|
+
const registryPath = new URL(registry).pathname
|
|
161
|
+
requestPath = advertisedPath.startsWith(registryPath)
|
|
162
|
+
? advertisedPath.slice(registryPath.length)
|
|
163
|
+
: advertisedPath.replace(/^\/+/, '')
|
|
164
|
+
} catch (error) {
|
|
165
|
+
throw new RegistryError('Package attestation URL is malformed', {
|
|
166
|
+
registry,
|
|
167
|
+
code: 'EREGISTRYPROTOCOL',
|
|
168
|
+
cause: error
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
const response = await this.requestJson(requestPath, packageSpec, {
|
|
172
|
+
capability: CAPABILITY.ATTESTATIONS
|
|
173
|
+
})
|
|
174
|
+
if (!response || !Array.isArray(response.attestations)) {
|
|
175
|
+
throw new RegistryError('Registry attestation response is malformed', {
|
|
176
|
+
registry,
|
|
177
|
+
code: 'EREGISTRYPROTOCOL'
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
if (response.attestations.length === 0) {
|
|
181
|
+
this.capabilityCache.set(cacheKey, false)
|
|
182
|
+
return this.unavailable(CAPABILITY.ATTESTATIONS)
|
|
183
|
+
}
|
|
184
|
+
return response.attestations
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async getDownloadInfo(packageName) {
|
|
188
|
+
if (this.registryFor(packageName) !== PUBLIC_REGISTRY) {
|
|
189
|
+
return this.unavailable(CAPABILITY.DOWNLOAD_COUNTS)
|
|
190
|
+
}
|
|
191
|
+
const escapedName = npa(packageName).escapedName
|
|
192
|
+
const response = await this.requestJson(
|
|
193
|
+
`https://api.npmjs.org/downloads/point/last-month/${escapedName}`,
|
|
194
|
+
packageName
|
|
195
|
+
)
|
|
196
|
+
if (!response || typeof response.downloads !== 'number') {
|
|
197
|
+
throw new RegistryError('Registry download response is malformed', {
|
|
198
|
+
registry: PUBLIC_REGISTRY,
|
|
199
|
+
code: 'EREGISTRYPROTOCOL'
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
return response.downloads
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = RegistryClient
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const path = require('node:path')
|
|
4
|
+
const Config = require('@npmcli/config')
|
|
5
|
+
const npmFetch = require('npm-registry-fetch')
|
|
6
|
+
const {
|
|
7
|
+
definitions,
|
|
8
|
+
flatten,
|
|
9
|
+
nerfDarts,
|
|
10
|
+
shorthands
|
|
11
|
+
} = require('@npmcli/config/lib/definitions')
|
|
12
|
+
const { RegistryError, sanitizeRegistryUrl } = require('./registryErrors')
|
|
13
|
+
|
|
14
|
+
const DEFAULT_REGISTRY = 'https://registry.npmjs.org/'
|
|
15
|
+
|
|
16
|
+
function normalizeRegistry(value) {
|
|
17
|
+
const url = new URL(value || DEFAULT_REGISTRY)
|
|
18
|
+
if (!url.pathname.endsWith('/')) {
|
|
19
|
+
url.pathname += '/'
|
|
20
|
+
}
|
|
21
|
+
return url.toString()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class RegistryConfig {
|
|
25
|
+
constructor(requestOptions) {
|
|
26
|
+
this.requestOptions = Object.freeze({
|
|
27
|
+
...requestOptions,
|
|
28
|
+
registry: normalizeRegistry(requestOptions.registry)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static defaults() {
|
|
33
|
+
return new RegistryConfig({ registry: DEFAULT_REGISTRY })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static async load({ argv = [], env = process.env, cwd = process.cwd() } = {}) {
|
|
37
|
+
try {
|
|
38
|
+
const registryArg = argv.find((arg) => arg.startsWith('--registry='))
|
|
39
|
+
if (registryArg) {
|
|
40
|
+
normalizeRegistry(registryArg.slice('--registry='.length))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const config = new Config({
|
|
44
|
+
npmPath: path.resolve(__dirname, '..'),
|
|
45
|
+
definitions,
|
|
46
|
+
shorthands,
|
|
47
|
+
flatten,
|
|
48
|
+
nerfDarts,
|
|
49
|
+
argv: ['node', 'npq', ...argv],
|
|
50
|
+
env,
|
|
51
|
+
cwd,
|
|
52
|
+
warn: false
|
|
53
|
+
})
|
|
54
|
+
await config.load()
|
|
55
|
+
if (!config.validate()) {
|
|
56
|
+
throw new Error('Invalid npm configuration')
|
|
57
|
+
}
|
|
58
|
+
return new RegistryConfig(config.flat)
|
|
59
|
+
} catch (error) {
|
|
60
|
+
const code =
|
|
61
|
+
error.code === 'ERR_INVALID_AUTH' ? 'EREGISTRYCONFIGAUTH' : 'EREGISTRYCONFIG'
|
|
62
|
+
const message =
|
|
63
|
+
error.code === 'ERR_INVALID_AUTH'
|
|
64
|
+
? 'Invalid npm registry authentication configuration'
|
|
65
|
+
: `Unable to load npm registry configuration: ${error.code || error.name}`
|
|
66
|
+
throw new RegistryError(message, { code, cause: error })
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
registryFor(packageSpec) {
|
|
71
|
+
return normalizeRegistry(npmFetch.pickRegistry(packageSpec, this.requestOptions))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describeRegistry(packageSpec) {
|
|
75
|
+
return sanitizeRegistryUrl(this.registryFor(packageSpec))
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = RegistryConfig
|