1- import { spawn } from "node:child_process" ;
1+ import { spawn , spawnSync } from "node:child_process" ;
22// Qa Lab tests cover gateway child plugin behavior.
33import { EventEmitter , once } from "node:events" ;
44import { lstat , mkdir , readFile , readdir , rm , symlink , writeFile } from "node:fs/promises" ;
@@ -160,22 +160,32 @@ if (!recordPath || !configPath || !stateDir) {
160160 throw new Error("missing fixture environment");
161161}
162162const record = (value) => fs.appendFileSync(recordPath, JSON.stringify(value) + "\\n");
163+ const authDbPath = path.join(stateDir, "agents", "qa", "agent", "openclaw-agent.sqlite");
163164if (args[0] === "models") {
164165 let stdin = "";
165166 process.stdin.setEncoding("utf8");
166167 for await (const chunk of process.stdin) stdin += chunk;
167168 const provider = args[args.indexOf("--provider") + 1];
169+ const configStat = fs.lstatSync(configPath);
168170 record({
169171 kind: "auth",
170172 args,
171173 stdin,
172- dbExists: fs.existsSync(path.join(stateDir, "agents", "qa", "agent", "openclaw-agent.sqlite")),
174+ authDbPath,
175+ dbExists: fs.existsSync(authDbPath),
176+ configPath,
177+ configMode: configStat.mode & 0o777,
178+ configRegular: configStat.isFile(),
179+ configSymlink: configStat.isSymbolicLink(),
180+ stateDir,
173181 env: {
174182 OPENCLAW_CLI: process.env.OPENCLAW_CLI,
175183 OPENCLAW_CONFIG_PATH: configPath,
176184 OPENCLAW_STATE_DIR: stateDir,
177185 },
178186 });
187+ fs.mkdirSync(path.dirname(authDbPath), { recursive: true });
188+ fs.writeFileSync(authDbPath, "fixture auth");
179189 if (process.env.QA_FAIL_PROVIDER === provider) {
180190 process.stderr.write("Authorization: Bearer " + stdin.trim());
181191 process.exit(9);
@@ -186,7 +196,16 @@ if (args[0] === "models") {
186196 process.exit(0);
187197}
188198const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
189- record({ kind: "gateway", args, fixtureProfiles: config.fixtureProfiles });
199+ record({
200+ kind: "gateway",
201+ args,
202+ authDbPath,
203+ dbExists: fs.existsSync(authDbPath),
204+ configPath,
205+ authProfileIds: Object.keys(config.auth?.profiles ?? {}),
206+ fixtureProfiles: config.fixtureProfiles,
207+ stateDir,
208+ });
190209process.stderr.write("fixture gateway exit");
191210process.exit(17);
192211` ,
@@ -793,6 +812,7 @@ describe("buildQaRuntimeEnv", () => {
793812 OPENCLAW_QA_TELEGRAM_GROUP_ID : "-1001234567890" ,
794813 OPENCLAW_QA_TELEGRAM_DRIVER_BOT_TOKEN : "driver-token" ,
795814 OPENCLAW_QA_TELEGRAM_SUT_BOT_TOKEN : "sut-token" ,
815+ "BASH_FUNC_sudo%%" : "() { printf imported; }" ,
796816 } ,
797817 } ) ;
798818
@@ -804,8 +824,64 @@ describe("buildQaRuntimeEnv", () => {
804824 expect ( env . OPENCLAW_QA_TELEGRAM_GROUP_ID ) . toBeUndefined ( ) ;
805825 expect ( env . OPENCLAW_QA_TELEGRAM_DRIVER_BOT_TOKEN ) . toBeUndefined ( ) ;
806826 expect ( env . OPENCLAW_QA_TELEGRAM_SUT_BOT_TOKEN ) . toBeUndefined ( ) ;
827+ expect ( env [ "BASH_FUNC_sudo%%" ] ) . toBeUndefined ( ) ;
807828 } ) ;
808829
830+ it . runIf ( process . platform === "linux" ) (
831+ "scrubs inherited shell startup env before the workflow allowlist runs" ,
832+ async ( ) => {
833+ const tempRoot = await tempDirs . makeTempDir ( "qa-shell-startup-env-" ) ;
834+ const markerPath = path . join ( tempRoot , "bash-env-ran" ) ;
835+ const functionMarkerPath = path . join ( tempRoot , "bash-function-ran" ) ;
836+ const bashEnvPath = path . join ( tempRoot , "malicious-bash-env" ) ;
837+ const allowlistProbePath = path . join ( tempRoot , "allowlist-probe.sh" ) ;
838+ await writeFile ( bashEnvPath , `printf 'ran' > ${ JSON . stringify ( markerPath ) } \n` , "utf8" ) ;
839+ await writeFile (
840+ allowlistProbePath ,
841+ `
842+ set -Eeuo pipefail
843+ for key in BASH_ENV BASHOPTS ENV SHELLOPTS; do
844+ ! compgen -e | grep -Fxq "$key"
845+ done
846+ declare -A keep_env=([SAFE_VALUE]=1)
847+ while IFS= read -r key; do
848+ if [[ -z "\${keep_env[$key]+x}" ]]; then
849+ unset "$key"
850+ fi
851+ done < <(compgen -e)
852+ printf '%s' "\${SAFE_VALUE:?}"
853+ ` ,
854+ "utf8" ,
855+ ) ;
856+ const env = buildQaRuntimeEnv ( {
857+ ...createParams ( { SAFE_VALUE : "base" } ) ,
858+ runtimeEnvPatch : {
859+ SAFE_VALUE : "allowlist-survived" ,
860+ BASH_ENV : bashEnvPath ,
861+ BASHOPTS : "checkwinsize" ,
862+ ENV : bashEnvPath ,
863+ SHELLOPTS : "braceexpand" ,
864+ "BASH_FUNC_compgen%%" : `() { printf 'ran' > ${ JSON . stringify ( functionMarkerPath ) } ; builtin compgen "$@"; }` ,
865+ } ,
866+ } ) ;
867+
868+ for ( const key of [ "BASH_ENV" , "BASHOPTS" , "ENV" , "SHELLOPTS" ] ) {
869+ expect ( env [ key ] ) . toBeUndefined ( ) ;
870+ }
871+ expect ( env [ "BASH_FUNC_compgen%%" ] ) . toBeUndefined ( ) ;
872+
873+ const result = spawnSync ( "/bin/bash" , [ "--noprofile" , "--norc" , allowlistProbePath ] , {
874+ encoding : "utf8" ,
875+ env,
876+ } ) ;
877+
878+ expect ( result . status , result . stderr ) . toBe ( 0 ) ;
879+ expect ( result . stdout ) . toBe ( "allowlist-survived" ) ;
880+ await expect ( readFile ( markerPath , "utf8" ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
881+ await expect ( readFile ( functionMarkerPath , "utf8" ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
882+ } ,
883+ ) ;
884+
809885 it ( "re-scrubs blocked credentials in the spawned gateway child env" , async ( ) => {
810886 const tempParent = await tempDirs . makeTempDir ( "qa-gateway-env-scrub-" ) ;
811887 qaTempPathState . preferredTmpDir = tempParent ;
@@ -1442,16 +1518,28 @@ describe("buildQaRuntimeEnv", () => {
14421518 ] ,
14431519 ] ) ;
14441520 for ( const record of authRecords ) {
1445- expect ( record . dbExists ) . toBe ( false ) ;
14461521 expect ( record . stdin ) . toMatch ( / ^ s k - q a - m o c k - [ a - f 0 - 9 ] { 32 } \n $ / u) ;
14471522 expect ( record . env ) . toMatchObject ( {
14481523 OPENCLAW_CLI : "1" ,
14491524 } ) ;
1525+ expect ( record . configMode ) . toBe ( 0o600 ) ;
1526+ expect ( record . configRegular ) . toBe ( true ) ;
1527+ expect ( record . configSymlink ) . toBe ( false ) ;
14501528 }
1529+ expect ( authRecords . map ( ( record ) => record . dbExists ) ) . toEqual ( [ false , true ] ) ;
1530+ const authConfigPaths = authRecords . map ( ( record ) => String ( record . configPath ) ) ;
1531+ expect ( new Set ( authConfigPaths ) . size ) . toBe ( 1 ) ;
1532+ expect ( authConfigPaths [ 0 ] ) . toBe (
1533+ path . join ( String ( authRecords [ 0 ] ?. stateDir ) , "qa-auth-bootstrap" , "openclaw.json" ) ,
1534+ ) ;
14511535 expect ( records . at ( - 1 ) ) . toMatchObject ( {
14521536 kind : "gateway" ,
1453- fixtureProfiles : [ "openai" , "anthropic" ] ,
1537+ authProfileIds : [ "qa-mock-openai" , "qa-mock-anthropic" ] ,
1538+ dbExists : true ,
14541539 } ) ;
1540+ expect ( records . at ( - 1 ) ?. configPath ) . not . toBe ( authConfigPaths [ 0 ] ) ;
1541+ expect ( records . at ( - 1 ) ?. fixtureProfiles ) . toBeUndefined ( ) ;
1542+ expect ( new Set ( records . map ( ( record ) => record . authDbPath ) ) . size ) . toBe ( 1 ) ;
14551543 } ) ;
14561544
14571545 it ( "blocks packaged gateway spawn when candidate auth bootstrap fails" , async ( ) => {
@@ -1487,7 +1575,13 @@ describe("buildQaRuntimeEnv", () => {
14871575 ) ;
14881576 const records = await readJsonLines ( recordPath ) ;
14891577 expect ( records ) . toHaveLength ( 1 ) ;
1490- expect ( records [ 0 ] ) . toMatchObject ( { kind : "auth" , dbExists : false } ) ;
1578+ expect ( records [ 0 ] ) . toMatchObject ( {
1579+ kind : "auth" ,
1580+ dbExists : false ,
1581+ configMode : 0o600 ,
1582+ configRegular : true ,
1583+ configSymlink : false ,
1584+ } ) ;
14911585 const submittedKey = String ( records [ 0 ] ?. stdin ) . trim ( ) ;
14921586 expect ( submittedKey ) . toMatch ( / ^ s k - q a - m o c k - [ a - f 0 - 9 ] { 32 } $ / u) ;
14931587 expect ( error . message ) . not . toContain ( submittedKey ) ;
0 commit comments