Skip to content
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
31 changes: 22 additions & 9 deletions src/commands/organization/cmd-organization-dependencies.mts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { logger } from '@socketsecurity/registry/lib/logger'

import { handleDependencies } from './handle-dependencies.mts'
import constants, { FLAG_JSON, FLAG_MARKDOWN } from '../../constants.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { checkCommandInput } from '../../utils/check-input.mts'
import { InputError } from '../../utils/errors.mts'
import { getOutputKind } from '../../utils/get-output-kind.mts'
import type {
CliCommandConfig,
CliCommandContext,
} from '../../utils/meow-with-subcommands.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import {
getFlagApiRequirementsOutput,
getFlagListOutput,
} from '../../utils/output-formatting.mts'
import { hasDefaultApiToken } from '../../utils/sdk.mts'

import type {
CliCommandConfig,
CliCommandContext,
} from '../../utils/meow-with-subcommands.mts'
import { parsePurlsOption } from './dependencies-filters.mts'
import { handleDependencies } from './handle-dependencies.mts'

export const CMD_NAME = 'dependencies'

Expand Down Expand Up @@ -49,7 +49,13 @@ async function run(
offset: {
type: 'number',
default: 0,
description: 'Page number',
description: 'Number of dependencies to skip',
},
purls: {
type: 'string',
isMultiple: true,
description:
'Search by PURL; pkg: is optional. Comma-separate values or repeat this flag',
},
...outputFlags,
},
Expand All @@ -66,6 +72,7 @@ async function run(
Examples
${command}
${command} --limit 20 --offset 10
${command} --purls 'npm/minimist@0.0.4,pkg:pypi/requests'
`,
}

Expand All @@ -76,7 +83,12 @@ async function run(
importMeta,
})

const { json, limit, markdown, offset } = cli.flags
const { json, limit, markdown, offset, purls: suppliedPurls } = cli.flags
const parsedPurls = parsePurlsOption(argv, suppliedPurls)
if (!parsedPurls.ok) {
throw new InputError(parsedPurls.message)
}
const { purls } = parsedPurls

const dryRun = !!cli.flags['dryRun']

Expand Down Expand Up @@ -112,5 +124,6 @@ async function run(
limit: Number(limit || 0) || 0,
offset: Number(offset || 0) || 0,
outputKind,
purls,
})
}
79 changes: 77 additions & 2 deletions src/commands/organization/cmd-organization-dependencies.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ describe('socket organization dependencies', async () => {
--json Output as JSON
--limit Maximum number of dependencies returned
--markdown Output as Markdown
--offset Page number
--offset Number of dependencies to skip
--purls Search by PURL; pkg: is optional. Comma-separate values or repeat this flag

Examples
socket organization dependencies
socket organization dependencies --limit 20 --offset 10"
socket organization dependencies --limit 20 --offset 10
socket organization dependencies --purls 'npm/minimist@0.0.4,pkg:pypi/requests'"
`,
)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -74,4 +76,77 @@ describe('socket organization dependencies', async () => {
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
},
)

cmdit(
[
'organization',
'dependencies',
FLAG_DRY_RUN,
'--purls',
'npm/minimist@0.0.4,pkg:pypi/requests',
'--purls',
'npm/lodash',
FLAG_CONFIG,
'{"apiToken":"fakeToken"}',
],
'should accept comma-separated and repeated PURL filters in a dry run',
async cmd => {
const { code, stdout } = await spawnSocketCli(binCliPath, cmd)
expect(code).toBe(0)
expect(stdout).toContain('[DryRun]: Bailing now')
},
)

cmdit(
[
'organization',
'dependencies',
FLAG_DRY_RUN,
'--purls',
'minimist',
FLAG_CONFIG,
'{"apiToken":"fakeToken"}',
],
'should reject invalid PURLs before dry-run success',
async cmd => {
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd)
expect(code).not.toBe(0)
expect(`${stdout}\n${stderr}`).toContain('Invalid PURL "minimist"')
expect(stdout).not.toContain('[DryRun]: Bailing now')
},
)

