@@ -46,6 +46,22 @@ const nim = require("./nim");
4646const onboardSession = require ( "./onboard-session" ) ;
4747const policies = require ( "./policies" ) ;
4848const { checkPortAvailable, ensureSwap, getMemoryInfo } = require ( "./preflight" ) ;
49+ function secureTempFile ( prefix , ext = "" ) {
50+ const dir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , `${ prefix } -` ) ) ;
51+ return path . join ( dir , `${ prefix } ${ ext } ` ) ;
52+ }
53+
54+ /**
55+ * Safely remove a mkdtemp-created directory. Guards against accidentally
56+ * deleting the system temp root if a caller passes os.tmpdir() itself.
57+ */
58+ function cleanupTempDir ( filePath , expectedPrefix ) {
59+ const parentDir = path . dirname ( filePath ) ;
60+ if ( parentDir !== os . tmpdir ( ) && path . basename ( parentDir ) . startsWith ( `${ expectedPrefix } -` ) ) {
61+ fs . rmSync ( parentDir , { recursive : true , force : true } ) ;
62+ }
63+ }
64+
4965const EXPERIMENTAL = process . env . NEMOCLAW_EXPERIMENTAL === "1" ;
5066const USE_COLOR = ! process . env . NO_COLOR && ! ! process . stdout . isTTY ;
5167const DIM = USE_COLOR ? "\x1b[2m" : "" ;
@@ -683,7 +699,7 @@ function getProbeRecovery(probe, options = {}) {
683699
684700// eslint-disable-next-line complexity
685701function runCurlProbe ( argv ) {
686- const bodyFile = path . join ( os . tmpdir ( ) , ` nemoclaw-curl-probe- ${ Date . now ( ) } - ${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } . json` ) ;
702+ const bodyFile = secureTempFile ( " nemoclaw-curl-probe" , ". json" ) ;
687703 try {
688704 const args = [ ...argv ] ;
689705 const url = args . pop ( ) ;
@@ -736,7 +752,7 @@ function runCurlProbe(argv) {
736752 message : summarizeCurlFailure ( error ?. status || 1 , error ?. message || String ( error ) ) ,
737753 } ;
738754 } finally {
739- fs . rmSync ( bodyFile , { force : true } ) ;
755+ cleanupTempDir ( bodyFile , "nemoclaw-curl-probe" ) ;
740756 }
741757}
742758
@@ -3196,7 +3212,7 @@ async function setupOpenclaw(sandboxName, model, provider) {
31963212 { stdio : [ "ignore" , "ignore" , "inherit" ] } ,
31973213 ) ;
31983214 } finally {
3199- fs . rmSync ( path . dirname ( scriptFile ) , { recursive : true , force : true } ) ;
3215+ cleanupTempDir ( scriptFile , "nemoclaw-sync" ) ;
32003216 }
32013217 }
32023218
0 commit comments