| 1 | 'use strict'; |
| 2 | |
| 3 | |
| 4 | |
| 5 | const cp = require('node:child_process'); |
| 6 | const fs = require('node:fs'); |
| 7 | const path = require('node:path'); |
| 8 | const crypto = require('node:crypto'); |
| 9 | |
| 10 | const uuid = /^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i; |
| 11 | const profile = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/; |
| 12 | function readObject(filename) { |
| 13 | const info = fs.lstatSync(filename); |
| 14 | if (!info.isFile() || info.isSymbolicLink() || info.size > 1 << 20) throw new Error('ccs_launch_file_invalid'); |
| 15 | const value = JSON.parse(fs.readFileSync(filename, 'utf8')); |
| 16 | if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('ccs_launch_file_invalid'); |
| 17 | return value; |
| 18 | } |
| 19 | function samePath(a, b) { |
| 20 | const normalize = value => process.platform === 'win32' ? path.resolve(value).toLowerCase() : path.resolve(value); |
| 21 | return typeof a === 'string' && typeof b === 'string' && normalize(a) === normalize(b); |
| 22 | } |
| 23 | function settingsSource(args) { |
| 24 | let source = ''; |
| 25 | for (let i = 0; i < args.length; i++) { |
| 26 | if (args[i] === '--settings') { if (typeof args[++i] !== 'string' || source) return ''; source = args[i]; } |
| 27 | else if (args[i].startsWith('--settings=')) { if (source) return ''; source = args[i].slice(11); } |
| 28 | } |
| 29 | return source && !source.trimStart().startsWith('{') && path.isAbsolute(source) ? path.resolve(source) : ''; |
| 30 | } |
| 31 | function install(context, originalSpawn = cp.spawn) { |
| 32 | const previousSpawn = cp.spawn; |
| 33 | if (!uuid.test(context.id) || !uuid.test(context.session_id) || !profile.test(context.profile)) throw new Error('ccs_launch_context_invalid'); |
| 34 | const folder = path.resolve(context.folder); |
| 35 | if (fs.lstatSync(folder).isSymbolicLink()) throw new Error('ccs_launch_directory_invalid'); |
| 36 | let captured = false; |
| 37 | cp.spawn = function (command, args, options) { |
| 38 | if (!samePath(command, context.binary) || !Array.isArray(args) || options?.shell || |
| 39 | !args.some((arg, index) => (arg === '--session-id' || arg === '--resume') && args[index + 1] === context.session_id)) { |
| 40 | return originalSpawn.apply(this, arguments); |
| 41 | } |
| 42 | if (captured) throw new Error('ccs_duplicate_claude_spawn'); |
| 43 | captured = true; |
| 44 | if (args.length > 1024 || args.some(arg => typeof arg !== 'string' || arg.includes('\0'))) throw new Error('ccs_launch_arguments_invalid'); |
| 45 | const source = settingsSource(args); |
| 46 | const metadata = {id:context.id, session_id:context.session_id, profile:context.profile, |
| 47 | actual_args:args, settings_source:source, expected_settings_source:context.settings_source || '', |
| 48 | profile_type:options?.env?.CCS_PROFILE_TYPE || ''}; |
| 49 | const filename = path.join(folder, `ccs-actual-${crypto.randomUUID()}.json`); |
| 50 | if (Buffer.byteLength(JSON.stringify(metadata)) > 1 << 20) throw new Error('ccs_launch_arguments_limit'); |
| 51 | fs.writeFileSync(filename, JSON.stringify(metadata), {flag:'wx', mode:0o600}); |
| 52 | let child; |
| 53 | try { |
| 54 | child = originalSpawn(context.python, ['-X','utf8','-I',context.helper,'ccs-live',filename], |
| 55 | {...options, env:{...(options?.env || process.env), CAGE_CCS_ACTUAL:'1'}}); |
| 56 | } catch (error) { fs.unlinkSync(filename); throw error; } |
| 57 | const cleanup = () => { try { fs.unlinkSync(filename); } catch (error) { if (error.code !== 'ENOENT') throw error; } }; |
| 58 | child.once('exit', cleanup); child.once('error', cleanup); |
| 59 | return child; |
| 60 | }; |
| 61 | return () => { cp.spawn = previousSpawn; }; |
| 62 | } |
| 63 | |
| 64 | if (process.env.CAGE_CCS_LAUNCH_FILE) { |
| 65 | const filename = process.env.CAGE_CCS_LAUNCH_FILE; |
| 66 | const context = readObject(filename); |
| 67 | if (!samePath(path.dirname(filename), context.folder) || path.basename(filename) !== 'ccs-launch.json') throw new Error('ccs_launch_context_invalid'); |
| 68 | |
| 69 | |
| 70 | try { |
| 71 | const manager = require(path.join(path.dirname(context.main), 'utils', 'config-manager.js')); |
| 72 | const source = manager.getSettingsPath(context.profile); |
| 73 | if (typeof source === 'string' && path.isAbsolute(source)) context.settings_source = source; |
| 74 | } catch { context.settings_source = ''; } |
| 75 | install(context); |
| 76 | } |
| 77 | module.exports = {install, readObject, samePath, settingsSource}; |