@@ -17,7 +17,8 @@ import { makeTempWorkspace } from "../../test-helpers/workspace.js";
1717import { captureEnv } from "../../test-utils/env.js" ;
1818import { createCliRuntimeCapture } from "../test-runtime-capture.js" ;
1919
20- const { runtimeLogs, defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture ( ) ;
20+ const { runtimeLogs, runtimeErrors, defaultRuntime, resetRuntimeCapture } =
21+ createCliRuntimeCapture ( ) ;
2122const busctl = vi . hoisted ( ( ) =>
2223 vi . fn < typeof import ( "../../daemon/systemd-exec.js" ) . execBusctlUser > ( ) ,
2324) ;
@@ -180,6 +181,69 @@ describe("runDaemonInstall integration", () => {
180181 expect ( joined ) . toContain ( "MISSING_GATEWAY_TOKEN" ) ;
181182 } ) ;
182183
184+ it . each ( [ true , false ] ) (
185+ "explains unsafe publication permissions and recovers without bypassing SecretRefs (json=%s)" ,
186+ async ( json ) => {
187+ const fixture = await fs . realpath (
188+ await fs . mkdtemp ( path . join ( tempHome , "private-path-canary-" ) ) ,
189+ ) ;
190+ const ancestor = path . join ( fixture , ".config" ) ;
191+ const config = {
192+ gateway : {
193+ auth : {
194+ mode : "token" ,
195+ token : { source : "env" , provider : "default" , id : "MISSING_GATEWAY_TOKEN" } ,
196+ } ,
197+ } ,
198+ } ;
199+ await fs . mkdir ( ancestor ) ;
200+ await fs . chmod ( ancestor , 0o777 ) ;
201+ await fs . writeFile ( configPath , JSON . stringify ( config ) ) ;
202+ clearConfigCache ( ) ;
203+ busctl . mockResolvedValue ( {
204+ code : 1 ,
205+ termination : "exit" ,
206+ stdout : "" ,
207+ stderr : "Call failed: Unit openclaw-gateway.service not found." ,
208+ } ) ;
209+ const env = { ...process . env , HOME : fixture , OPENCLAW_SYSTEMD_UNIT : "openclaw-gateway" } ;
210+ serviceMock . readCommand . mockImplementation ( ( _env , options ) =>
211+ readSystemdServiceExecStart ( env , options ) ,
212+ ) ;
213+ serviceMock . readDefinitionMutationCapability . mockImplementation ( ( ) =>
214+ readSystemdDefinitionMutationCapability ( env ) ,
215+ ) ;
216+ const before = await snapshotConfig ( ) ;
217+ try {
218+ await expect ( runDaemonInstall ( { json, force : true } ) ) . rejects . toThrow ( "__exit__:1" ) ;
219+ expect ( await snapshotConfig ( ) ) . toEqual ( before ) ;
220+ expect ( await fs . readdir ( ancestor ) ) . toEqual ( [ ] ) ;
221+ expect ( serviceMock . install ) . not . toHaveBeenCalled ( ) ;
222+ const output = [ ...runtimeLogs , ...runtimeErrors ] . join ( "\n" ) ;
223+ expect ( output ) . toContain ( "SERVICE_DEFINITION_UNKNOWN" ) ;
224+ expect ( output ) . toContain ( "unsafe-permissions" ) ;
225+ expect ( output ) . toContain ( "service directory" ) ;
226+ expect ( output ) . toContain ( "group/world-writable" ) ;
227+ expect ( output ) . toContain ( "chmod go-w" ) ;
228+ expect ( output ) . not . toContain ( "private-path-canary" ) ;
229+ expect ( output ) . not . toContain ( "MISSING_GATEWAY_TOKEN" ) ;
230+
231+ await fs . chmod ( ancestor , 0o700 ) ;
232+ resetRuntimeCapture ( ) ;
233+ await expect ( runDaemonInstall ( { json, force : true } ) ) . rejects . toThrow ( "__exit__:1" ) ;
234+ const recovered = [ ...runtimeLogs , ...runtimeErrors ] . join ( "\n" ) ;
235+ expect ( recovered ) . not . toContain ( "SERVICE_DEFINITION_UNKNOWN" ) ;
236+ expect ( recovered ) . toContain ( "SecretRef is configured but unresolved" ) ;
237+ expect ( ( await readJson ( configPath ) ) . gateway ) . toEqual ( { ...config . gateway , mode : "local" } ) ;
238+ expect ( await fs . readdir ( ancestor ) ) . toEqual ( [ ] ) ;
239+ expect ( serviceMock . install ) . not . toHaveBeenCalled ( ) ;
240+ } finally {
241+ await fs . chmod ( ancestor , 0o700 ) ;
242+ await fs . rm ( fixture , { recursive : true , force : true } ) ;
243+ }
244+ } ,
245+ ) ;
246+
183247 it . each ( [ "fragment" , "drop-in" ] ) (
184248 "blocks a root-owned manager %s before config or token writes" ,
185249 async ( kind ) => {
@@ -349,19 +413,19 @@ describe("runDaemonInstall integration", () => {
349413 it . each ( [
350414 {
351415 name : "gateway.mode is missing" ,
352- capability : { kind : "sealed" as const , detail : "unit definition is owned by root" } ,
416+ capability : { kind : "sealed" as const , reason : "foreign-owner" as const } ,
353417 config : { gateway : { auth : { mode : "token" , token : "existing-token" } } } ,
354418 marker : "SERVICE_DEFINITION_SEALED" ,
355419 } ,
356420 {
357421 name : "the gateway token is missing" ,
358- capability : { kind : "sealed" as const , detail : "unit definition is owned by root" } ,
422+ capability : { kind : "sealed" as const , reason : "foreign-owner" as const } ,
359423 config : { gateway : { mode : "local" , auth : { mode : "token" } } } ,
360424 marker : "SERVICE_DEFINITION_SEALED" ,
361425 } ,
362426 {
363427 name : "gateway.mode is missing and definition authority is unknown" ,
364- capability : { kind : "unknown" as const , detail : "unit definition cannot be inspected" } ,
428+ capability : { kind : "unknown" as const , reason : "inspection-failed" as const } ,
365429 config : { gateway : { auth : { mode : "token" } } } ,
366430 marker : "SERVICE_DEFINITION_UNKNOWN" ,
367431 } ,
@@ -370,7 +434,7 @@ describe("runDaemonInstall integration", () => {
370434 async ( { capability, config, marker } ) => {
371435 await fs . writeFile ( configPath , JSON . stringify ( config , null , 2 ) ) ;
372436 clearConfigCache ( ) ;
373- serviceMock . readDefinitionMutationCapability . mockResolvedValueOnce ( capability as never ) ;
437+ serviceMock . readDefinitionMutationCapability . mockResolvedValueOnce ( capability ) ;
374438 const before = await snapshotConfig ( ) ;
375439
376440 await expect ( runDaemonInstall ( { json : true , force : true } ) ) . rejects . toThrow ( "__exit__:1" ) ;
@@ -437,8 +501,8 @@ describe("runDaemonInstall integration", () => {
437501 serviceMock . isLoaded . mockResolvedValue ( true ) ;
438502 serviceMock . readDefinitionMutationCapability . mockResolvedValue ( {
439503 kind : "sealed" ,
440- detail : "unit definition is owned by root " ,
441- } as never ) ;
504+ reason : "foreign-owner " ,
505+ } ) ;
442506 serviceMock . readCommand . mockResolvedValue ( await createInstalledServiceCommand ( ) ) ;
443507
444508 await runDaemonInstall ( { json : true } ) ;
@@ -462,8 +526,8 @@ describe("runDaemonInstall integration", () => {
462526 } as never ) ;
463527 serviceMock . readDefinitionMutationCapability . mockResolvedValueOnce ( {
464528 kind : "sealed" ,
465- detail : "unit definition is owned by root " ,
466- } as never ) ;
529+ reason : "foreign-owner " ,
530+ } ) ;
467531 const before = await snapshotConfig ( ) ;
468532
469533 await expect ( runDaemonInstall ( { json : true } ) ) . rejects . toThrow ( "__exit__:1" ) ;
@@ -482,11 +546,10 @@ describe("runDaemonInstall integration", () => {
482546 programArguments : [ "openclaw" , "gateway" , "run" ] ,
483547 environment : { OPENCLAW_STATE_DIR : effectiveStateDir } ,
484548 } as never ) ;
485- serviceMock . readDefinitionMutationCapability . mockImplementationOnce (
486- async ( args ) =>
487- ( args ?. environment ?. OPENCLAW_STATE_DIR === effectiveStateDir
488- ? { kind : "sealed" , detail : "effective state is owned by root" }
489- : { kind : "writable" } ) as never ,
549+ serviceMock . readDefinitionMutationCapability . mockImplementationOnce ( async ( args ) =>
550+ args ?. environment ?. OPENCLAW_STATE_DIR === effectiveStateDir
551+ ? { kind : "sealed" , reason : "foreign-owner" }
552+ : { kind : "writable" } ,
490553 ) ;
491554 const before = await snapshotConfig ( ) ;
492555
@@ -522,6 +585,7 @@ describe("runDaemonInstall integration", () => {
522585 } else {
523586 serviceMock . readDefinitionMutationCapability . mockResolvedValueOnce ( {
524587 kind,
588+ reason : kind === "sealed" ? "foreign-owner" : "inspection-failed" ,
525589 detail : secret ,
526590 } as never ) ;
527591 }
0 commit comments