11import { execFileSync , spawnSync } from 'node:child_process' ;
22import { randomUUID } from 'node:crypto' ;
33import { lstatSync , realpathSync } from 'node:fs' ;
4- import type { ContainerProfile , TaskFilesystems } from './profile.ts' ;
4+ import { assertContainerProfile , type ContainerProfile , type TaskFilesystems } from './profile.ts' ;
55import { BASE_IMAGE , CLAUDE_VERSION , CODEX_VERSION } from './image.ts' ;
66
77const dockerEnvironment = ( secrets : Readonly < Record < string , string > > = { } ) => ( {
@@ -30,6 +30,8 @@ const createDeadline = (timeoutMs: number) => {
3030 } ;
3131} ;
3232const resourceName = ( kind : string ) => `codeboost-${ kind } -${ randomUUID ( ) } ` ;
33+ const exactNoNewPrivileges = ( options : string [ ] | null | undefined ) => options ?. length === 1
34+ && ( options [ 0 ] === 'no-new-privileges' || options [ 0 ] === 'no-new-privileges:true' ) ;
3335const canonicalDockerBindSource = ( source : string ) => {
3436 const desktopHostPath = source . startsWith ( '/host_mnt/' ) ? source . slice ( '/host_mnt' . length ) : source ;
3537 try { return realpathSync ( desktopHostPath ) ; } catch { return source ; }
@@ -82,6 +84,7 @@ export function prepareTaskFilesystems(stagingDirectory: string, limits: TaskSto
8284 '--mount' , `type=volume,source=${ workVolume } ,target=/work` ,
8385 '--mount' , `type=volume,source=${ metadataVolume } ,target=/metadata` ,
8486 '--entrypoint' , 'sh' , imageId , '-c' , seed ] , { timeoutMs : remaining ( ) } ) ;
87+ remaining ( ) ;
8588 return Object . freeze ( { keeper, workVolume, metadataVolume, ...limits } ) ;
8689 } catch ( error ) {
8790 spawnSync ( 'docker' , [ 'rm' , '--force' , keeper ] , { env : dockerEnvironment ( ) , stdio : 'ignore' } ) ;
@@ -104,10 +107,11 @@ type Inspect = {
104107/** Validate daemon-resolved configuration before starting an agent. */
105108export function validateContainer ( container : string , profile : ContainerProfile , timeoutMs = 30_000 ) : void {
106109 const remaining = createDeadline ( timeoutMs ) ;
110+ assertContainerProfile ( profile ) ;
107111 const inspect = JSON . parse ( docker ( [ 'container' , 'inspect' , container ] , { timeoutMs : remaining ( ) } ) ) [ 0 ] as Inspect | undefined ;
108112 if ( ! inspect ) throw new Error ( 'Docker did not return the created container.' ) ;
109113 const image = JSON . parse ( docker ( [ 'image' , 'inspect' , profile . expectedImage ] , { timeoutMs : remaining ( ) } ) ) [ 0 ] as
110- { Id ?: string ; Config ?: { User ?: string ; Entrypoint ?: string [ ] ; Labels ?: Record < string , string > } } | undefined ;
114+ { Id ?: string ; Config ?: { User ?: string ; Env ?: string [ ] ; Entrypoint ?: string [ ] ; Labels ?: Record < string , string > } } | undefined ;
111115 const imageId = image ?. Id , labels = image ?. Config ?. Labels ?? { } ;
112116 const host = inspect . HostConfig ;
113117 if ( ! imageId || imageId !== profile . expectedImage || inspect . Image !== profile . expectedImage
@@ -124,7 +128,7 @@ export function validateContainer(container: string, profile: ContainerProfile,
124128 || JSON . stringify ( inspect . Config . Cmd ) !== JSON . stringify ( profile . command )
125129 || ! host . ReadonlyRootfs || host . Privileged
126130 || ! host . CapDrop ?. map ( value => value . toUpperCase ( ) ) . includes ( 'ALL' )
127- || ! host . SecurityOpt ?. some ( value => value . startsWith ( 'no-new-privileges' ) )
131+ || ! exactNoNewPrivileges ( host . SecurityOpt )
128132 || host . NetworkMode !== 'none' || host . PidMode !== '' || host . IpcMode !== 'private'
129133 || ( host . Devices ?. length ?? 0 ) !== 0 || ( host . DeviceRequests ?. length ?? 0 ) !== 0 || host . PidsLimit !== 128
130134 || host . Memory !== 512 * 1024 * 1024 || host . NanoCpus !== 1_000_000_000 )
@@ -183,7 +187,7 @@ export function validateContainer(container: string, profile: ContainerProfile,
183187 || keeper . Config ?. Labels ?. [ 'io.codeboost.task-storage' ] !== 'keeper' || ! keeper . HostConfig ?. ReadonlyRootfs
184188 || keeper . HostConfig . Privileged || keeper . HostConfig . NetworkMode !== 'none'
185189 || ! keeper . HostConfig . CapDrop ?. map ( value => value . toUpperCase ( ) ) . includes ( 'ALL' )
186- || ! keeper . HostConfig . SecurityOpt ?. some ( value => value . startsWith ( 'no-new-privileges' ) )
190+ || ! exactNoNewPrivileges ( keeper . HostConfig . SecurityOpt )
187191 || keeperVolumes . get ( '/work' ) ?. Name !== profile . filesystems . workVolume
188192 || keeperVolumes . get ( '/metadata' ) ?. Name !== profile . filesystems . metadataVolume )
189193 throw new Error ( 'Task filesystems must remain owned by their trusted keeper.' ) ;
@@ -197,12 +201,14 @@ export function validateContainer(container: string, profile: ContainerProfile,
197201 if ( inspect . Config . Env . some ( value => value . indexOf ( '=' ) < 1 ) ) throw new Error ( 'Container environment is malformed.' ) ;
198202 const names = inspect . Config . Env . map ( value => value . slice ( 0 , value . indexOf ( '=' ) ) ) ;
199203 const environment = new Map ( inspect . Config . Env . map ( value => [ value . slice ( 0 , value . indexOf ( '=' ) ) , value . slice ( value . indexOf ( '=' ) + 1 ) ] ) ) ;
204+ const imageEnvironment = new Map ( ( image ?. Config ?. Env ?? [ ] ) . map ( value => [ value . slice ( 0 , value . indexOf ( '=' ) ) , value . slice ( value . indexOf ( '=' ) + 1 ) ] ) ) ;
200205 const allowedEnvironment = new Set ( [ 'PATH' , 'NODE_VERSION' , 'YARN_VERSION' , 'HOME' , 'CODEBOOST_PHASE' , 'CODEBOOST_VENDOR' ,
201206 'CODEBOOST_WORK_BYTES' , 'CODEBOOST_WORK_INODES' , 'CODEBOOST_METADATA_BYTES' , 'CODEBOOST_METADATA_INODES' ,
202207 'npm_config_cache' , 'XDG_CACHE_HOME' , ...( profile . vendor === 'codex' ? [ 'CODEX_HOME' ] : [ 'CLAUDE_CODE_OAUTH_TOKEN' ] ) ] ) ;
203208 if ( new Set ( names ) . size !== names . length || names . some ( name => ! allowedEnvironment . has ( name ) ) )
204209 throw new Error ( 'Container includes an unexpected environment variable.' ) ;
205- if ( environment . get ( 'HOME' ) !== '/home/codeboost' || environment . get ( 'CODEBOOST_PHASE' ) !== profile . phase
210+ if ( environment . get ( 'PATH' ) !== imageEnvironment . get ( 'PATH' )
211+ || environment . get ( 'HOME' ) !== '/home/codeboost' || environment . get ( 'CODEBOOST_PHASE' ) !== profile . phase
206212 || environment . get ( 'CODEBOOST_VENDOR' ) !== profile . vendor
207213 || environment . get ( 'CODEBOOST_WORK_BYTES' ) !== String ( profile . filesystems . workBytes )
208214 || environment . get ( 'CODEBOOST_WORK_INODES' ) !== String ( profile . filesystems . workInodes )
@@ -212,15 +218,20 @@ export function validateContainer(container: string, profile: ContainerProfile,
212218 if ( profile . vendor === 'codex' && names . includes ( 'CLAUDE_CODE_OAUTH_TOKEN' ) ) throw new Error ( 'Credential profiles must not be combined.' ) ;
213219 if ( profile . vendor === 'claude' && ( names . includes ( 'CODEX_HOME' ) || ! names . includes ( 'CLAUDE_CODE_OAUTH_TOKEN' ) ) )
214220 throw new Error ( 'Credential profiles must not be combined.' ) ;
221+ assertContainerProfile ( profile ) ;
222+ remaining ( ) ;
215223}
216224
217225export function createValidatedContainer ( profile : ContainerProfile , timeoutMs = 30_000 ,
218226 secrets : Readonly < Record < string , string > > = { } ) : string {
219227 const remaining = createDeadline ( timeoutMs ) ;
220228 validateSecrets ( profile , secrets ) ;
229+ assertContainerProfile ( profile ) ;
221230 try {
222231 docker ( profile . args , { timeoutMs : remaining ( ) , secrets } ) ;
223232 validateContainer ( profile . name , profile , remaining ( ) ) ;
233+ assertContainerProfile ( profile ) ;
234+ remaining ( ) ;
224235 return profile . name ;
225236 } catch ( error ) {
226237 spawnSync ( 'docker' , [ 'rm' , '--force' , profile . name ] , { timeout : 30_000 , env : dockerEnvironment ( ) , stdio : 'ignore' } ) ;
@@ -232,7 +243,12 @@ export function runContainer(profile: ContainerProfile, timeoutMs = 60_000,
232243 secrets : Readonly < Record < string , string > > = { } ) : string {
233244 const remaining = createDeadline ( timeoutMs ) ;
234245 const container = createValidatedContainer ( profile , remaining ( ) , secrets ) ;
235- try { return docker ( [ 'start' , '--attach' , container ] , { timeoutMs : remaining ( ) , secrets } ) ; }
246+ try {
247+ assertContainerProfile ( profile ) ;
248+ const output = docker ( [ 'start' , '--attach' , container ] , { timeoutMs : remaining ( ) , secrets } ) ;
249+ remaining ( ) ;
250+ return output ;
251+ }
236252 finally { spawnSync ( 'docker' , [ 'rm' , '--force' , container ] , { timeout : 30_000 , env : dockerEnvironment ( ) , stdio : 'ignore' } ) ; }
237253}
238254
0 commit comments