@@ -3398,3 +3398,214 @@ describe("tool.shell wave O classification", () => {
33983398 120_000 ,
33993399 )
34003400} )
3401+
3402+ describe ( "tool.shell wave P classification" , ( ) => {
3403+ if ( process . platform === "win32" ) return
3404+
3405+ const requests = ( command : string , directory : string , stop : boolean ) =>
3406+ Effect . gen ( function * ( ) {
3407+ const list : Array < Omit < PermissionV1 . Request , "id" | "sessionID" | "tool" > > = [ ]
3408+ if ( stop ) {
3409+ yield * runIn ( directory , fail ( { command } , capture ( list , new Error ( "stop after permission" ) ) ) )
3410+ } else {
3411+ yield * runIn ( directory , run ( { command } , capture ( list ) ) )
3412+ }
3413+ return list
3414+ } )
3415+
3416+ const expectExternal = ( command : string , directory : string , stop = true ) =>
3417+ Effect . gen ( function * ( ) {
3418+ const list = yield * requests ( command , directory , stop )
3419+ expect ( { command, external : list . some ( ( item ) => item . permission === "external_directory" ) } ) . toEqual ( {
3420+ command,
3421+ external : true ,
3422+ } )
3423+ } )
3424+
3425+ const expectCleanStopped = ( command : string , directory : string ) =>
3426+ Effect . gen ( function * ( ) {
3427+ const list : Array < Omit < PermissionV1 . Request , "id" | "sessionID" | "tool" > > = [ ]
3428+ yield * runIn ( directory , run ( { command } , capture ( list , new Error ( "stop after permission" ) ) ) . pipe ( Effect . exit ) )
3429+ expect ( { command, external : list . some ( ( item ) => item . permission === "external_directory" ) } ) . toEqual ( {
3430+ command,
3431+ external : false ,
3432+ } )
3433+ } )
3434+
3435+ const expectNotAbsorbed = ( rule : string , command : string , directory : string ) =>
3436+ Effect . gen ( function * ( ) {
3437+ const list = yield * requests ( command , directory , true )
3438+ const approved = [ { permission : "bash" , pattern : rule } ]
3439+ const uncovered = list . filter (
3440+ ( req ) =>
3441+ ! req . patterns . every ( ( pattern ) =>
3442+ approved . some (
3443+ ( item ) =>
3444+ Wildcard . matchStrict ( req . permission , item . permission ) && Wildcard . matchStrict ( pattern , item . pattern ) ,
3445+ ) ,
3446+ ) ,
3447+ )
3448+ expect ( {
3449+ rule,
3450+ command,
3451+ external : uncovered . some ( ( item ) => item . permission === "external_directory" ) ,
3452+ } ) . toEqual ( { rule, command, external : true } )
3453+ } )
3454+
3455+ const expectNoAlways = ( command : string , directory : string ) =>
3456+ Effect . gen ( function * ( ) {
3457+ const list = yield * requests ( command , directory , false )
3458+ const bash = list . find ( ( item ) => item . permission === "bash" )
3459+ expect ( {
3460+ command,
3461+ external : list . some ( ( item ) => item . permission === "external_directory" ) ,
3462+ always : bash ?. always ?? [ ] ,
3463+ } ) . toEqual ( { command, external : true , always : [ ] } )
3464+ } )
3465+
3466+ it . live (
3467+ "wave P: scopes a function-body redirect destination with the fully-merged scope" ,
3468+ ( ) =>
3469+ Effect . gen ( function * ( ) {
3470+ const tmp = yield * tmpdirScoped ( )
3471+ yield * Effect . promise ( ( ) => Bun . write ( path . join ( tmp , "notes.txt" ) , "x" ) )
3472+ for ( const command of [
3473+ "x=notes.txt; f(){ cat < $x; }; x=/etc/hostname; f" ,
3474+ "x=notes.txt; f(){ echo hi > $x; }; x=/etc/hostname; f" ,
3475+ "x=notes.txt; f(){ cat >> $x; }; x=/etc/hostname; f" ,
3476+ "function f { cat < $x; }; x=/etc/hostname; f" ,
3477+ ] ) {
3478+ yield * expectExternal ( command , tmp )
3479+ }
3480+ yield * expectNotAbsorbed ( "f *" , "x=notes.txt; f(){ echo hi > $x; }; x=/etc/hostname; f" , tmp )
3481+ for ( const command of [
3482+ "x=notes.txt; f(){ cat < $x; }; f" ,
3483+ "f(){ echo hi > $x; }; x=dist/o.txt; f" ,
3484+ "x=notes.txt; f(){ echo hi > $x; }; f" ,
3485+ ] ) {
3486+ yield * expectCleanStopped ( command , tmp )
3487+ }
3488+ } ) ,
3489+ 120_000 ,
3490+ )
3491+
3492+ it . live (
3493+ "wave P: anchors an eval script when a dynamic-named command is dropped beside a classifiable one" ,
3494+ ( ) =>
3495+ Effect . gen ( function * ( ) {
3496+ const tmp = yield * tmpdirScoped ( )
3497+ yield * Effect . promise ( ( ) => Bun . write ( path . join ( tmp , "notes.txt" ) , "x" ) )
3498+ for ( const command of [
3499+ 'bash -c "$Q cat /etc/hostname; true"' ,
3500+ 'bash -c "$Q cat /etc/hostname; cat notes.txt"' ,
3501+ 'bash -c "true; $Q cat /etc/hostname"' ,
3502+ 'bash -c "$Q cat /etc/hostname && true"' ,
3503+ 'bash -c "$Q cat /etc/hostname; echo hi"' ,
3504+ 'sh -c "$Q cat /etc/hostname; true"' ,
3505+ 'eval "$Q cat /etc/hostname; true"' ,
3506+ 'bash -c "$Q cat /etc/hostname | cat"' ,
3507+ ] ) {
3508+ yield * expectExternal ( command , tmp )
3509+ }
3510+ yield * expectNoAlways ( 'bash -c "$Q cat /etc/hostname; true"' , tmp )
3511+ yield * expectNotAbsorbed ( "bash *" , 'bash -c "$Q cat /etc/hostname; true"' , tmp )
3512+ for ( const command of [
3513+ 'bash -c "echo hi; true"' ,
3514+ 'bash -c "set -e; npm ci; npm test"' ,
3515+ 'bash -c "cat notes.txt; true"' ,
3516+ ] ) {
3517+ yield * expectCleanStopped ( command , tmp )
3518+ }
3519+ } ) ,
3520+ 120_000 ,
3521+ )
3522+
3523+ it . live (
3524+ "wave P: strips quotes from a remote option name before inspecting it" ,
3525+ ( ) =>
3526+ Effect . gen ( function * ( ) {
3527+ const tmp = yield * tmpdirScoped ( )
3528+ for ( const command of [
3529+ 'docker "--tlscacert" ps cp /etc/hostname cid:/x' ,
3530+ "docker '--tlscacert' ps cp /etc/hostname cid:/x" ,
3531+ 'docker "--config=/etc/hostname" ps' ,
3532+ "docker '--config=/etc/hostname' ps" ,
3533+ 'podman "--connection" ps cp /etc/hostname cid:/x' ,
3534+ 'docker "--tlscacert=ps" cp /etc/hostname cid:/x' ,
3535+ ] ) {
3536+ yield * expectExternal ( command , tmp )
3537+ }
3538+ yield * expectNoAlways ( 'docker "--tlscacert" ps cp /etc/hostname cid:/x' , tmp )
3539+ yield * expectNotAbsorbed ( 'docker "--tlscacert" *' , 'docker "--tlscacert" ps cp /etc/hostname cid:/x' , tmp )
3540+ yield * expectNotAbsorbed ( 'docker "--config=/etc/hostname" *' , 'docker "--config=/etc/hostname" ps' , tmp )
3541+ for ( const command of [ 'docker "--log-level" debug ps' , 'docker "--context" default ps' , 'docker "--config" ~/.docker ps' ] ) {
3542+ yield * expectCleanStopped ( command , tmp )
3543+ }
3544+ } ) ,
3545+ 120_000 ,
3546+ )
3547+
3548+ it . live (
3549+ "wave P: scans the attached value of an unmodelled remote global option" ,
3550+ ( ) =>
3551+ Effect . gen ( function * ( ) {
3552+ const tmp = yield * tmpdirScoped ( )
3553+ for ( const command of [
3554+ "docker --tlscacert=/etc/hostname ps" ,
3555+ "docker --data-root=/etc/hostname ps" ,
3556+ "podman --root=/etc/hostname ps" ,
3557+ "docker --tlscacert=../etc/hostname ps" ,
3558+ ] ) {
3559+ yield * expectExternal ( command , tmp )
3560+ }
3561+ yield * expectNoAlways ( "docker --tlscacert=/etc/hostname ps" , tmp )
3562+ yield * expectNotAbsorbed ( "docker --tlscacert *" , "docker --tlscacert=/etc/hostname ps" , tmp )
3563+ yield * expectNotAbsorbed ( "docker --config *" , "docker --tlscacert=/etc/hostname ps" , tmp )
3564+ for ( const command of [
3565+ "docker --log-level=debug ps" ,
3566+ "docker --context=default ps" ,
3567+ "docker --host=tcp://127.0.0.1:2375 ps" ,
3568+ "docker --config=.docker ps" ,
3569+ ] ) {
3570+ yield * expectCleanStopped ( command , tmp )
3571+ }
3572+ } ) ,
3573+ 120_000 ,
3574+ )
3575+
3576+ it . live (
3577+ "wave P: bounds a self-referential expansion and fails closed" ,
3578+ ( ) =>
3579+ Effect . gen ( function * ( ) {
3580+ const tmp = yield * tmpdirScoped ( )
3581+ for ( const command of [
3582+ "A='$A$A$A$A$A$A$A$A'; cat $A" ,
3583+ "A='$A$A$A$A$A$A$A$A$A$A$A$A'; cat $A" ,
3584+ "bash -c \"A='$A$A$A$A$A$A$A$A'; cat \\$A\"" ,
3585+ ] ) {
3586+ yield * expectExternal ( command , tmp )
3587+ }
3588+ yield * expectExternal ( 'cat "${X:-${Y:-/etc/hostname}}"' , tmp )
3589+ } ) ,
3590+ 120_000 ,
3591+ )
3592+
3593+ it . live (
3594+ "wave P: resolves the alternate ${VAR:+word} expansion in an eval script" ,
3595+ ( ) =>
3596+ Effect . gen ( function * ( ) {
3597+ const tmp = yield * tmpdirScoped ( )
3598+ yield * Effect . promise ( ( ) => Bun . write ( path . join ( tmp , "notes.txt" ) , "x" ) )
3599+ for ( const command of [ 'bash -c "${PAGER:+cat} notes.txt"' , 'bash -c "${X:+echo hi}"' ] ) {
3600+ yield * expectCleanStopped ( command , tmp )
3601+ }
3602+ for ( const command of [
3603+ 'bash -c "${X:+cat /etc/hostname}"' ,
3604+ 'X=1; bash -c "${X:+cat /etc/hostname}"' ,
3605+ ] ) {
3606+ yield * expectExternal ( command , tmp )
3607+ }
3608+ } ) ,
3609+ 120_000 ,
3610+ )
3611+ } )
0 commit comments