@@ -36,10 +36,12 @@ const consoleLogMock = vi.fn();
3636const consoleErrorMock = vi . fn ( ) ;
3737const stdoutWriteMock = vi . fn ( ) ;
3838
39- function mockRecoveryKeyStdin ( value : string ) : void {
39+ function mockRecoveryKeyStdin ( ... values : string [ ] ) : void {
4040 vi . spyOn ( process . stdin , Symbol . asyncIterator ) . mockReturnValue (
4141 ( async function * ( ) : AsyncGenerator < Buffer , undefined , unknown > {
42- yield Buffer . from ( value ) ;
42+ for ( const value of values ) {
43+ yield Buffer . from ( value ) ;
44+ }
4345 return undefined ;
4446 } ) ( ) ,
4547 ) ;
@@ -839,6 +841,33 @@ describe("matrix CLI verification commands", () => {
839841 } ) ;
840842 } ) ;
841843
844+ it ( "rejects oversized recovery key stdin before backup restore" , async ( ) => {
845+ mockRecoveryKeyStdin ( "x" . repeat ( 1024 * 1024 ) , "x" ) ;
846+ const program = buildProgram ( ) ;
847+
848+ await program . parseAsync ( [ "matrix" , "verify" , "backup" , "restore" , "--recovery-key-stdin" ] , {
849+ from : "user" ,
850+ } ) ;
851+
852+ expect ( process . exitCode ) . toBe ( 1 ) ;
853+ expect ( consoleErrorMock ) . toHaveBeenCalledWith (
854+ "Backup restore failed: Matrix recovery key stdin exceeds 1048576 bytes." ,
855+ ) ;
856+ expect ( restoreMatrixRoomKeyBackupMock ) . not . toHaveBeenCalled ( ) ;
857+ } ) ;
858+
859+ it ( "preserves a multibyte recovery key at the stdin byte limit" , async ( ) => {
860+ const recoveryKey = "é" . repeat ( ( 1024 * 1024 ) / 2 ) ;
861+ mockRecoveryKeyStdin ( recoveryKey ) ;
862+ const program = buildProgram ( ) ;
863+
864+ await program . parseAsync ( [ "matrix" , "verify" , "backup" , "restore" , "--recovery-key-stdin" ] , {
865+ from : "user" ,
866+ } ) ;
867+
868+ expectRecordFields ( mockCallArg ( restoreMatrixRoomKeyBackupMock ) , { recoveryKey } ) ;
869+ } ) ;
870+
842871 it ( "sets non-zero exit code for backup reset failures in JSON mode" , async ( ) => {
843872 resetMatrixRoomKeyBackupMock . mockResolvedValue ( {
844873 success : false ,
0 commit comments