@pnpm/deps.compliance.audit 1002.0.14

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
4
+ Copyright (c) 2016-2026 Zoltan Kochan and other contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # @pnpm/audit
2
+
3
+ > Audit a lockfile
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@pnpm/audit.svg)](https://www.npmjs.com/package/@pnpm/audit)
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ pnpm add @pnpm/audit
11
+ ```
12
+
13
+ ## License
14
+
15
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { PnpmError } from '@pnpm/error';
2
+ import type { GetAuthHeader } from '@pnpm/fetching.types';
3
+ import type { EnvLockfile, LockfileObject } from '@pnpm/lockfile.types';
4
+ import { type AgentOptions, type RetryTimeoutOptions } from '@pnpm/network.fetch';
5
+ import type { DependenciesField } from '@pnpm/types';
6
+ import type { AuditReport } from './types.js';
7
+ export * from './types.js';
8
+ export declare function audit(lockfile: LockfileObject, getAuthHeader: GetAuthHeader, opts: {
9
+ agentOptions?: AgentOptions;
10
+ envLockfile?: EnvLockfile | null;
11
+ include?: {
12
+ [dependenciesField in DependenciesField]: boolean;
13
+ };
14
+ lockfileDir: string;
15
+ registry: string;
16
+ retry?: RetryTimeoutOptions;
17
+ timeout?: number;
18
+ virtualStoreDirMaxLength: number;
19
+ }): Promise<AuditReport>;
20
+ export declare class AuditEndpointNotExistsError extends PnpmError {
21
+ constructor(endpoint: string);
22
+ }
package/lib/index.js ADDED
@@ -0,0 +1,52 @@
1
+ import { PnpmError } from '@pnpm/error';
2
+ import { fetchWithAgent } from '@pnpm/network.fetch';
3
+ import { lockfileToAuditTree } from './lockfileToAuditTree.js';
4
+ export * from './types.js';
5
+ export async function audit(lockfile, getAuthHeader, opts) {
6
+ const auditTree = await lockfileToAuditTree(lockfile, { envLockfile: opts.envLockfile, include: opts.include, lockfileDir: opts.lockfileDir });
7
+ const registry = opts.registry.endsWith('/') ? opts.registry : `${opts.registry}/`;
8
+ const auditUrl = `${registry}-/npm/v1/security/audits`;
9
+ const quickAuditUrl = `${registry}-/npm/v1/security/audits/quick`;
10
+ const authHeaderValue = getAuthHeader(registry);
11
+ const requestBody = JSON.stringify(auditTree);
12
+ const requestHeaders = {
13
+ 'Content-Type': 'application/json',
14
+ ...getAuthHeaders(authHeaderValue),
15
+ };
16
+ const requestOptions = {
17
+ agentOptions: opts.agentOptions ?? {},
18
+ body: requestBody,
19
+ headers: requestHeaders,
20
+ method: 'POST',
21
+ retry: opts.retry,
22
+ timeout: opts.timeout,
23
+ };
24
+ const quickRes = await fetchWithAgent(quickAuditUrl, requestOptions);
25
+ if (quickRes.status === 200) {
26
+ return quickRes.json();
27
+ }
28
+ const res = await fetchWithAgent(auditUrl, requestOptions);
29
+ if (res.status === 200) {
30
+ return res.json();
31
+ }
32
+ if (quickRes.status === 404 && res.status === 404) {
33
+ throw new AuditEndpointNotExistsError(quickAuditUrl);
34
+ }
35
+ throw new PnpmError('AUDIT_BAD_RESPONSE', `The audit endpoint (at ${quickAuditUrl}) responded with ${quickRes.status}: ${await quickRes.text()}. Fallback endpoint (at ${auditUrl}) responded with ${res.status}: ${await res.text()}`);
36
+ }
37
+ function getAuthHeaders(authHeaderValue) {
38
+ const headers = {};
39
+ if (authHeaderValue) {
40
+ headers['authorization'] = authHeaderValue;
41
+ }
42
+ return headers;
43
+ }
44
+ export class AuditEndpointNotExistsError extends PnpmError {
45
+ constructor(endpoint) {
46
+ const message = `The audit endpoint (at ${endpoint}) is doesn't exist.`;
47
+ super('AUDIT_ENDPOINT_NOT_EXISTS', message, {
48
+ hint: 'This issue is probably because you are using a private npm registry and that endpoint doesn\'t have an implementation of audit.',
49
+ });
50
+ }
51
+ }
52
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,24 @@
1
+ import type { EnvLockfile, LockfileObject } from '@pnpm/lockfile.types';
2
+ import type { DependenciesField } from '@pnpm/types';
3
+ export interface AuditNode {
4
+ version?: string;
5
+ integrity?: string;
6
+ requires?: Record<string, string>;
7
+ dependencies?: {
8
+ [name: string]: AuditNode;
9
+ };
10
+ dev: boolean;
11
+ }
12
+ export interface AuditTree extends AuditNode {
13
+ name?: string;
14
+ install: string[];
15
+ remove: string[];
16
+ metadata: unknown;
17
+ }
18
+ export declare function lockfileToAuditTree(lockfile: LockfileObject, opts: {
19
+ envLockfile?: EnvLockfile | null;
20
+ include?: {
21
+ [dependenciesField in DependenciesField]: boolean;
22
+ };
23
+ lockfileDir: string;
24
+ }): Promise<AuditTree>;
@@ -0,0 +1,93 @@
1
+ import path from 'node:path';
2
+ import { DepType, detectDepTypes } from '@pnpm/lockfile.detect-dep-types';
3
+ import { convertToLockfileObject } from '@pnpm/lockfile.fs';
4
+ import { nameVerFromPkgSnapshot } from '@pnpm/lockfile.utils';
5
+ import { lockfileWalkerGroupImporterSteps } from '@pnpm/lockfile.walker';
6
+ import { safeReadProjectManifestOnly } from '@pnpm/workspace.project-manifest-reader';
7
+ import { map as mapValues } from 'ramda';
8
+ export async function lockfileToAuditTree(lockfile, opts) {
9
+ const importerWalkers = lockfileWalkerGroupImporterSteps(lockfile, Object.keys(lockfile.importers), { include: opts?.include });
10
+ const dependencies = {};
11
+ const depTypes = detectDepTypes(lockfile);
12
+ await Promise.all(importerWalkers.map(async (importerWalker) => {
13
+ const importerDeps = lockfileToAuditNode(depTypes, importerWalker.step);
14
+ // For some reason the registry responds with 500 if the keys in dependencies have slashes
15
+ // see issue: https://github.com/pnpm/pnpm/issues/2848
16
+ const depName = importerWalker.importerId.replace(/\//g, '__');
17
+ const manifest = await safeReadProjectManifestOnly(path.join(opts.lockfileDir, importerWalker.importerId));
18
+ dependencies[depName] = {
19
+ dependencies: importerDeps,
20
+ dev: false,
21
+ requires: toRequires(importerDeps),
22
+ version: manifest?.version ?? '0.0.0',
23
+ };
24
+ }));
25
+ if (opts.envLockfile) {
26
+ const envLockfileObject = envLockfileToLockfileObject(opts.envLockfile);
27
+ const envDepTypes = detectDepTypes(envLockfileObject);
28
+ for (const { importerId, step } of lockfileWalkerGroupImporterSteps(envLockfileObject, Object.keys(envLockfileObject.importers), { include: opts.include })) {
29
+ const deps = lockfileToAuditNode(envDepTypes, step);
30
+ if (Object.keys(deps).length > 0) {
31
+ dependencies[importerId] = wrapDepsGroup(deps);
32
+ }
33
+ }
34
+ }
35
+ const auditTree = {
36
+ name: undefined,
37
+ version: undefined,
38
+ dependencies,
39
+ dev: false,
40
+ install: [],
41
+ integrity: undefined,
42
+ metadata: {},
43
+ remove: [],
44
+ requires: toRequires(dependencies),
45
+ };
46
+ return auditTree;
47
+ }
48
+ function lockfileToAuditNode(depTypes, step) {
49
+ const dependencies = {};
50
+ for (const { depPath, pkgSnapshot, next } of step.dependencies) {
51
+ const { name, version } = nameVerFromPkgSnapshot(depPath, pkgSnapshot);
52
+ const subdeps = lockfileToAuditNode(depTypes, next());
53
+ const dep = {
54
+ dev: depTypes[depPath] === DepType.DevOnly,
55
+ integrity: pkgSnapshot.resolution.integrity,
56
+ version,
57
+ };
58
+ if (Object.keys(subdeps).length > 0) {
59
+ dep.dependencies = subdeps;
60
+ dep.requires = toRequires(subdeps);
61
+ }
62
+ dependencies[name] = dep;
63
+ }
64
+ return dependencies;
65
+ }
66
+ function toRequires(auditNodesByDepName) {
67
+ return mapValues((auditNode) => auditNode.version, auditNodesByDepName);
68
+ }
69
+ function wrapDepsGroup(deps) {
70
+ return {
71
+ dependencies: deps,
72
+ dev: false,
73
+ requires: toRequires(deps),
74
+ version: '0.0.0',
75
+ };
76
+ }
77
+ function envLockfileToLockfileObject(envLockfile) {
78
+ const envImporter = envLockfile.importers['.'];
79
+ const importers = {};
80
+ if (Object.keys(envImporter.configDependencies).length > 0) {
81
+ importers['configDependencies'] = { dependencies: envImporter.configDependencies };
82
+ }
83
+ if (envImporter.packageManagerDependencies) {
84
+ importers['packageManagerDependencies'] = { dependencies: envImporter.packageManagerDependencies };
85
+ }
86
+ return convertToLockfileObject({
87
+ lockfileVersion: envLockfile.lockfileVersion,
88
+ importers,
89
+ packages: envLockfile.packages,
90
+ snapshots: envLockfile.snapshots,
91
+ });
92
+ }
93
+ //# sourceMappingURL=lockfileToAuditTree.js.map
package/lib/types.d.ts ADDED
@@ -0,0 +1,88 @@
1
+ export interface AuditVulnerabilityCounts {
2
+ info: number;
3
+ low: number;
4
+ moderate: number;
5
+ high: number;
6
+ critical: number;
7
+ }
8
+ export interface IgnoredAuditVulnerabilityCounts {
9
+ low: number;
10
+ moderate: number;
11
+ high: number;
12
+ critical: number;
13
+ }
14
+ export interface AuditResolution {
15
+ id: number;
16
+ path: string;
17
+ dev: boolean;
18
+ optional: boolean;
19
+ bundled: boolean;
20
+ }
21
+ export interface AuditAction {
22
+ action: string;
23
+ module: string;
24
+ target: string;
25
+ isMajor: boolean;
26
+ resolves: AuditResolution[];
27
+ }
28
+ export type AuditLevelString = 'low' | 'moderate' | 'high' | 'critical';
29
+ export type AuditLevelNumber = 0 | 1 | 2 | 3;
30
+ export interface AuditAdvisory {
31
+ findings: [
32
+ {
33
+ version: string;
34
+ paths: string[];
35
+ dev: boolean;
36
+ optional: boolean;
37
+ bundled: boolean;
38
+ }
39
+ ];
40
+ id: number;
41
+ created: string;
42
+ updated: string;
43
+ deleted?: boolean;
44
+ title: string;
45
+ found_by: {
46
+ name: string;
47
+ };
48
+ reported_by: {
49
+ name: string;
50
+ };
51
+ module_name: string;
52
+ cves: string[];
53
+ vulnerable_versions: string;
54
+ patched_versions: string;
55
+ overview: string;
56
+ recommendation: string;
57
+ references: string;
58
+ access: string;
59
+ severity: AuditLevelString;
60
+ cwe: string;
61
+ github_advisory_id: string;
62
+ metadata: {
63
+ module_type: string;
64
+ exploitability: number;
65
+ affected_components: string;
66
+ };
67
+ url: string;
68
+ }
69
+ export interface AuditMetadata {
70
+ vulnerabilities: AuditVulnerabilityCounts;
71
+ dependencies: number;
72
+ devDependencies: number;
73
+ optionalDependencies: number;
74
+ totalDependencies: number;
75
+ }
76
+ export interface AuditReport {
77
+ actions: AuditAction[];
78
+ advisories: {
79
+ [id: string]: AuditAdvisory;
80
+ };
81
+ muted: unknown[];
82
+ metadata: AuditMetadata;
83
+ }
84
+ export interface AuditActionRecommendation {
85
+ cmd: string;
86
+ isBreaking: boolean;
87
+ action: AuditAction;
88
+ }
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@pnpm/deps.compliance.audit",
3
+ "version": "1002.0.14",
4
+ "description": "Audit a lockfile",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm11",
8
+ "audit"
9
+ ],
10
+ "license": "MIT",
11
+ "funding": "https://opencollective.com/pnpm",
12
+ "repository": "https://github.com/pnpm/pnpm/tree/main/deps/compliance/audit",
13
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/deps/compliance/audit#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/pnpm/pnpm/issues"
16
+ },
17
+ "type": "module",
18
+ "main": "lib/index.js",
19
+ "types": "lib/index.d.ts",
20
+ "exports": {
21
+ ".": "./lib/index.js"
22
+ },
23
+ "files": [
24
+ "lib",
25
+ "!*.map"
26
+ ],
27
+ "dependencies": {
28
+ "ramda": "npm:@pnpm/ramda@0.28.1",
29
+ "@pnpm/error": "1000.0.5",
30
+ "@pnpm/fetching.types": "1000.2.0",
31
+ "@pnpm/lockfile.fs": "1001.1.21",
32
+ "@pnpm/lockfile.detect-dep-types": "1001.0.16",
33
+ "@pnpm/lockfile.types": "1002.0.2",
34
+ "@pnpm/lockfile.walker": "1001.0.16",
35
+ "@pnpm/network.fetch": "1000.2.6",
36
+ "@pnpm/types": "1000.9.0",
37
+ "@pnpm/lockfile.utils": "1003.0.3",
38
+ "@pnpm/workspace.project-manifest-reader": "1001.1.4"
39
+ },
40
+ "peerDependencies": {
41
+ "@pnpm/logger": ">=1001.0.0 <1002.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/ramda": "0.29.12",
45
+ "nock": "13.3.4",
46
+ "@pnpm/logger": "1001.0.1",
47
+ "@pnpm/test-fixtures": "1000.0.0",
48
+ "@pnpm/deps.compliance.audit": "1002.0.14",
49
+ "@pnpm/constants": "1001.3.1"
50
+ },
51
+ "engines": {
52
+ "node": ">=22.13"
53
+ },
54
+ "jest": {
55
+ "preset": "@pnpm/jest-config"
56
+ },
57
+ "scripts": {
58
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
59
+ "_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest",
60
+ "test": "pnpm run compile && pnpm run _test",
61
+ "compile": "tsgo --build && pnpm run lint --fix"
62
+ }
63
+ }