// 설명 문서: docs/specs/host-security-guard.md (v1.0.0) // 실제 spawn 직전에 사용하는 공통 계약. 엄격 모드가 미지원인 경로는 호스트 실행으로 // 되돌아가지 않는다. 현재 실행의 보호와 새 실행에 적용할 설정은 구분한다. const { scrubAgentEnvironment, MODEL_KEYS, RUNTIME_KEYS } = require('./agent-environment'); function policy() { const value = require('./host-security-policy').getHostSecurityPolicy(); if (!value.valid) throw Object.assign(new Error('호스트 보안 설정을 확인할 수 없어 실행을 중단했습니다.'), { code: 'HOST_SECURITY_POLICY_INVALID' }); return value; } function assertHostPathSupported(kind) { const value = policy(); // 서버의 listen·OAuth·외부 모델 연결은 network deny와 호환되지 않는다. // 승인된 모델 통신 broker가 준비되기 전에는 엄격 모드에서 실행하지 않는다. // PTY는 자연 종료 후 독립 자손의 종료 영수증이 없어 엄격 모드에서 아직 지원하지 않는다. if (value.mode === 'strict-local' && ['gui-server', 'native-cloud-chat', 'uncontained-task', 'pty'].includes(kind)) { throw Object.assign(new Error('이 실행 경로는 엄격 로컬 격리를 지원하지 않아 차단했습니다.'), { code: 'HOST_ISOLATION_UNAVAILABLE' }); } return value; } async function prepareHostLaunch({ command, args = [], cwd, env = {}, kind = 'cli' }) { const selected = assertHostPathSupported(kind); let launch; try { launch = selected.mode === 'strict-local' ? await require('./host-containment').prepareContainedLaunch({ command, args, cwd, env, network: 'deny' }) : { command, args, cwd, env: scrubAgentEnvironment(env, { trustedKeys: [...MODEL_KEYS, ...RUNTIME_KEYS] }), dispose() {}, receipt: { enforced: false, mode: 'standard' } }; const assertCurrent = () => { const current = policy(); if (current.mode !== selected.mode || current.revision !== selected.revision) { throw Object.assign(new Error('실행 준비 중 보안 설정이 변경되어 실행을 중단했습니다.'), { code: 'HOST_SECURITY_POLICY_CHANGED' }); } }; assertCurrent(); return { ...launch, mode: selected.mode, assertCurrent }; } catch (error) { try { await launch?.dispose(); } catch { /* 원래 격리 실패를 보존한다. */ } try { require('../utils/logger').makeLogger('host-security').warn('HOST_LAUNCH_REJECTED', { code: /^[A-Z_]{1,80}$/.test(error.code || '') ? error.code : 'HOST_LAUNCH_FAILED' }); } catch { /* 자격/명령 원문을 기록하지 않는다. */ } throw error; } } module.exports = { prepareHostLaunch, assertHostPathSupported };