Skip to content

Commit

Permalink
fix(audit): project versions (#5661)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Nov 20, 2022
1 parent c216a4e commit 3801d2f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-onions-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pnpm": patch
---

`pnpm audit` should send the versions of workspace projects for audit.
5 changes: 5 additions & 0 deletions .changeset/shy-foxes-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/audit": major
---

New required option added: lockfileDir.
2 changes: 2 additions & 0 deletions lockfile/audit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@pnpm/audit": "workspace:*",
"@pnpm/constants": "workspace:*",
"@pnpm/lockfile-file": "workspace:*",
"@pnpm/test-fixtures": "workspace:*",
"@types/ramda": "0.28.15",
"nock": "13.2.9"
},
Expand All @@ -43,6 +44,7 @@
"@pnpm/lockfile-types": "workspace:*",
"@pnpm/lockfile-utils": "workspace:*",
"@pnpm/lockfile-walker": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/types": "workspace:*",
"ramda": "npm:@pnpm/ramda@0.28.1"
},
Expand Down
3 changes: 2 additions & 1 deletion lockfile/audit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export async function audit (
opts: {
agentOptions?: AgentOptions
include?: { [dependenciesField in DependenciesField]: boolean }
lockfileDir: string
registry: string
retry?: RetryTimeoutOptions
timeout?: number
}
) {
const auditTree = lockfileToAuditTree(lockfile, { include: opts.include })
const auditTree = await lockfileToAuditTree(lockfile, { include: opts.include, lockfileDir: opts.lockfileDir })
const registry = opts.registry.endsWith('/') ? opts.registry : `${opts.registry}/`
const auditUrl = `${registry}-/npm/v1/security/audits`
const authHeaderValue = getAuthHeader(registry)
Expand Down
34 changes: 20 additions & 14 deletions lockfile/audit/src/lockfileToAuditTree.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path'
import { Lockfile } from '@pnpm/lockfile-types'
import { nameVerFromPkgSnapshot } from '@pnpm/lockfile-utils'
import { lockfileWalkerGroupImporterSteps, LockfileWalkerStep } from '@pnpm/lockfile-walker'
import { DependenciesField } from '@pnpm/types'
import { readProjectManifest } from '@pnpm/read-project-manifest'
import mapValues from 'ramda/src/map'

export interface AuditNode {
Expand All @@ -19,25 +21,29 @@ export type AuditTree = AuditNode & {
metadata: Object
}

export function lockfileToAuditTree (
export async function lockfileToAuditTree (
lockfile: Lockfile,
opts?: {
opts: {
include?: { [dependenciesField in DependenciesField]: boolean }
lockfileDir: string
}
): AuditTree {
): Promise<AuditTree> {
const importerWalkers = lockfileWalkerGroupImporterSteps(lockfile, Object.keys(lockfile.importers), { include: opts?.include })
const dependencies = {}
importerWalkers.forEach((importerWalker) => {
const importerDeps = lockfileToAuditNode(importerWalker.step)
// For some reason the registry responds with 500 if the keys in dependencies have slashes
// see issue: https://github.com/pnpm/pnpm/issues/2848
const depName = importerWalker.importerId.replace(/\//g, '__')
dependencies[depName] = {
dependencies: importerDeps,
requires: toRequires(importerDeps),
version: '0.0.0',
}
})
await Promise.all(
importerWalkers.map(async (importerWalker) => {
const importerDeps = lockfileToAuditNode(importerWalker.step)
// For some reason the registry responds with 500 if the keys in dependencies have slashes
// see issue: https://github.com/pnpm/pnpm/issues/2848
const depName = importerWalker.importerId.replace(/\//g, '__')
const { manifest } = await readProjectManifest(path.join(opts.lockfileDir, importerWalker.importerId))
dependencies[depName] = {
dependencies: importerDeps,
requires: toRequires(importerDeps),
version: manifest.version,
}
})
)
const auditTree: AuditTree = {
name: undefined,
version: undefined,
Expand Down
4 changes: 4 additions & 0 deletions lockfile/audit/test/__fixtures__/one-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg",
"version": "1.0.0"
}
14 changes: 9 additions & 5 deletions lockfile/audit/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { audit } from '@pnpm/audit'
import { LOCKFILE_VERSION } from '@pnpm/constants'
import { PnpmError } from '@pnpm/error'
import { fixtures } from '@pnpm/test-fixtures'
import nock from 'nock'
import { lockfileToAuditTree } from '../lib/lockfileToAuditTree'

const f = fixtures(__dirname)

describe('audit', () => {
test('lockfileToAuditTree()', () => {
expect(lockfileToAuditTree({
test('lockfileToAuditTree()', async () => {
expect(await lockfileToAuditTree({
importers: {
'.': {
dependencies: {
Expand All @@ -33,7 +36,7 @@ describe('audit', () => {
},
},
},
})).toEqual({
}, { lockfileDir: f.find('one-project') })).toEqual({
name: undefined,
version: undefined,

Expand All @@ -59,15 +62,15 @@ describe('audit', () => {
requires: {
foo: '1.0.0',
},
version: '0.0.0',
version: '1.0.0',
},
},
dev: false,
install: [],
integrity: undefined,
metadata: {},
remove: [],
requires: { '.': '0.0.0' },
requires: { '.': '1.0.0' },
})
})

Expand All @@ -88,6 +91,7 @@ describe('audit', () => {
},
getAuthHeader,
{
lockfileDir: f.find('one-project'),
registry,
retry: {
retries: 0,
Expand Down
6 changes: 6 additions & 0 deletions lockfile/audit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../__utils__/test-fixtures"
},
{
"path": "../../network/fetch"
},
Expand All @@ -24,6 +27,9 @@
{
"path": "../../packages/types"
},
{
"path": "../../pkg-manifest/read-project-manifest"
},
{
"path": "../lockfile-file"
},
Expand Down
4 changes: 3 additions & 1 deletion lockfile/plugin-commands-audit/src/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export async function handler (
| 'rootProjectManifest'
>
) {
const lockfile = await readWantedLockfile(opts.lockfileDir ?? opts.dir, { ignoreIncompatible: true })
const lockfileDir = opts.lockfileDir ?? opts.dir
const lockfile = await readWantedLockfile(lockfileDir, { ignoreIncompatible: true })
if (lockfile == null) {
throw new PnpmError('AUDIT_NO_LOCKFILE', `No ${WANTED_LOCKFILE} found: Cannot audit a project without a lockfile`)
}
Expand All @@ -156,6 +157,7 @@ export async function handler (
timeout: opts.fetchTimeout,
},
include,
lockfileDir,
registry: opts.registries.default,
retry: {
factor: opts.fetchRetryFactor,
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3801d2f

Please sign in to comment.