@@ -5,12 +5,21 @@ import {
55 createPluginStateSyncKeyedStoreForTests ,
66 resetPluginStateStoreForTests ,
77} from "openclaw/plugin-sdk/plugin-state-test-runtime" ;
8- import { afterEach , describe , expect , it , vi } from "vitest" ;
8+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
99import { assertMatrixQaCliBackupRestoreFailed } from "./scenario-runtime-e2ee-destructive-recovery.js" ;
1010import { mutateMatrixQaCliStateLoss } from "./scenario-runtime-e2ee-state.js" ;
11+ import type { MatrixQaScenarioContext } from "./scenario-runtime-shared.js" ;
1112
1213const testing = { assertMatrixQaCliBackupRestoreFailed } ;
1314
15+ const destructiveScenarioMocks = vi . hoisted ( ( ) => ( {
16+ createMatrixQaClient : vi . fn ( ) ,
17+ createMatrixQaE2eeScenarioClient : vi . fn ( ) ,
18+ createMatrixQaRecoveryCliRuntime : vi . fn ( ) ,
19+ loginMatrixQaRecoveryDevice : vi . fn ( ) ,
20+ runMatrixQaCliJson : vi . fn ( ) ,
21+ } ) ) ;
22+
1423const storageMetadataRuntime = vi . hoisted ( ( ) => ( {
1524 normalizeMatrixStorageMetadata ( value : unknown ) {
1625 if ( ! value || typeof value !== "object" ) {
@@ -31,7 +40,12 @@ const storageMetadataRuntime = vi.hoisted(() => ({
3140 } ,
3241} ) ) ;
3342
43+ vi . mock ( "../substrate/client.js" , ( ) => ( {
44+ createMatrixQaClient : destructiveScenarioMocks . createMatrixQaClient ,
45+ } ) ) ;
46+
3447vi . mock ( "../substrate/e2ee-client.js" , ( ) => ( {
48+ createMatrixQaE2eeScenarioClient : destructiveScenarioMocks . createMatrixQaE2eeScenarioClient ,
3549 loadMatrixQaE2eeRuntime : async ( ) => ( {
3650 ...storageMetadataRuntime ,
3751 openMatrixRecoveryKeyStoreOptions : ( storageRootDir : string ) => ( {
@@ -42,6 +56,85 @@ vi.mock("../substrate/e2ee-client.js", () => ({
4256 } ) ,
4357} ) ) ;
4458
59+ vi . mock ( "./scenario-runtime-e2ee-destructive-recovery.js" , async ( importOriginal ) => ( {
60+ ...( await importOriginal < typeof import ( "./scenario-runtime-e2ee-destructive-recovery.js" ) > ( ) ) ,
61+ createMatrixQaRecoveryCliRuntime : destructiveScenarioMocks . createMatrixQaRecoveryCliRuntime ,
62+ loginMatrixQaRecoveryDevice : destructiveScenarioMocks . loginMatrixQaRecoveryDevice ,
63+ runMatrixQaCliJson : destructiveScenarioMocks . runMatrixQaCliJson ,
64+ } ) ) ;
65+
66+ import { runMatrixQaE2eeWrongAccountRecoveryKeyScenario } from "./scenario-runtime-e2ee-destructive.js" ;
67+
68+ function createDisposableOwner ( params : {
69+ backupVersion : string ;
70+ cleanupOrder ?: string [ ] ;
71+ encodedRecoveryKey : string ;
72+ failCleanup ?: boolean ;
73+ label : string ;
74+ } ) {
75+ return {
76+ bootstrapOwnDeviceVerification : vi . fn ( async ( ) => ( {
77+ success : true ,
78+ verification : {
79+ backupVersion : params . backupVersion ,
80+ crossSigningVerified : true ,
81+ verified : true ,
82+ } ,
83+ } ) ) ,
84+ deleteOwnDevices : vi . fn ( async ( deviceIds : string [ ] ) => {
85+ params . cleanupOrder ?. push ( `${ params . label } :delete:${ deviceIds . join ( "," ) } ` ) ;
86+ if ( params . failCleanup ) {
87+ throw new Error ( `${ params . label } device cleanup failed` ) ;
88+ }
89+ return { } ;
90+ } ) ,
91+ getRecoveryKey : vi . fn ( async ( ) => ( {
92+ encodedPrivateKey : params . encodedRecoveryKey ,
93+ keyId : `${ params . label } -key` ,
94+ } ) ) ,
95+ sendTextMessage : vi . fn ( async ( ) => `$${ params . label } -seed` ) ,
96+ stop : vi . fn ( async ( ) => {
97+ params . cleanupOrder ?. push ( `${ params . label } :stop` ) ;
98+ if ( params . failCleanup ) {
99+ throw new Error ( `${ params . label } stop failed` ) ;
100+ }
101+ } ) ,
102+ } ;
103+ }
104+
105+ function createWrongAccountContext ( ) : MatrixQaScenarioContext {
106+ const context = {
107+ baseUrl : "http://127.0.0.1:28123" ,
108+ driverAccessToken : "unused-driver-token" ,
109+ driverUserId : "@unused-driver:matrix-qa.test" ,
110+ observedEvents : [ ] ,
111+ observerAccessToken : "must-not-read" ,
112+ observerDeviceId : "must-not-read" ,
113+ observerPassword : "must-not-read" ,
114+ observerUserId : "must-not-read" ,
115+ outputDir : "/tmp/matrix-qa-output" ,
116+ registrationToken : "registration-token" ,
117+ roomId : "!unused:matrix-qa.test" ,
118+ sutAccessToken : "unused-sut-token" ,
119+ sutUserId : "@unused-sut:matrix-qa.test" ,
120+ syncState : { } ,
121+ timeoutMs : 30_000 ,
122+ topology : { rooms : [ ] } ,
123+ } as unknown as MatrixQaScenarioContext ;
124+ return new Proxy ( context , {
125+ get ( target , property , receiver ) {
126+ if ( typeof property === "string" && property . startsWith ( "observer" ) ) {
127+ throw new Error ( `observer credential read: ${ property } ` ) ;
128+ }
129+ return Reflect . get ( target , property , receiver ) ;
130+ } ,
131+ } ) ;
132+ }
133+
134+ beforeEach ( ( ) => {
135+ vi . clearAllMocks ( ) ;
136+ } ) ;
137+
45138describe ( "Matrix destructive E2EE storage discovery" , ( ) => {
46139 const tempDirs : string [ ] = [ ] ;
47140
@@ -74,6 +167,133 @@ describe("Matrix destructive E2EE storage discovery", () => {
74167 } ) ;
75168} ) ;
76169
170+ describe ( "Matrix wrong-account recovery-key isolation" , ( ) => {
171+ it ( "uses disposable source and target owners without reading observer credentials" , async ( ) => {
172+ const cleanupOrder : string [ ] = [ ] ;
173+ const sourceOwner = createDisposableOwner ( {
174+ backupVersion : "source-backup" ,
175+ cleanupOrder,
176+ encodedRecoveryKey : "source-recovery-key" ,
177+ failCleanup : true ,
178+ label : "source" ,
179+ } ) ;
180+ const targetOwner = createDisposableOwner ( {
181+ backupVersion : "target-backup" ,
182+ cleanupOrder,
183+ encodedRecoveryKey : "target-recovery-key" ,
184+ failCleanup : true ,
185+ label : "target" ,
186+ } ) ;
187+ const registerWithToken = vi
188+ . fn ( )
189+ . mockResolvedValueOnce ( {
190+ accessToken : "source-token" ,
191+ deviceId : "SOURCE-DEVICE" ,
192+ password : "source-password" ,
193+ userId : "@source:matrix-qa.test" ,
194+ } )
195+ . mockResolvedValueOnce ( {
196+ accessToken : "target-token" ,
197+ deviceId : "TARGET-DEVICE" ,
198+ password : "target-password" ,
199+ userId : "@target:matrix-qa.test" ,
200+ } ) ;
201+ destructiveScenarioMocks . createMatrixQaClient . mockReturnValue ( {
202+ createPrivateRoom : vi
203+ . fn ( )
204+ . mockResolvedValueOnce ( "!source:matrix-qa.test" )
205+ . mockResolvedValueOnce ( "!target:matrix-qa.test" ) ,
206+ registerWithToken,
207+ } ) ;
208+ destructiveScenarioMocks . createMatrixQaE2eeScenarioClient
209+ . mockResolvedValueOnce ( sourceOwner )
210+ . mockResolvedValueOnce ( targetOwner ) ;
211+ destructiveScenarioMocks . loginMatrixQaRecoveryDevice . mockResolvedValue ( {
212+ accessToken : "target-recovery-token" ,
213+ deviceId : "TARGET-RECOVERY-DEVICE" ,
214+ userId : "@target:matrix-qa.test" ,
215+ } ) ;
216+ destructiveScenarioMocks . createMatrixQaRecoveryCliRuntime . mockResolvedValue ( {
217+ dispose : vi . fn ( async ( ) => {
218+ cleanupOrder . push ( "cli:dispose" ) ;
219+ throw new Error ( "CLI cleanup failed" ) ;
220+ } ) ,
221+ } ) ;
222+ destructiveScenarioMocks . runMatrixQaCliJson . mockResolvedValue ( {
223+ artifacts : { stderrPath : "/tmp/stderr" , stdoutPath : "/tmp/stdout" } ,
224+ payload : {
225+ backup : {
226+ decryptionKeyCached : false ,
227+ keyLoadError : "bad MAC" ,
228+ matchesDecryptionKey : false ,
229+ } ,
230+ backupVersion : "target-backup" ,
231+ error : "bad MAC" ,
232+ success : false ,
233+ } ,
234+ result : { exitCode : 1 } ,
235+ } ) ;
236+
237+ const result = await runMatrixQaE2eeWrongAccountRecoveryKeyScenario (
238+ createWrongAccountContext ( ) ,
239+ ) ;
240+
241+ expect ( registerWithToken ) . toHaveBeenCalledTimes ( 2 ) ;
242+ expect ( destructiveScenarioMocks . createMatrixQaE2eeScenarioClient ) . toHaveBeenCalledTimes ( 2 ) ;
243+ expect ( destructiveScenarioMocks . loginMatrixQaRecoveryDevice ) . toHaveBeenCalledWith (
244+ expect . objectContaining ( {
245+ password : "target-password" ,
246+ userId : "@target:matrix-qa.test" ,
247+ } ) ,
248+ ) ;
249+ expect ( destructiveScenarioMocks . runMatrixQaCliJson ) . toHaveBeenCalledWith (
250+ expect . objectContaining ( { stdin : "source-recovery-key\n" } ) ,
251+ ) ;
252+ expect ( result . artifacts ) . toMatchObject ( {
253+ restoreExitCode : 1 ,
254+ targetRecoveryDeviceId : "TARGET-RECOVERY-DEVICE" ,
255+ } ) ;
256+ expect ( cleanupOrder ) . toEqual ( [
257+ "cli:dispose" ,
258+ "target:delete:TARGET-RECOVERY-DEVICE" ,
259+ "target:stop" ,
260+ "source:stop" ,
261+ ] ) ;
262+ expect ( targetOwner . deleteOwnDevices ) . toHaveBeenCalledOnce ( ) ;
263+ expect ( targetOwner . stop ) . toHaveBeenCalledOnce ( ) ;
264+ expect ( sourceOwner . stop ) . toHaveBeenCalledOnce ( ) ;
265+ } ) ;
266+
267+ it ( "releases the source owner when target setup fails" , async ( ) => {
268+ const sourceOwner = createDisposableOwner ( {
269+ backupVersion : "source-backup" ,
270+ encodedRecoveryKey : "source-recovery-key" ,
271+ label : "source" ,
272+ } ) ;
273+ const registerWithToken = vi
274+ . fn ( )
275+ . mockResolvedValueOnce ( {
276+ accessToken : "source-token" ,
277+ deviceId : "SOURCE-DEVICE" ,
278+ password : "source-password" ,
279+ userId : "@source:matrix-qa.test" ,
280+ } )
281+ . mockRejectedValueOnce ( new Error ( "target registration failed" ) ) ;
282+ destructiveScenarioMocks . createMatrixQaClient . mockReturnValue ( {
283+ createPrivateRoom : vi . fn ( ) . mockResolvedValue ( "!source:matrix-qa.test" ) ,
284+ registerWithToken,
285+ } ) ;
286+ destructiveScenarioMocks . createMatrixQaE2eeScenarioClient . mockResolvedValueOnce ( sourceOwner ) ;
287+
288+ await expect (
289+ runMatrixQaE2eeWrongAccountRecoveryKeyScenario ( createWrongAccountContext ( ) ) ,
290+ ) . rejects . toThrow ( "target registration failed" ) ;
291+
292+ expect ( sourceOwner . stop ) . toHaveBeenCalledOnce ( ) ;
293+ expect ( destructiveScenarioMocks . createMatrixQaRecoveryCliRuntime ) . not . toHaveBeenCalled ( ) ;
294+ } ) ;
295+ } ) ;
296+
77297describe ( "Matrix destructive E2EE backup failure assertions" , ( ) => {
78298 it ( "requires a nonzero CLI exit" , ( ) => {
79299 expect ( ( ) =>
0 commit comments