|
| 1 | +/** |
| 2 | + * @fileoverview Socket pip command - forwards pip operations to Socket Firewall (sfw). |
| 3 | + * |
| 4 | + * This command wraps pip with Socket Firewall security scanning, providing real-time |
| 5 | + * security analysis of Python packages before installation. |
| 6 | + * |
| 7 | + * Architecture: |
| 8 | + * - Parses Socket CLI flags (--help, --config, etc.) |
| 9 | + * - Filters out Socket-specific flags |
| 10 | + * - Forwards remaining arguments to Socket Firewall via npx |
| 11 | + * - Socket Firewall acts as a proxy for pip operations |
| 12 | + * |
| 13 | + * Usage: |
| 14 | + * socket pip install <package> |
| 15 | + * socket pip install -r requirements.txt |
| 16 | + * socket pip list |
| 17 | + * |
| 18 | + * Environment: |
| 19 | + * Requires Node.js and npx (bundled with npm) |
| 20 | + * Socket Firewall (sfw) is downloaded automatically via npx on first use |
| 21 | + * |
| 22 | + * See also: |
| 23 | + * - Socket Firewall: https://www.npmjs.com/package/sfw |
| 24 | + * - Python CLI: src/utils/python-standalone.mts |
| 25 | + */ |
| 26 | + |
| 27 | +import { spawn } from '@socketsecurity/registry/lib/spawn' |
| 28 | + |
| 29 | +import constants from '../../constants.mts' |
| 30 | +import { commonFlags } from '../../flags.mts' |
| 31 | +import { filterFlags } from '../../utils/cmd.mts' |
| 32 | +import { meowOrExit } from '../../utils/meow-with-subcommands.mts' |
| 33 | + |
| 34 | +import type { |
| 35 | + CliCommandConfig, |
| 36 | + CliCommandContext, |
| 37 | +} from '../../utils/meow-with-subcommands.mts' |
| 38 | + |
| 39 | +const { WIN32 } = constants |
| 40 | + |
| 41 | +const CMD_NAME = 'pip' |
| 42 | +const description = 'Run pip with Socket Firewall security' |
| 43 | + |
| 44 | +/** |
| 45 | + * Command export for socket pip. |
| 46 | + * Provides description and run function for CLI registration. |
| 47 | + */ |
| 48 | +export const cmdPip = { |
| 49 | + description, |
| 50 | + hidden: false, |
| 51 | + run, |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Execute the socket pip command. |
| 56 | + * |
| 57 | + * Flow: |
| 58 | + * 1. Parse CLI flags with meow to handle --help |
| 59 | + * 2. Filter out Socket CLI flags (--config, --org, etc.) |
| 60 | + * 3. Forward remaining arguments to Socket Firewall via npx |
| 61 | + * 4. Socket Firewall proxies the pip command with security scanning |
| 62 | + * 5. Exit with the same code as the pip command |
| 63 | + * |
| 64 | + * @param argv - Command arguments (after "pip") |
| 65 | + * @param importMeta - Import metadata for meow |
| 66 | + * @param context - CLI command context (parent name, etc.) |
| 67 | + */ |
| 68 | +async function run( |
| 69 | + argv: string[] | readonly string[], |
| 70 | + importMeta: ImportMeta, |
| 71 | + context: CliCommandContext, |
| 72 | +): Promise<void> { |
| 73 | + const { parentName } = { __proto__: null, ...context } as CliCommandContext |
| 74 | + const config: CliCommandConfig = { |
| 75 | + commandName: CMD_NAME, |
| 76 | + description, |
| 77 | + hidden: false, |
| 78 | + flags: { |
| 79 | + ...commonFlags, |
| 80 | + }, |
| 81 | + help: command => ` |
| 82 | + Usage |
| 83 | + $ ${command} ... |
| 84 | +
|
| 85 | + Note: Everything after "${CMD_NAME}" is forwarded to Socket Firewall (sfw). |
| 86 | + Socket Firewall provides real-time security scanning for pip packages. |
| 87 | +
|
| 88 | + Examples |
| 89 | + $ ${command} install flask |
| 90 | + $ ${command} install -r requirements.txt |
| 91 | + $ ${command} list |
| 92 | + `, |
| 93 | + } |
| 94 | + |
| 95 | + // Parse flags to handle --help |
| 96 | + meowOrExit({ |
| 97 | + argv, |
| 98 | + config, |
| 99 | + importMeta, |
| 100 | + parentName, |
| 101 | + }) |
| 102 | + |
| 103 | + // Filter out Socket CLI flags before forwarding to sfw |
| 104 | + const argsToForward = filterFlags(argv, commonFlags, []) |
| 105 | + |
| 106 | + // Forward arguments to sfw (Socket Firewall) via npx. |
| 107 | + const result = await spawn('npx', ['sfw', 'pip', ...argsToForward], { |
| 108 | + shell: WIN32, |
| 109 | + stdio: 'inherit', |
| 110 | + }) |
| 111 | + |
| 112 | + if (result.code !== 0) { |
| 113 | + process.exitCode = result.code || 1 |
| 114 | + } |
| 115 | +} |
0 commit comments