22import fs from "node:fs" ;
33import os from "node:os" ;
44import path from "node:path" ;
5- import { createOpenClawTestState , type OpenClawTestState } from "openclaw/plugin-sdk/test-state " ;
6- import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { withEnv , withTempDir } from "openclaw/plugin-sdk/test-env " ;
6+ import { describe , expect , it , vi } from "vitest" ;
77import type { BrowserConfig , BrowserProfileConfig } from "../config/config.js" ;
88import { resolveUserPath } from "../utils.js" ;
99import {
@@ -16,55 +16,6 @@ import { getBrowserProfileCapabilities } from "./profile-capabilities.js";
1616
1717const BROWSER_HEADLESS_ENV_KEY = "OPENCLAW_BROWSER_HEADLESS" ;
1818
19- // Isolate the extension relay secret (read from stateDir/credentials) so the
20- // extension-token assertions do not pick up a developer's real secret file.
21- let isolatedStateDir = "" ;
22- let openClawState : OpenClawTestState ;
23- beforeEach ( async ( ) => {
24- openClawState = await createOpenClawTestState ( {
25- layout : "state-only" ,
26- prefix : "openclaw-cfg-" ,
27- } ) ;
28- isolatedStateDir = openClawState . stateDir ;
29- } ) ;
30- afterEach ( async ( ) => {
31- await openClawState . cleanup ( ) ;
32- } ) ;
33-
34- /** Write a relay secret into the isolated state dir's credentials directory. */
35- function writeRelaySecret ( token : string ) : void {
36- const dir = path . join ( isolatedStateDir , "credentials" ) ;
37- fs . mkdirSync ( dir , { recursive : true } ) ;
38- fs . writeFileSync ( path . join ( dir , "browser-extension-relay.secret" ) , `${ token } \n` ) ;
39- }
40-
41- function withEnv < T > ( env : Record < string , string | undefined > , fn : ( ) => T ) : T {
42- const snapshot = new Map < string , string | undefined > ( ) ;
43- for ( const [ key ] of Object . entries ( env ) ) {
44- snapshot . set ( key , process . env [ key ] ) ;
45- }
46-
47- try {
48- for ( const key of Object . keys ( env ) ) {
49- const value = env [ key ] ;
50- if ( value === undefined ) {
51- delete process . env [ key ] ;
52- } else {
53- process . env [ key ] = value ;
54- }
55- }
56- return fn ( ) ;
57- } finally {
58- for ( const [ key , value ] of snapshot ) {
59- if ( value === undefined ) {
60- delete process . env [ key ] ;
61- } else {
62- process . env [ key ] = value ;
63- }
64- }
65- }
66- }
67-
6819function resolveRequiredProfile ( config : BrowserConfig , profileName : string ) {
6920 const profile = resolveProfile ( resolveBrowserConfig ( config ) , profileName ) ;
7021 if ( ! profile ) {
@@ -125,8 +76,7 @@ describe("browser config", () => {
12576 // Relay port sits just below the CDP allocation range (controlPort + 8).
12677 expect ( chrome ?. cdpPort ) . toBe ( resolved . extensionRelayDefaultPort ) ;
12778 expect ( resolved . extensionRelayDefaultPort ) . toBe ( resolved . controlPort + 8 ) ;
128- // No host-local relay secret exists yet (isolated state dir), so the relay
129- // cdpUrl carries no Basic credentials until pairing/startup creates one.
79+ // Only a running relay supplies the process-local Basic credential.
13080 expect ( chrome ?. cdpUrl ) . toBe ( `http://127.0.0.1:${ resolved . extensionRelayDefaultPort } ` ) ;
13181 expect ( chrome ?. cdpIsLoopback ) . toBe ( true ) ;
13282 } ) ;
@@ -202,11 +152,28 @@ describe("browser config", () => {
202152 expect ( ( ) => resolveBrowserConfig ( { profiles } ) ) . toThrow ( / e x t e n s i o n .* r e l a y .* p o r t / i) ;
203153 } ) ;
204154
205- it ( "keeps the host-local relay key out of the extension cdpUrl" , ( ) => {
206- const relayKey = "a" . repeat ( 64 ) ;
207- writeRelaySecret ( relayKey ) ;
155+ it . each ( [
156+ { name : "empty" , content : "" } ,
157+ { name : "malformed" , content : "not-a-relay-key\n" } ,
158+ { name : "valid" , content : `${ "a1" . repeat ( 32 ) } \n` } ,
159+ ] ) ( "normalizes config independently of a $name relay secret" , async ( { content } ) => {
160+ await withTempDir ( "openclaw-config-relay-" , async ( dir ) => {
161+ const stateDir = fs . realpathSync ( dir ) ;
162+ const credentials = path . join ( stateDir , "credentials" ) ;
163+ fs . mkdirSync ( credentials , { mode : 0o700 } ) ;
164+ const secretPath = path . join ( credentials , "browser-extension-relay.secret" ) ;
165+ withEnv ( { OPENCLAW_STATE_DIR : stateDir , OPENCLAW_OAUTH_DIR : credentials } , ( ) => {
166+ const withoutSecret = resolveBrowserConfig ( undefined ) ;
167+ fs . writeFileSync ( secretPath , content , { flag : "wx" , mode : 0o600 } ) ;
168+ expect ( resolveBrowserConfig ( undefined ) ) . toEqual ( withoutSecret ) ;
169+ expect ( fs . readFileSync ( secretPath , "utf8" ) ) . toBe ( content ) ;
170+ } ) ;
171+ } ) ;
172+ } ) ;
173+
174+ it ( "keeps the lifecycle's host-local relay key out of the extension cdpUrl" , ( ) => {
208175 const resolved = resolveBrowserConfig ( undefined ) ;
209- expect ( resolved . extensionRelayToken ) . toBe ( relayKey ) ;
176+ resolved . extensionRelayToken = "a1" . repeat ( 32 ) ;
210177 const chrome = resolveProfile ( resolved , "chrome" ) ;
211178 expect ( chrome ?. cdpUrl ) . toBe ( `http://127.0.0.1:${ resolved . extensionRelayDefaultPort } ` ) ;
212179
0 commit comments