@@ -10,25 +10,79 @@ const startup = spec.match(/ - phase: startup\n user: "0"\n description:
1010assert . ok ( startup , "scratch directory preparation must be wired as a root startup command" )
1111const body = startup [ 1 ] . replace ( / ^ / gm, "" )
1212
13- async function fixture ( t ) {
13+ const idShim = `#!/bin/sh
14+ [ -n "$FAKE_UID" ] || exit 1
15+ [ "$2" = agent ] || exit 1
16+ case "$1" in -u) echo "$FAKE_UID" ;; -g) echo "$FAKE_GID" ;; *) exit 1 ;; esac
17+ `
18+
19+ // Records the requested ownership, then installs with the caller's real ids
20+ // (the test cannot chown to arbitrary ids without root).
21+ const installShim = `#!/bin/sh
22+ echo "$*" >> "$INSTALL_LOG"
23+ n=$#
24+ while [ "$n" -gt 0 ]; do
25+ a=$1; shift; n=$((n - 1))
26+ case "$a" in
27+ -o) shift; n=$((n - 1)); set -- "$@" -o "$REAL_UID" ;;
28+ -g) shift; n=$((n - 1)); set -- "$@" -g "$REAL_GID" ;;
29+ *) set -- "$@" "$a" ;;
30+ esac
31+ done
32+ exec /usr/bin/install "$@"
33+ `
34+
35+ async function fixture ( t , { agent = { uid : process . getuid ( ) , gid : process . getgid ( ) } } = { } ) {
1436 const root = await mkdtemp ( path . join ( os . tmpdir ( ) , "opencode-tmp-test-" ) )
1537 t . after ( ( ) => rm ( root , { recursive : true , force : true } ) )
1638 const directory = path . join ( root , "opencode" )
39+ const bin = path . join ( root , "bin" )
40+ const installLog = path . join ( root , "install.log" )
41+ await mkdir ( bin )
42+ await writeFile ( path . join ( bin , "id" ) , idShim , { mode : 0o755 } )
43+ await writeFile ( path . join ( bin , "install" ) , installShim , { mode : 0o755 } )
1744 // Exercise real install/chmod without requiring root or touching /tmp/opencode.
1845 const command = body . replaceAll ( "/tmp/opencode" , directory )
19- . replace ( "-o 1000 -g 1000" , `-o ${ process . getuid ( ) } -g ${ process . getgid ( ) } ` )
2046 const run = ( ) => spawnSync ( "/bin/sh" , [ "-c" , command ] , {
21- env : { PATH : "/usr/bin:/bin" } , encoding : "utf8" ,
47+ env : {
48+ PATH : `${ bin } :/usr/bin:/bin` ,
49+ INSTALL_LOG : installLog ,
50+ REAL_UID : String ( process . getuid ( ) ) ,
51+ REAL_GID : String ( process . getgid ( ) ) ,
52+ ...( agent ? { FAKE_UID : String ( agent . uid ) , FAKE_GID : String ( agent . gid ) } : { } ) ,
53+ } ,
54+ encoding : "utf8" ,
2255 } )
23- return { root, directory, run }
56+ const installCalls = async ( ) => readFile ( installLog , "utf8" ) . catch ( ( ) => "" )
57+ return { root, directory, run, installCalls }
2458}
2559
26- test ( "root startup assigns the sandbox agent's numeric UID/GID without recursive changes" , ( ) => {
27- assert . match ( body , / i n s t a l l - d - o 1 0 0 0 - g 1 0 0 0 - m 0 7 0 0 \/ t m p \/ o p e n c o d e / )
60+ test ( "root startup resolves the agent's UID/GID by name without recursive changes" , ( ) => {
61+ assert . match ( body , / i d - u a g e n t / )
62+ assert . match ( body , / i d - g a g e n t / )
63+ assert . match ( body , / i n s t a l l - d - o " \$ a g e n t _ u i d " - g " \$ a g e n t _ g i d " - m 0 7 0 0 \/ t m p \/ o p e n c o d e / )
64+ assert . doesNotMatch ( body , / \b 1 0 0 0 \b / )
2865 assert . doesNotMatch ( body , / (?: c h o w n | c h m o d ) \s + - R / )
2966 assert . ok ( spec . indexOf ( startup [ 0 ] ) < spec . indexOf ( "description: Render current provider inputs" ) )
3067} )
3168
69+ test ( "startup uses a non-1000 agent uid/gid reported by the sandbox" , async ( t ) => {
70+ const { directory, run, installCalls } = await fixture ( t , { agent : { uid : 2345 , gid : 2346 } } )
71+ const result = run ( )
72+ assert . equal ( result . status , 0 , result . stderr )
73+ assert . match ( await installCalls ( ) , / - d - o 2 3 4 5 - g 2 3 4 6 - m 0 7 0 0 \S + \/ o p e n c o d e / )
74+ assert . ok ( ( await stat ( directory ) ) . isDirectory ( ) )
75+ } )
76+
77+ test ( "startup fails closed when the agent user does not exist" , async ( t ) => {
78+ const { directory, run, installCalls } = await fixture ( t , { agent : null } )
79+ const result = run ( )
80+ assert . notEqual ( result . status , 0 )
81+ assert . match ( result . stderr , / u s e r ' a g e n t ' n o t f o u n d / )
82+ assert . equal ( await installCalls ( ) , "" )
83+ await assert . rejects ( stat ( directory ) )
84+ } )
85+
3286test ( "startup creates a private writable directory and is idempotent" , async ( t ) => {
3387 const { directory, run } = await fixture ( t )
3488 for ( let attempt = 0 ; attempt < 2 ; attempt ++ ) {
0 commit comments