|
| 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 `flow-credential-literal` |
| 5 | + * 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. The |
| 10 | + * measured starting point was silence at both doors: a flow carrying a |
| 11 | + * credential-shaped literal in an `http` node's headers and url and in a |
| 12 | + * connector node's input validated clean and linted clean. |
| 13 | + * |
| 14 | + * Two fixtures, one flow each: LIT carries three literals (a header, a url |
| 15 | + * query parameter, a nested connector input); DARK is the same flow with |
| 16 | + * `{var}` templates in those three positions, and must stay silent. Every |
| 17 | + * value is a probe sentinel, not a credential. |
| 18 | + * |
| 19 | + * Integration tier: it spawns the source CLI (`packages/cli/vitest-tiers.ts`). |
| 20 | + */ |
| 21 | + |
| 22 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 23 | +import { execFile } from 'node:child_process'; |
| 24 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 25 | +import { tmpdir } from 'node:os'; |
| 26 | +import { join, resolve } from 'node:path'; |
| 27 | +import { fileURLToPath } from 'node:url'; |
| 28 | +import { FLOW_CREDENTIAL_LITERAL } from '@objectstack/lint'; |
| 29 | +import { childEnv } from './helpers/serve-process.js'; |
| 30 | +import { writeDefineStackConfig } from './helpers/define-stack-fixture.js'; |
| 31 | + |
| 32 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 33 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 34 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 35 | + |
| 36 | +const SENTINELS = ['cli-header-sentinel', 'cli-url-sentinel', 'cli-input-sentinel'] as const; |
| 37 | + |
| 38 | +const stackOf = (literal: boolean) => ({ |
| 39 | + manifest: { |
| 40 | + id: `com.example.credential.${literal ? 'lit' : 'dark'}`, |
| 41 | + namespace: 'cred', |
| 42 | + version: '1.0.0', |
| 43 | + type: 'app', |
| 44 | + name: 'Credential door probe', |
| 45 | + engines: { protocol: '^17' }, |
| 46 | + }, |
| 47 | + objects: [ |
| 48 | + { |
| 49 | + name: 'cred_note', |
| 50 | + label: 'Note', |
| 51 | + pluralLabel: 'Notes', |
| 52 | + sharingModel: 'private', |
| 53 | + fields: { name: { type: 'text', label: 'Name', required: true } }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + flows: [ |
| 57 | + { |
| 58 | + name: 'cred_probe', |
| 59 | + label: 'Credential probe', |
| 60 | + type: 'autolaunched', |
| 61 | + variables: [{ name: 'api_token', type: 'text', isInput: true }], |
| 62 | + nodes: [ |
| 63 | + { id: 'start', type: 'start', label: 'Start' }, |
| 64 | + { |
| 65 | + id: 'call', |
| 66 | + type: 'http', |
| 67 | + label: 'Call', |
| 68 | + config: { |
| 69 | + url: literal |
| 70 | + ? `https://example.invalid/hook?api_key=${SENTINELS[1]}` |
| 71 | + : 'https://example.invalid/hook?api_key={api_token}', |
| 72 | + method: 'POST', |
| 73 | + headers: { |
| 74 | + Authorization: literal ? `Bearer ${SENTINELS[0]}` : 'Bearer {api_token}', |
| 75 | + 'X-Trace-Label': 'plain-value', |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + id: 'conn', |
| 81 | + type: 'connector_action', |
| 82 | + label: 'Connector', |
| 83 | + connectorConfig: { |
| 84 | + connectorId: 'rest', |
| 85 | + actionId: 'request', |
| 86 | + input: { method: 'GET', path: '/p', auth: { clientSecret: literal ? SENTINELS[2] : '{api_token}' } }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { id: 'end', type: 'end', label: 'End' }, |
| 90 | + ], |
| 91 | + edges: [ |
| 92 | + { id: 'e1', source: 'start', target: 'call' }, |
| 93 | + { id: 'e2', source: 'call', target: 'conn' }, |
| 94 | + { id: 'e3', source: 'conn', target: 'end' }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + ], |
| 98 | +}); |
| 99 | + |
| 100 | +const LIT_PATHS = [ |
| 101 | + 'flows[0].nodes[1].config.headers.Authorization', |
| 102 | + 'flows[0].nodes[1].config.url', |
| 103 | + 'flows[0].nodes[2].connectorConfig.input.auth.clientSecret', |
| 104 | +]; |
| 105 | + |
| 106 | +interface Run { |
| 107 | + code: number; |
| 108 | + stdout: string; |
| 109 | + stderr: string; |
| 110 | +} |
| 111 | + |
| 112 | +function runCli(args: string[], cwd: string): Promise<Run> { |
| 113 | + return new Promise((resolvePromise) => { |
| 114 | + execFile( |
| 115 | + TSX, |
| 116 | + [CLI, ...args], |
| 117 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 118 | + (err, stdout, stderr) => { |
| 119 | + resolvePromise({ |
| 120 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 121 | + stdout: String(stdout), |
| 122 | + stderr: String(stderr), |
| 123 | + }); |
| 124 | + }, |
| 125 | + ); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +interface Finding { |
| 130 | + rule?: string; |
| 131 | + path?: string; |
| 132 | + severity?: string; |
| 133 | + message?: string; |
| 134 | +} |
| 135 | + |
| 136 | +/** `os validate --json`: registry advisories ride `warnings` as finding objects. */ |
| 137 | +const validateFindings = (stdout: string): Finding[] => |
| 138 | + ((JSON.parse(stdout) as { warnings?: unknown[] }).warnings ?? []).filter( |
| 139 | + (w): w is Finding => !!w && typeof w === 'object' && (w as Finding).rule === FLOW_CREDENTIAL_LITERAL, |
| 140 | + ); |
| 141 | + |
| 142 | +/** `os lint --json`: every finding is an `issues` entry. */ |
| 143 | +const lintFindings = (stdout: string): Finding[] => |
| 144 | + ((JSON.parse(stdout) as { issues?: Finding[] }).issues ?? []).filter((i) => i.rule === FLOW_CREDENTIAL_LITERAL); |
| 145 | + |
| 146 | +const dirs = new Map<'lit' | 'dark', string>(); |
| 147 | + |
| 148 | +beforeAll(() => { |
| 149 | + for (const kind of ['lit', 'dark'] as const) { |
| 150 | + const dir = mkdtempSync(join(tmpdir(), `os-flow-credential-${kind}-`)); |
| 151 | + writeDefineStackConfig(dir, stackOf(kind === 'lit')); |
| 152 | + dirs.set(kind, dir); |
| 153 | + } |
| 154 | +}); |
| 155 | + |
| 156 | +afterAll(() => { |
| 157 | + for (const dir of dirs.values()) rmSync(dir, { recursive: true, force: true }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe('flow-credential-literal at the CLI doors', () => { |
| 161 | + it('os validate — LIT prints one warning per literal and still passes; DARK prints none', async () => { |
| 162 | + const lit = await runCli(['validate', '--json'], dirs.get('lit')!); |
| 163 | + expect(lit.code, `validate failed:\n${lit.stdout}\n${lit.stderr}`).toBe(0); |
| 164 | + expect((JSON.parse(lit.stdout) as { valid?: boolean }).valid).toBe(true); |
| 165 | + const found = validateFindings(lit.stdout); |
| 166 | + expect(found.map((f) => f.path)).toEqual(LIT_PATHS); |
| 167 | + expect(found.every((f) => f.severity === 'warning')).toBe(true); |
| 168 | + // The route is in the message, which is what the text face prints. |
| 169 | + expect(found.every((f) => (f.message ?? '').includes('auth.credentialRef'))).toBe(true); |
| 170 | + for (const sentinel of SENTINELS) expect(JSON.stringify(found).includes(sentinel), sentinel).toBe(false); |
| 171 | + |
| 172 | + const dark = await runCli(['validate', '--json'], dirs.get('dark')!); |
| 173 | + expect(dark.code, `validate failed:\n${dark.stdout}\n${dark.stderr}`).toBe(0); |
| 174 | + expect(validateFindings(dark.stdout)).toEqual([]); |
| 175 | + }, 180_000); |
| 176 | + |
| 177 | + it('os lint — LIT reports one warning per literal and still passes; DARK reports none', async () => { |
| 178 | + const lit = await runCli(['lint', '--json'], dirs.get('lit')!); |
| 179 | + expect(lit.code, `lint failed:\n${lit.stdout}\n${lit.stderr}`).toBe(0); |
| 180 | + expect((JSON.parse(lit.stdout) as { passed?: boolean }).passed).toBe(true); |
| 181 | + const found = lintFindings(lit.stdout); |
| 182 | + expect(found.map((f) => f.path)).toEqual(LIT_PATHS); |
| 183 | + expect(found.every((f) => f.severity === 'warning')).toBe(true); |
| 184 | + expect(found.every((f) => (f.message ?? '').includes('auth.credentialRef'))).toBe(true); |
| 185 | + for (const sentinel of SENTINELS) expect(JSON.stringify(found).includes(sentinel), sentinel).toBe(false); |
| 186 | + |
| 187 | + const dark = await runCli(['lint', '--json'], dirs.get('dark')!); |
| 188 | + expect(dark.code, `lint failed:\n${dark.stdout}\n${dark.stderr}`).toBe(0); |
| 189 | + expect(lintFindings(dark.stdout)).toEqual([]); |
| 190 | + }, 180_000); |
| 191 | +}); |
0 commit comments