From 36108c0c2242ed797d73d79436296b7ea915f38f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Dec 2024 17:18:43 +0000 Subject: [PATCH 1/7] Update js-sdk usages around Buffers to avoid needing Buffer polyfill Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- package.json | 1 - .../views/elements/crypto/VerificationQRCode.tsx | 2 +- .../views/right_panel/VerificationPanel.tsx | 2 +- src/utils/tokens/pickling.ts | 2 +- webpack.config.js | 2 -- yarn.lock | 15 +-------------- 6 files changed, 4 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index d0a073ec2e6..d2e6c31ac18 100644 --- a/package.json +++ b/package.json @@ -214,7 +214,6 @@ "babel-loader": "^9.0.0", "babel-plugin-jsx-remove-data-test-id": "^3.0.0", "blob-polyfill": "^9.0.0", - "buffer": "^6.0.3", "chokidar": "^4.0.0", "concurrently": "^9.0.0", "copy-webpack-plugin": "^12.0.0", diff --git a/src/components/views/elements/crypto/VerificationQRCode.tsx b/src/components/views/elements/crypto/VerificationQRCode.tsx index 82f4ca20d3b..021457bc006 100644 --- a/src/components/views/elements/crypto/VerificationQRCode.tsx +++ b/src/components/views/elements/crypto/VerificationQRCode.tsx @@ -12,7 +12,7 @@ import QRCode from "../QRCode"; interface IProps { /** The data for the QR code. If `undefined`, a spinner is shown. */ - qrCodeBytes: undefined | Buffer; + qrCodeBytes: undefined | Uint8ClampedArray; } export default class VerificationQRCode extends React.PureComponent { diff --git a/src/components/views/right_panel/VerificationPanel.tsx b/src/components/views/right_panel/VerificationPanel.tsx index 7e04af78249..6f8295dece2 100644 --- a/src/components/views/right_panel/VerificationPanel.tsx +++ b/src/components/views/right_panel/VerificationPanel.tsx @@ -46,7 +46,7 @@ interface IState { * We attempt to calculate this once the verification request transitions into the "Ready" phase. If the other * side cannot scan QR codes, it will remain `undefined`. */ - qrCodeBytes: Buffer | undefined; + qrCodeBytes: Uint8ClampedArray | undefined; sasEvent: ShowSasCallbacks | null; emojiButtonClicked?: boolean; diff --git a/src/utils/tokens/pickling.ts b/src/utils/tokens/pickling.ts index a56915a4887..5fc82c16a5c 100644 --- a/src/utils/tokens/pickling.ts +++ b/src/utils/tokens/pickling.ts @@ -118,7 +118,7 @@ export async function buildAndEncodePickleKey( data.encrypted, ); if (pickleKeyBuf) { - return encodeUnpaddedBase64(pickleKeyBuf); + return encodeUnpaddedBase64(new Uint8Array(pickleKeyBuf)); } } catch { logger.error("Error decrypting pickle key"); diff --git a/webpack.config.js b/webpack.config.js index 0ed4dc36505..d01003af613 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -231,8 +231,6 @@ module.exports = (env, argv) => { // Polyfill needed by counterpart "util": require.resolve("util/"), - // Polyfill needed by matrix-js-sdk/src/crypto - "buffer": require.resolve("buffer/"), // Polyfill needed by sentry "process/browser": require.resolve("process/browser"), }, diff --git a/yarn.lock b/yarn.lock index 5cf9b0d5958..9a4888028c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4044,11 +4044,6 @@ base64-arraybuffer@^1.0.2: resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz#1c37589a7c4b0746e34bd1feb951da2df01c1bdc" integrity sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ== -base64-js@^1.3.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a" - integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA== - batch@0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/batch/-/batch-0.6.1.tgz#dc34314f4e679318093fc760272525f94bf25c16" @@ -4170,14 +4165,6 @@ buffer-from@^1.0.0: resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ== -buffer@^6.0.3: - version "6.0.3" - resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6" - integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA== - dependencies: - base64-js "^1.3.1" - ieee754 "^1.2.1" - builtin-modules@^3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.3.0.tgz#cae62812b89801e9656336e46223e030386be7b6" @@ -6777,7 +6764,7 @@ icss-utils@^5.0.0, icss-utils@^5.1.0: resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-5.1.0.tgz#c6be6858abd013d768e98366ae47e25d5887b1ae" integrity sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA== -ieee754@^1.1.12, ieee754@^1.2.1: +ieee754@^1.1.12: version "1.2.1" resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== From af0391b86a33e5e7f3735b6157eb7a2a8b04acf4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Dec 2024 17:32:10 +0000 Subject: [PATCH 2/7] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- webpack.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index d01003af613..3b75d2114bd 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -677,7 +677,6 @@ module.exports = (env, argv) => { // Automatically load buffer & process modules as we use them without explicitly // importing them new webpack.ProvidePlugin({ - Buffer: ["buffer", "Buffer"], process: "process/browser", }), From f3ef9e660231355cfdcf73996cc843b04a48c337 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Dec 2024 17:38:24 +0000 Subject: [PATCH 3/7] Update test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- test/unit-tests/Lifecycle-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit-tests/Lifecycle-test.ts b/test/unit-tests/Lifecycle-test.ts index 67ac7c76373..04c3459bdf6 100644 --- a/test/unit-tests/Lifecycle-test.ts +++ b/test/unit-tests/Lifecycle-test.ts @@ -418,7 +418,7 @@ describe("Lifecycle", () => { undefined, ); - expect(MatrixClientPeg.start).toHaveBeenCalledWith({ rustCryptoStoreKey: expect.any(Buffer) }); + expect(MatrixClientPeg.start).toHaveBeenCalledWith({ rustCryptoStoreKey: expect.any(Uint8Array) }); }); describe("with a refresh token", () => { From d0fea745bb825693ae808cb67723c2089b3a19f2 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Dec 2024 17:41:28 +0000 Subject: [PATCH 4/7] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/elements/crypto/VerificationQRCode-test.tsx | 4 +++- .../components/views/right_panel/VerificationPanel-test.tsx | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/unit-tests/components/views/elements/crypto/VerificationQRCode-test.tsx b/test/unit-tests/components/views/elements/crypto/VerificationQRCode-test.tsx index a62834bd9c2..a0db4bfdadc 100644 --- a/test/unit-tests/components/views/elements/crypto/VerificationQRCode-test.tsx +++ b/test/unit-tests/components/views/elements/crypto/VerificationQRCode-test.tsx @@ -17,7 +17,9 @@ describe("", () => { }); it("renders a QR code", async () => { - const { container, getAllByAltText } = render(); + const { container, getAllByAltText } = render( + , + ); // wait for the spinner to go away await waitFor(() => getAllByAltText("QR Code").length === 1); expect(container).toMatchSnapshot(); diff --git a/test/unit-tests/components/views/right_panel/VerificationPanel-test.tsx b/test/unit-tests/components/views/right_panel/VerificationPanel-test.tsx index adc496e1975..2adfc1eeb6c 100644 --- a/test/unit-tests/components/views/right_panel/VerificationPanel-test.tsx +++ b/test/unit-tests/components/views/right_panel/VerificationPanel-test.tsx @@ -46,7 +46,7 @@ describe("", () => { const request = makeMockVerificationRequest({ phase: Phase.Ready, }); - request.generateQRCode.mockResolvedValue(Buffer.from("test", "utf-8")); + request.generateQRCode.mockResolvedValue(new Uint8ClampedArray(Buffer.from("test", "utf-8"))); const container = renderComponent({ request: request, layout: "dialog", @@ -71,7 +71,7 @@ describe("", () => { const request = makeMockVerificationRequest({ phase: Phase.Ready, }); - request.generateQRCode.mockResolvedValue(Buffer.from("test", "utf-8")); + request.generateQRCode.mockResolvedValue(new Uint8ClampedArray(Buffer.from("test", "utf-8"))); const container = renderComponent({ request: request, member: new User("@other:user"), From cfdfc4e6406b6492bd022d86229c03c12bc46079 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 3 Dec 2024 18:06:05 +0000 Subject: [PATCH 5/7] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- playwright/e2e/crypto/device-verification.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playwright/e2e/crypto/device-verification.spec.ts b/playwright/e2e/crypto/device-verification.spec.ts index e01e5bbd83d..83a81c260cd 100644 --- a/playwright/e2e/crypto/device-verification.spec.ts +++ b/playwright/e2e/crypto/device-verification.spec.ts @@ -102,7 +102,7 @@ test.describe("Device verification", () => { // feed the QR code into the verification request. const qrData = await readQrCode(infoDialog); const verifier = await verificationRequest.evaluateHandle( - (request, qrData) => request.scanQRCode(new Uint8Array(qrData)), + (request, qrData) => request.scanQRCode(new Uint8ClampedArray(qrData)), [...qrData], ); From 974d3c175a967a45b3534d761885a6142dc472c9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Dec 2024 08:46:25 +0000 Subject: [PATCH 6/7] Use specific import for bloom-filters to avoid Buffer dependency Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/DecryptionFailureTracker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DecryptionFailureTracker.ts b/src/DecryptionFailureTracker.ts index 1e07ba252b5..bfcd126eeaa 100644 --- a/src/DecryptionFailureTracker.ts +++ b/src/DecryptionFailureTracker.ts @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only Please see LICENSE files in the repository root for full details. */ -import { ScalableBloomFilter } from "bloom-filters"; +import ScalableBloomFilter from "bloom-filters/dist/bloom/scalable-bloom-filter"; import { HttpApiEvent, MatrixClient, MatrixEventEvent, MatrixEvent } from "matrix-js-sdk/src/matrix"; import { Error as ErrorEvent } from "@matrix-org/analytics-events/types/typescript/Error"; import { DecryptionFailureCode, CryptoEvent } from "matrix-js-sdk/src/crypto-api"; From 619e41e3a2594f9d89bdaab72e74a1fa2ce94d5d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Dec 2024 11:46:48 +0000 Subject: [PATCH 7/7] Remove usages of Buffer Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .eslintrc.js | 7 +++++++ src/@types/png-chunks-extract.d.ts | 2 +- src/components/views/auth/LoginWithQR.tsx | 4 +--- src/stores/ThreepidInviteStore.ts | 2 +- src/utils/WidgetUtils.ts | 2 +- src/utils/exportUtils/HtmlExport.tsx | 2 +- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index f168a87a066..2b0dd2c186b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -42,6 +42,10 @@ module.exports = { name: "setImmediate", message: "Use setTimeout instead.", }, + { + name: "Buffer", + message: "Buffer is not available in the web.", + }, ], "import/no-duplicates": ["error"], @@ -255,6 +259,9 @@ module.exports = { additionalTestBlockFunctions: ["beforeAll", "beforeEach", "oldBackendOnly"], }, ], + + // These are fine in tests + "no-restricted-globals": "off", }, }, { diff --git a/src/@types/png-chunks-extract.d.ts b/src/@types/png-chunks-extract.d.ts index 38ea84c32af..c767b40655b 100644 --- a/src/@types/png-chunks-extract.d.ts +++ b/src/@types/png-chunks-extract.d.ts @@ -12,7 +12,7 @@ declare module "png-chunks-extract" { data: Uint8Array; } - function extractPngChunks(data: Uint8Array | Buffer): IChunk[]; + function extractPngChunks(data: Uint8Array): IChunk[]; export default extractPngChunks; } diff --git a/src/components/views/auth/LoginWithQR.tsx b/src/components/views/auth/LoginWithQR.tsx index e18b0e24abf..76475e1dd35 100644 --- a/src/components/views/auth/LoginWithQR.tsx +++ b/src/components/views/auth/LoginWithQR.tsx @@ -37,7 +37,6 @@ interface IState { userCode?: string; checkCode?: string; failureReason?: FailureReason; - lastScannedCode?: Buffer; } export enum LoginWithQRFailureReason { @@ -154,7 +153,7 @@ export default class LoginWithQR extends React.Component { throw new Error("Rendezvous not found"); } - if (!this.state.lastScannedCode && this.state.rendezvous?.checkCode !== checkCode) { + if (this.state.rendezvous?.checkCode !== checkCode) { this.setState({ failureReason: LoginWithQRFailureReason.CheckCodeMismatch }); return; } @@ -201,7 +200,6 @@ export default class LoginWithQR extends React.Component { failureReason: undefined, userCode: undefined, checkCode: undefined, - lastScannedCode: undefined, mediaPermissionError: false, }); } diff --git a/src/stores/ThreepidInviteStore.ts b/src/stores/ThreepidInviteStore.ts index 23c6d450d60..bdb1c8faa26 100644 --- a/src/stores/ThreepidInviteStore.ts +++ b/src/stores/ThreepidInviteStore.ts @@ -99,7 +99,7 @@ export default class ThreepidInviteStore extends EventEmitter { private generateIdOf(persisted: IPersistedThreepidInvite): string { // Use a consistent "hash" to form an ID. - return base32.stringify(Buffer.from(JSON.stringify(persisted))); + return base32.stringify(new TextEncoder().encode(JSON.stringify(persisted))); } private translateInvite(persisted: IPersistedThreepidInvite): IThreepidInvite { diff --git a/src/utils/WidgetUtils.ts b/src/utils/WidgetUtils.ts index de158efceff..ad2ed63ba16 100644 --- a/src/utils/WidgetUtils.ts +++ b/src/utils/WidgetUtils.ts @@ -445,7 +445,7 @@ export default class WidgetUtils { // For compatibility with Jitsi, use base32 without padding. // More details here: // https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification - confId = base32.stringify(Buffer.from(roomId), { pad: false }); + confId = base32.stringify(new TextEncoder().encode(roomId), { pad: false }); } else { // Create a random conference ID confId = `Jitsi${randomUppercaseString(1)}${randomLowercaseString(23)}`; diff --git a/src/utils/exportUtils/HtmlExport.tsx b/src/utils/exportUtils/HtmlExport.tsx index 9a6bb93bba8..e5b4667fc22 100644 --- a/src/utils/exportUtils/HtmlExport.tsx +++ b/src/utils/exportUtils/HtmlExport.tsx @@ -431,7 +431,7 @@ export default class HTMLExporter extends Exporter { !this.needsDateSeparator(event, prevEvent) && shouldFormContinuation(prevEvent, event, this.room.client, false); const body = await this.createMessageBody(event, shouldBeJoined); - this.totalSize += Buffer.byteLength(body); + this.totalSize += new TextEncoder().encode(body).byteLength; content += body; prevEvent = event; }