cmdit(
[
'organization',
'dependencies',
FLAG_DRY_RUN,
FLAG_CONFIG,
'{"apiToken":"fakeToken"}',
'--purls',
],
'should reject a bare PURL flag',
async cmd => {
const { code, stdout } = await spawnSocketCli(binCliPath, cmd)
expect(code).not.toBe(0)
expect(stdout).not.toContain('[DryRun]: Bailing now')
},
)

cmdit(
[
'organization',
'dependencies',
FLAG_DRY_RUN,
FLAG_CONFIG,
'{"apiToken":"fakeToken"}',
'--purls=',
],
'should reject an explicitly empty PURL filter',
async cmd => {
const { code, stdout } = await spawnSocketCli(binCliPath, cmd)
expect(code).not.toBe(0)
expect(stdout).not.toContain('[DryRun]: Bailing now')
},
)
})
113 changes: 113 additions & 0 deletions src/commands/organization/dependencies-filters.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { getPurlObject, normalizePurl } from '../../utils/purl.mts'

export type DependenciesPurlsResult =
| { ok: true; purls: string[] | undefined }
| { ok: false; message: string }

export function parsePurlsOption(
argv: readonly string[],
suppliedPurls: unknown,
): DependenciesPurlsResult {
const hasPurlsOption = argv.some(
argument => argument === '--purls' || argument.startsWith('--purls='),
)
if (suppliedPurls === undefined) {
return hasPurlsOption
? parseDependenciesPurls([])
: { ok: true, purls: undefined }
}
if (
!hasPurlsOption &&
Array.isArray(suppliedPurls) &&
suppliedPurls.length === 0
) {
return { ok: true, purls: undefined }
}
if (
!Array.isArray(suppliedPurls) ||
!suppliedPurls.every(
(value: unknown): value is string => typeof value === 'string',
)
) {
return {
ok: false,
message: 'Provide at least one non-empty PURL with --purls.',
}
}
return parseDependenciesPurls(suppliedPurls)
}

export function parseDependenciesPurls(
values: readonly string[] | undefined,
): DependenciesPurlsResult {
if (values === undefined) {
return { ok: true, purls: undefined }
}

const purls: string[] = []
const seen = new Set<string>()
const purlValues = values.flatMap(value => value.split(','))

for (const value of purlValues) {
const purl = normalizePurl(value.trim())
const parsed = getPurlObject(purl, { throws: false })

if (!parsed) {
return {
ok: false,
message: `Invalid PURL "${value}". Use a full PURL such as pkg:npm/minimist@1.2.3.`,
}
}

if (Object.keys(parsed.qualifiers ?? {}).length > 0 || parsed.subpath) {
return {
ok: false,
message: `PURL "${value}" includes qualifiers or a subpath, which this search does not support. Search by package identity and version only.`,
}
}

if (!seen.has(purl)) {
seen.add(purl)
purls.push(purl)
}
}

if (purls.length === 0) {
return {
ok: false,
message: 'Provide at least one non-empty PURL with --purls.',
}
}

return { ok: true, purls }
}

export function matchesConfirmedPurls(
requested: readonly string[],
filters: unknown,
): boolean {
if (typeof filters !== 'object' || filters === null) {
return false
}
if (!('valid' in filters) || !('invalid' in filters)) {
return false
}
const { invalid, valid } = filters
if (
!Array.isArray(valid) ||
!valid.every(value => typeof value === 'string') ||
!Array.isArray(invalid) ||
!invalid.every(value => typeof value === 'string') ||
invalid.length > 0
) {
return false
}
const expected = new Set(requested)
const actual = new Set(valid)
return (
expected.size === requested.length &&
actual.size === valid.length &&
expected.size === actual.size &&
[...expected].every(value => actual.has(value))
)
}
Loading
Loading