@@ -13,12 +13,22 @@ const chmodFailHook = vi.hoisted(() => ({
1313 error : undefined as Error | undefined ,
1414 calls : 0 ,
1515 failProbe : true ,
16+ removeTargetSuffix : undefined as string | undefined ,
17+ targets : [ ] as string [ ] ,
1618} ) ) ;
1719
1820vi . mock ( "node:fs" , async ( importOriginal ) => {
1921 const actual = await importOriginal < typeof import ( "node:fs" ) > ( ) ;
2022 const chmodSync : typeof actual . chmodSync = ( ( target : unknown , mode : unknown ) => {
2123 chmodFailHook . calls += 1 ;
24+ chmodFailHook . targets . push ( String ( target ) ) ;
25+ if (
26+ chmodFailHook . removeTargetSuffix &&
27+ String ( target ) . endsWith ( chmodFailHook . removeTargetSuffix )
28+ ) {
29+ // Remove the file after existsSync reaches chmod to reproduce the exact race.
30+ actual . unlinkSync ( String ( target ) ) ;
31+ }
2232 const isProbe = String ( target ) . includes ( ".openclaw-chmod-probe-" ) ;
2333 if ( chmodFailHook . error && ( chmodFailHook . failProbe || ! isProbe ) ) {
2434 throw chmodFailHook . error ;
@@ -53,6 +63,8 @@ describe("state database permission hardening without chmod support", () => {
5363 chmodFailHook . error = undefined ;
5464 chmodFailHook . calls = 0 ;
5565 chmodFailHook . failProbe = true ;
66+ chmodFailHook . removeTargetSuffix = undefined ;
67+ chmodFailHook . targets = [ ] ;
5668 closeOpenClawStateDatabaseForTest ( ) ;
5769 if ( stateDir ) {
5870 fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
@@ -135,6 +147,40 @@ describe("state database permission hardening without chmod support", () => {
135147 ) ;
136148 } ) ;
137149
150+ it . each ( [ "-wal" , "-shm" , "-journal" ] ) (
151+ "opens when the %s sidecar disappears before chmod" ,
152+ ( suffix ) => {
153+ stateDir = fs . mkdtempSync ( join ( tmpdir ( ) , "openclaw-state-chmod-" ) ) ;
154+ const options = { env : { OPENCLAW_STATE_DIR : stateDir } } ;
155+ openOpenClawStateDatabase ( options ) ;
156+ closeOpenClawStateDatabaseForTest ( ) ;
157+ const sidecarPath = join ( stateDir , "state" , `openclaw.sqlite${ suffix } ` ) ;
158+ fs . writeFileSync ( sidecarPath , "" ) ;
159+ chmodFailHook . removeTargetSuffix = suffix ;
160+
161+ const database = openOpenClawStateDatabase ( options ) ;
162+
163+ expect ( database . db . isOpen ) . toBe ( true ) ;
164+ expect ( fs . existsSync ( sidecarPath ) ) . toBe ( false ) ;
165+ expect ( chmodFailHook . targets ) . toContain ( sidecarPath ) ;
166+ } ,
167+ ) ;
168+
169+ it ( "rethrows when the main database vanishes between the existence check and the chmod" , ( ) => {
170+ // resolveSqliteDatabaseFilePaths lists the unsuffixed database first. Losing
171+ // it must stay fatal: swallowing that ENOENT would fall through to a SQLite
172+ // open that creates a fresh empty database instead of surfacing the loss.
173+ stateDir = fs . mkdtempSync ( join ( tmpdir ( ) , "openclaw-state-chmod-" ) ) ;
174+ const options = { env : { OPENCLAW_STATE_DIR : stateDir } } ;
175+ openOpenClawStateDatabase ( options ) ;
176+ closeOpenClawStateDatabaseForTest ( ) ;
177+ chmodFailHook . removeTargetSuffix = "openclaw.sqlite" ;
178+
179+ expect ( ( ) => openOpenClawStateDatabase ( options ) ) . toThrow ( / E N O E N T / ) ;
180+ // Guards against a vacuous pass if the main file were skipped before chmod.
181+ expect ( chmodFailHook . targets . some ( ( target ) => target . endsWith ( "openclaw.sqlite" ) ) ) . toBe ( true ) ;
182+ } ) ;
183+
138184 it ( "repairs the schema when chmodSync throws ENOTSUP" , ( ) => {
139185 stateDir = fs . mkdtempSync ( join ( tmpdir ( ) , "openclaw-state-chmod-" ) ) ;
140186 openOpenClawStateDatabase ( { env : { OPENCLAW_STATE_DIR : stateDir } } ) ;
0 commit comments