Skip to content

Commit addfe9d

Browse files
authored
fix(shadow): scan the target of npm aliases (#1795)
* fix(shadow): scan the target of npm aliases Assisted-by: Codex:GPT-6-Luna * fix(ci): keep one pnpm store layout on Windows Assisted-by: Codex:GPT-6-Luna
1 parent f5b0f59 commit addfe9d

3 files changed

Lines changed: 65 additions & 18 deletions

File tree

‎src/shadow/common.test.mts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
3+
const mockGetAlertsMapFromPurls = vi.hoisted(() => vi.fn())
4+
5+
vi.mock('../utils/alerts-map.mts', () => ({
6+
getAlertsMapFromPurls: mockGetAlertsMapFromPurls,
7+
}))
8+
9+
import { scanPackagesAndLogAlerts } from './common.mts'
10+
11+
describe('scanPackagesAndLogAlerts', () => {
12+
it('scans npm alias arguments using the target package PURL', async () => {
13+
mockGetAlertsMapFromPurls.mockResolvedValue(new Map())
14+
15+
await scanPackagesAndLogAlerts({
16+
acceptRisks: false,
17+
command: 'add',
18+
installCommands: new Set(['add']),
19+
managerName: 'npm',
20+
rawArgs: ['npm', 'alias@npm:real-package@1.2.3'],
21+
})
22+
23+
expect(mockGetAlertsMapFromPurls).toHaveBeenCalledWith(
24+
['pkg:npm/real-package@1.2.3'],
25+
expect.any(Object),
26+
)
27+
})
28+
})

‎src/utils/npm-spec.mts‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
import npmPackageArg from 'npm-package-arg'
25+
import type { AliasResult } from 'npm-package-arg'
2526

2627
import { NPM } from '../constants.mts'
2728
import { createPurlObject } from './purl.mts'
@@ -98,38 +99,37 @@ export function safeParseNpmSpec(
9899
}
99100
}
100101

101-
// Extract name and version from parsed spec.
102-
const name = parsed.name || pkgSpec
102+
// npm aliases carry the target package in subSpec. Use it for PURLs and
103+
// version extraction so the alias label does not become the package name.
104+
const spec =
105+
parsed.type === 'alias' ? (parsed as AliasResult).subSpec : parsed
106+
const name = spec.name || pkgSpec
103107
let version: string | undefined
104108

105109
// Handle different spec types from npm-package-arg.
106-
if (
107-
parsed.type === 'tag' ||
108-
parsed.type === 'version' ||
109-
parsed.type === 'range'
110-
) {
110+
if (spec.type === 'tag' || spec.type === 'version' || spec.type === 'range') {
111111
// For npm registry packages:
112112
// - type 'tag': latest, beta, etc.
113113
// - type 'version': exact version like 1.0.0
114114
// - type 'range': version range like ^1.0.0, ~1.0.0, or * for bare names
115115
// Don't include '*' as a version - it means "any version".
116-
if (parsed.fetchSpec && parsed.fetchSpec !== '*') {
117-
version = parsed.fetchSpec
116+
if (spec.fetchSpec && spec.fetchSpec !== '*') {
117+
version = spec.fetchSpec
118118
} else if (
119-
parsed.rawSpec &&
120-
parsed.rawSpec !== '*' &&
121-
parsed.rawSpec !== parsed.name
119+
spec.rawSpec &&
120+
spec.rawSpec !== '*' &&
121+
spec.rawSpec !== spec.name
122122
) {
123-
version = parsed.rawSpec
123+
version = spec.rawSpec
124124
}
125125
} else if (
126-
parsed.type === 'git' ||
127-
parsed.type === 'remote' ||
128-
parsed.type === 'file'
126+
spec.type === 'git' ||
127+
spec.type === 'remote' ||
128+
spec.type === 'file'
129129
) {
130130
// For non-registry specs, use rawSpec if different from name.
131-
if (parsed.rawSpec && parsed.rawSpec !== parsed.name) {
132-
version = parsed.rawSpec
131+
if (spec.rawSpec && spec.rawSpec !== spec.name) {
132+
version = spec.rawSpec
133133
}
134134
}
135135

‎src/utils/npm-spec.test.mts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { safeParseNpmSpec, safeNpmSpecToPurl } from './npm-spec.mts'
4+
5+
describe('npm aliases', () => {
6+
it.each([
7+
['my-alias@npm:real-package@1.2.3', 'real-package', '1.2.3'],
8+
['my-alias@npm:real-package@^1.2.0', 'real-package', '^1.2.0'],
9+
['my-alias@npm:real-package@beta', 'real-package', 'beta'],
10+
['my-alias@npm:real-package', 'real-package', undefined],
11+
['my-alias@npm:@scope/real-package@2.0.0', '@scope/real-package', '2.0.0'],
12+
['my-alias@npm:real-package@*', 'real-package', undefined],
13+
])('parses %s using its target', (input, name, version) => {
14+
expect(safeParseNpmSpec(input)).toEqual({ name, version })
15+
expect(safeNpmSpecToPurl(input)).toBe(
16+
`pkg:npm/${name}${version ? `@${encodeURIComponent(version)}` : ''}`,
17+
)
18+
})
19+
})

0 commit comments

Comments
 (0)