|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os validate` and `os lint` — the two CLI doors of the |
| 5 | + * `connector-credential-literal` advisory, run for real. |
| 6 | + * |
| 7 | + * The registry proves the rule is WIRED to all three commands |
| 8 | + * (`@objectstack/lint`'s own tests); only running the commands proves each one |
| 9 | + * PRINTS it, on the channel an author reads, without failing the run. |
| 10 | + * |
| 11 | + * Two fixtures: LIT declares three connectors carrying a `Bearer …` literal in |
| 12 | + * each of the four maps (a `rest` entry's `headers` and `defaultHeaders`, an |
| 13 | + * `mcp` http transport's `headers`, an `mcp` stdio transport's `env`); DARK is |
| 14 | + * the same three routed through `auth.credentialRef` with only ordinary |
| 15 | + * entries (`Accept: application/json`) left in the maps, and must stay silent. |
| 16 | + * Every value is a probe sentinel, not a credential. |
| 17 | + * |
| 18 | + * Integration tier: it spawns the source CLI (`packages/cli/vitest-tiers.ts`). |
| 19 | + */ |
| 20 | + |
| 21 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 22 | +import { execFile } from 'node:child_process'; |
| 23 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 24 | +import { tmpdir } from 'node:os'; |
| 25 | +import { join, resolve } from 'node:path'; |
| 26 | +import { fileURLToPath } from 'node:url'; |
| 27 | +import { CONNECTOR_CREDENTIAL_LITERAL } from '@objectstack/lint'; |
| 28 | +import { childEnv } from './helpers/serve-process.js'; |
| 29 | +import { writeDefineStackConfig } from './helpers/define-stack-fixture.js'; |
| 30 | + |
| 31 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 32 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 33 | + |
| 34 | +const SENTINELS = [ |
| 35 | + 'cli-headers-sentinel', |
| 36 | + 'cli-default-headers-sentinel', |
| 37 | + 'cli-transport-headers-sentinel', |
| 38 | + 'cli-env-sentinel', |
| 39 | +] as const; |
| 40 | + |
| 41 | +const ACCEPT = { Accept: 'application/json' }; |
| 42 | + |
| 43 | +const connectorsOf = (literal: boolean) => [ |
| 44 | + { |
| 45 | + name: 'vendor_api', |
| 46 | + label: 'Vendor API', |
| 47 | + type: 'api', |
| 48 | + provider: 'rest', |
| 49 | + providerConfig: { |
| 50 | + baseUrl: 'https://api.vendor.invalid/v1', |
| 51 | + headers: literal ? { ...ACCEPT, Authorization: `Bearer ${SENTINELS[0]}` } : ACCEPT, |
| 52 | + defaultHeaders: literal ? { 'X-Vendor-Trace': `Bearer ${SENTINELS[1]}` } : ACCEPT, |
| 53 | + }, |
| 54 | + ...(literal ? {} : { auth: { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN' } }), |
| 55 | + }, |
| 56 | + { |
| 57 | + name: 'search_tools', |
| 58 | + label: 'Search Tools', |
| 59 | + type: 'api', |
| 60 | + provider: 'mcp', |
| 61 | + providerConfig: { |
| 62 | + transport: { |
| 63 | + kind: 'http', |
| 64 | + url: 'https://mcp.vendor.invalid/mcp', |
| 65 | + headers: literal ? { Authorization: `Bearer ${SENTINELS[2]}` } : ACCEPT, |
| 66 | + }, |
| 67 | + }, |
| 68 | + ...(literal ? {} : { auth: { type: 'bearer', credentialRef: 'SEARCH_TOOLS_TOKEN' } }), |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'local_tools', |
| 72 | + label: 'Local Tools', |
| 73 | + type: 'api', |
| 74 | + provider: 'mcp', |
| 75 | + providerConfig: { |
| 76 | + transport: { |
| 77 | + kind: 'stdio', |
| 78 | + command: 'local-mcp-server', |
| 79 | + env: literal ? { MCP_UPSTREAM: `Bearer ${SENTINELS[3]}` } : { LOG_LEVEL: 'info' }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | +]; |
| 84 | + |
| 85 | +const stackOf = (literal: boolean) => ({ |
| 86 | + manifest: { |
| 87 | + id: `com.example.connector.credential.${literal ? 'lit' : 'dark'}`, |
| 88 | + namespace: 'conncred', |
| 89 | + version: '1.0.0', |
| 90 | + type: 'app', |
| 91 | + name: 'Connector credential door probe', |
| 92 | + engines: { protocol: '^18' }, |
| 93 | + }, |
| 94 | + objects: [ |
| 95 | + { |
| 96 | + name: 'conncred_note', |
| 97 | + label: 'Note', |
| 98 | + pluralLabel: 'Notes', |
| 99 | + sharingModel: 'private', |
| 100 | + fields: { name: { type: 'text', label: 'Name', required: true } }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + connectors: connectorsOf(literal), |
| 104 | +}); |
| 105 | + |
| 106 | +const LIT_PATHS = [ |
| 107 | + 'connectors[0].providerConfig.headers.Authorization', |
| 108 | + 'connectors[0].providerConfig.defaultHeaders["X-Vendor-Trace"]', |
| 109 | + 'connectors[1].providerConfig.transport.headers.Authorization', |
| 110 | + 'connectors[2].providerConfig.transport.env.MCP_UPSTREAM', |
| 111 | +]; |
| 112 | + |
| 113 | +interface Run { |
| 114 | + code: number; |
| 115 | + stdout: string; |
| 116 | + stderr: string; |
| 117 | +} |
| 118 | + |
| 119 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 120 | + return new Promise((resolvePromise) => { |
| 121 | + execFile( |
| 122 | + process.execPath, |
| 123 | + [CLI, ...args], |
| 124 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 125 | + (err, stdout, stderr) => { |
| 126 | + resolvePromise({ |
| 127 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 128 | + stdout: String(stdout), |
| 129 | + stderr: String(stderr), |
| 130 | + }); |
| 131 | + }, |
| 132 | + ); |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +interface Finding { |
| 137 | + rule?: string; |
| 138 | + path?: string; |
| 139 | + severity?: string; |
| 140 | + message?: string; |
| 141 | +} |
| 142 | + |
| 143 | +/** `os validate --json`: registry advisories ride `warnings` as finding objects. */ |
| 144 | +const validateFindings = (stdout: string): Finding[] => |
| 145 | + ((JSON.parse(stdout) as { warnings?: unknown[] }).warnings ?? []).filter( |
| 146 | + (w): w is Finding => !!w && typeof w === 'object' && (w as Finding).rule === CONNECTOR_CREDENTIAL_LITERAL, |
| 147 | + ); |
| 148 | + |
| 149 | +/** `os lint --json`: every finding is an `issues` entry. */ |
| 150 | +const lintFindings = (stdout: string): Finding[] => |
| 151 | + ((JSON.parse(stdout) as { issues?: Finding[] }).issues ?? []).filter((i) => i.rule === CONNECTOR_CREDENTIAL_LITERAL); |
| 152 | + |
| 153 | +const dirs = new Map<'lit' | 'dark', string>(); |
| 154 | + |
| 155 | +beforeAll(() => { |
| 156 | + for (const kind of ['lit', 'dark'] as const) { |
| 157 | + const dir = mkdtempSync(join(tmpdir(), `os-connector-credential-${kind}-`)); |
| 158 | + writeDefineStackConfig(dir, stackOf(kind === 'lit')); |
| 159 | + dirs.set(kind, dir); |
| 160 | + } |
| 161 | +}); |
| 162 | + |
| 163 | +afterAll(() => { |
| 164 | + for (const dir of dirs.values()) rmSync(dir, { recursive: true, force: true }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe('connector-credential-literal at the CLI doors', () => { |
| 168 | + it('os validate — LIT prints one warning per literal and still passes; DARK prints none', async () => { |
| 169 | + const lit = await runCli(['validate', '--json'], dirs.get('lit')!); |
| 170 | + expect(lit.code, `validate failed:\n${lit.stdout}\n${lit.stderr}`).toBe(0); |
| 171 | + expect((JSON.parse(lit.stdout) as { valid?: boolean }).valid).toBe(true); |
| 172 | + const found = validateFindings(lit.stdout); |
| 173 | + expect(found.map((f) => f.path)).toEqual(LIT_PATHS); |
| 174 | + expect(found.every((f) => f.severity === 'warning')).toBe(true); |
| 175 | + // The route is in the message, which is what the text face prints. |
| 176 | + expect(found.every((f) => (f.message ?? '').includes('auth.credentialRef'))).toBe(true); |
| 177 | + for (const sentinel of SENTINELS) expect(JSON.stringify(found).includes(sentinel), sentinel).toBe(false); |
| 178 | + |
| 179 | + const dark = await runCli(['validate', '--json'], dirs.get('dark')!); |
| 180 | + expect(dark.code, `validate failed:\n${dark.stdout}\n${dark.stderr}`).toBe(0); |
| 181 | + expect(validateFindings(dark.stdout)).toEqual([]); |
| 182 | + }, 180_000); |
| 183 | + |
| 184 | + it('os lint — LIT reports one warning per literal and still passes; DARK reports none', async () => { |
| 185 | + const lit = await runCli(['lint', '--json'], dirs.get('lit')!); |
| 186 | + expect(lit.code, `lint failed:\n${lit.stdout}\n${lit.stderr}`).toBe(0); |
| 187 | + expect((JSON.parse(lit.stdout) as { passed?: boolean }).passed).toBe(true); |
| 188 | + const found = lintFindings(lit.stdout); |
| 189 | + expect(found.map((f) => f.path)).toEqual(LIT_PATHS); |
| 190 | + expect(found.every((f) => f.severity === 'warning')).toBe(true); |
| 191 | + expect(found.every((f) => (f.message ?? '').includes('auth.credentialRef'))).toBe(true); |
| 192 | + for (const sentinel of SENTINELS) expect(JSON.stringify(found).includes(sentinel), sentinel).toBe(false); |
| 193 | + |
| 194 | + const dark = await runCli(['lint', '--json'], dirs.get('dark')!); |
| 195 | + expect(dark.code, `lint failed:\n${dark.stdout}\n${dark.stderr}`).toBe(0); |
| 196 | + expect(lintFindings(dark.stdout)).toEqual([]); |
| 197 | + }, 180_000); |
| 198 | +}); |
0 commit comments