Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pnpm audit bad API response on private registry #5246

Merged
merged 8 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-kangaroos-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pnpm": patch
---

Fail with a meaningful error when the audit endpoint doesn't exist [#5200](https://github.com/pnpm/pnpm/issues/5200).
8 changes: 8 additions & 0 deletions .changeset/tough-eyes-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@pnpm/audit": patch
"@pnpm/plugin-commands-audit": patch
---

- Add new Error type: AuditEndpointNotExistsError
- On AuditUrl returns 404, AuditEndpointNotExistsError will throw
- When audit handler catches AuditEndpointNotExistsError, the command will return to avoid execute further codes
18 changes: 18 additions & 0 deletions packages/audit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default async function audit (
retry: opts.retry,
timeout: opts.timeout,
})

if (res.status === 404) {
throw new AuditEndpointNotExistsError(auditUrl)
}

if (res.status !== 200) {
throw new PnpmError('AUDIT_BAD_RESPONSE', `The audit endpoint (at ${auditUrl}) responded with ${res.status}: ${await res.text()}`)
}
Expand All @@ -53,3 +58,16 @@ function getAuthHeaders (
}
return headers
}

export class AuditEndpointNotExistsError extends PnpmError {
constructor (endpoint: string) {
const message = `The audit endpoint (at ${endpoint}) is doesn't exist.`
super(
'AUDIT_ENDPOINT_NOT_EXISTS',
message,
{
hint: 'This issue is probably because you are using a private npm registry and that endpoint doesn\'t have an implementation of audit.',
}
)
}
}
2 changes: 2 additions & 0 deletions packages/plugin-commands-audit/src/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export async function handler (
output: err.message,
}
}

throw err
}
if (opts.fix) {
const newOverrides = await fix(opts.dir, auditReport)
Expand Down
18 changes: 18 additions & 0 deletions packages/plugin-commands-audit/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import { audit } from '@pnpm/plugin-commands-audit'
import { AuditEndpointNotExistsError } from '@pnpm/audit'
import nock from 'nock'
import stripAnsi from 'strip-ansi'
import * as responses from './utils/responses'
Expand Down Expand Up @@ -154,3 +155,20 @@ test('audit sends authToken if alwaysAuth is true', async () => {
expect(stripAnsi(output)).toBe('No known vulnerabilities found\n')
expect(exitCode).toBe(0)
})

test('audit endpoint does not exist', async () => {
nock(registries.default)
.post('/-/npm/v1/security/audits')
.reply(404, {})

await expect(audit.handler({
dir: path.join(__dirname, 'fixtures/has-vulnerabilities'),
dev: true,
fetchRetries: 0,
ignoreRegistryErrors: false,
production: false,
userConfig: {},
rawConfig,
registries,
})).rejects.toThrow(AuditEndpointNotExistsError)
})