|
| 1 | +#!/usr/bin/env node |
| 2 | +import { test } from 'node:test' |
| 3 | +import assert from 'node:assert' |
| 4 | +import { execFileSync } from 'node:child_process' |
| 5 | +import { join } from 'node:path' |
| 6 | + |
| 7 | +const hookPath = join(import.meta.dirname, 'socket-gate.ts') |
| 8 | + |
| 9 | +function runHook (input: string, env?: Record<string, string>): string { |
| 10 | + return execFileSync('node', ['--experimental-strip-types', hookPath], { |
| 11 | + input, |
| 12 | + encoding: 'utf-8', |
| 13 | + timeout: 30_000, |
| 14 | + env: { |
| 15 | + ...process.env, |
| 16 | + ...env |
| 17 | + } |
| 18 | + }).trim() |
| 19 | +} |
| 20 | + |
| 21 | +function parseOutput (output: string): { decision: string, reason?: string } { |
| 22 | + const parsed = JSON.parse(output) |
| 23 | + return { |
| 24 | + decision: parsed.hookSpecificOutput.permissionDecision, |
| 25 | + reason: parsed.hookSpecificOutput.permissionDecisionReason |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function makeInput (command: string): string { |
| 30 | + return JSON.stringify({ |
| 31 | + session_id: 'test', |
| 32 | + tool_name: 'Bash', |
| 33 | + tool_input: { command } |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +test('socket-gate hook', async (t) => { |
| 38 | + const apiKey = process.env['SOCKET_API_KEY'] |
| 39 | + |
| 40 | + // ======================================== |
| 41 | + // Unit tests (no API key required) |
| 42 | + // ======================================== |
| 43 | + |
| 44 | + await t.test('allows non-Bash tools', () => { |
| 45 | + const input = JSON.stringify({ session_id: 'test', tool_name: 'Read', tool_input: { path: '/tmp/foo' } }) |
| 46 | + const result = parseOutput(runHook(input)) |
| 47 | + assert.strictEqual(result.decision, 'allow') |
| 48 | + }) |
| 49 | + |
| 50 | + await t.test('allows non-install commands', () => { |
| 51 | + const result = parseOutput(runHook(makeInput('ls -la'))) |
| 52 | + assert.strictEqual(result.decision, 'allow') |
| 53 | + }) |
| 54 | + |
| 55 | + await t.test('allows lockfile-only installs', () => { |
| 56 | + for (const cmd of ['npm install', 'npm i', 'npm ci', 'yarn', 'yarn install', 'bun install', 'pnpm install']) { |
| 57 | + const result = parseOutput(runHook(makeInput(cmd))) |
| 58 | + assert.strictEqual(result.decision, 'allow', `should allow: ${cmd}`) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + await t.test('allows empty input', () => { |
| 63 | + const result = parseOutput(runHook('')) |
| 64 | + assert.strictEqual(result.decision, 'allow') |
| 65 | + }) |
| 66 | + |
| 67 | + await t.test('allows invalid JSON', () => { |
| 68 | + const result = parseOutput(runHook('not json')) |
| 69 | + assert.strictEqual(result.decision, 'allow') |
| 70 | + }) |
| 71 | + |
| 72 | + await t.test('allows when no API key is set', () => { |
| 73 | + const result = parseOutput(runHook(makeInput('npm install malicious-pkg'), { SOCKET_API_KEY: '' })) |
| 74 | + assert.strictEqual(result.decision, 'allow') |
| 75 | + }) |
| 76 | + |
| 77 | + // ======================================== |
| 78 | + // Integration tests (require SOCKET_API_KEY) |
| 79 | + // ======================================== |
| 80 | + |
| 81 | + await t.test('allows safe package (lodash)', { skip: !apiKey && 'SOCKET_API_KEY not set' }, () => { |
| 82 | + const result = parseOutput(runHook(makeInput('npm install lodash'))) |
| 83 | + assert.strictEqual(result.decision, 'allow') |
| 84 | + }) |
| 85 | + |
| 86 | + await t.test('allows safe scoped package (@types/node)', { skip: !apiKey && 'SOCKET_API_KEY not set' }, () => { |
| 87 | + const result = parseOutput(runHook(makeInput('yarn add @types/node'))) |
| 88 | + assert.strictEqual(result.decision, 'allow') |
| 89 | + }) |
| 90 | + |
| 91 | + await t.test('blocks typosquat (browserlist)', { skip: !apiKey && 'SOCKET_API_KEY not set' }, () => { |
| 92 | + const result = parseOutput(runHook(makeInput('npm install browserlist'))) |
| 93 | + assert.strictEqual(result.decision, 'deny') |
| 94 | + assert.ok(result.reason?.includes('browserlist'), 'reason should mention package name') |
| 95 | + assert.ok(result.reason?.includes('socket.dev'), 'reason should include review link') |
| 96 | + }) |
| 97 | + |
| 98 | + await t.test('handles versioned install', { skip: !apiKey && 'SOCKET_API_KEY not set' }, () => { |
| 99 | + const result = parseOutput(runHook(makeInput('npm install express@4.18.2'))) |
| 100 | + assert.strictEqual(result.decision, 'allow') |
| 101 | + }) |
| 102 | + |
| 103 | + await t.test('handles pnpm add', { skip: !apiKey && 'SOCKET_API_KEY not set' }, () => { |
| 104 | + const result = parseOutput(runHook(makeInput('pnpm add express'))) |
| 105 | + assert.strictEqual(result.decision, 'allow') |
| 106 | + }) |
| 107 | + |
| 108 | + await t.test('handles bun add', { skip: !apiKey && 'SOCKET_API_KEY not set' }, () => { |
| 109 | + const result = parseOutput(runHook(makeInput('bun add express'))) |
| 110 | + assert.strictEqual(result.decision, 'allow') |
| 111 | + }) |
| 112 | +}) |
0 commit comments