Skip to content

Commit 54f1800

Browse files
ZengWen-DTsteipete
andauthored
fix(matrix): bound recovery key stdin input (#108120)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 784ede0 commit 54f1800

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎extensions/matrix/src/cli.test.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ const consoleLogMock = vi.fn();
3636
const consoleErrorMock = vi.fn();
3737
const 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,

‎extensions/matrix/src/cli.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { matrixSetupAdapter } from "./setup-core.js";
3838
import type { CoreConfig } from "./types.js";
3939

4040
let matrixCliExitScheduled = false;
41+
const MATRIX_CLI_RECOVERY_KEY_STDIN_MAX_BYTES = 1024 * 1024;
4142

4243
const loadMatrixActionClientModule = createLazyRuntimeModule(
4344
() => import("./matrix/actions/client.js"),
@@ -68,10 +69,18 @@ function markCliFailure(): void {
6869

6970
async function readMatrixCliRecoveryKeyFromStdin(): Promise<string> {
7071
const chunks: Buffer[] = [];
72+
let totalBytes = 0;
7173
for await (const chunk of process.stdin) {
72-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
74+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
75+
totalBytes += buffer.byteLength;
76+
if (totalBytes > MATRIX_CLI_RECOVERY_KEY_STDIN_MAX_BYTES) {
77+
throw new Error(
78+
`Matrix recovery key stdin exceeds ${MATRIX_CLI_RECOVERY_KEY_STDIN_MAX_BYTES} bytes.`,
79+
);
80+
}
81+
chunks.push(buffer);
7382
}
74-
const recoveryKey = Buffer.concat(chunks).toString("utf8").trim();
83+
const recoveryKey = Buffer.concat(chunks, totalBytes).toString("utf8").trim();
7584
if (!recoveryKey) {
7685
throw new Error("Matrix recovery key was requested from stdin, but stdin was empty.");
7786
}

0 commit comments

Comments
 (0)