From ec6272aa3ddc6a274b4b62c293745b58a6f49055 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Thu, 5 Jan 2023 15:00:37 +0000 Subject: [PATCH] Fix outgoing messages for rust-crypto (#3025) It turns out that MatrixClient uses a `FetchHttpApi` instance with `opts.onlyData = true`, so it was returning the json-parsed response rather than the raw response. Change the way we call `authedRequest` so that we get the raw body back. --- spec/unit/rust-crypto.spec.ts | 7 ++++--- src/rust-crypto/index.ts | 2 +- src/rust-crypto/rust-crypto.ts | 36 +++++++++++++++++----------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/spec/unit/rust-crypto.spec.ts b/spec/unit/rust-crypto.spec.ts index 0bcc88d5d26..69dde71239e 100644 --- a/spec/unit/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto.spec.ts @@ -30,7 +30,7 @@ import MockHttpBackend from "matrix-mock-request"; import { RustCrypto } from "../../src/rust-crypto/rust-crypto"; import { initRustCrypto } from "../../src/rust-crypto"; -import { HttpApiEvent, HttpApiEventHandlerMap, IHttpOpts, IToDeviceEvent, MatrixHttpApi } from "../../src"; +import { HttpApiEvent, HttpApiEventHandlerMap, IToDeviceEvent, MatrixClient, MatrixHttpApi } from "../../src"; import { TypedEventEmitter } from "../../src/models/typed-event-emitter"; afterEach(() => { @@ -48,7 +48,7 @@ describe("RustCrypto", () => { let rustCrypto: RustCrypto; beforeEach(async () => { - const mockHttpApi = {} as MatrixHttpApi; + const mockHttpApi = {} as MatrixClient["http"]; rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto; }); @@ -62,7 +62,7 @@ describe("RustCrypto", () => { let rustCrypto: RustCrypto; beforeEach(async () => { - const mockHttpApi = {} as MatrixHttpApi; + const mockHttpApi = {} as MatrixClient["http"]; rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto; }); @@ -132,6 +132,7 @@ describe("RustCrypto", () => { baseUrl: "https://example.com", prefix: "/_matrix", fetchFn: httpBackend.fetchFn as typeof global.fetch, + onlyData: true, }); // for these tests we use a mock OlmMachine, with an implementation of outgoingRequests that diff --git a/src/rust-crypto/index.ts b/src/rust-crypto/index.ts index e3b3cb67cd8..4c826078f9f 100644 --- a/src/rust-crypto/index.ts +++ b/src/rust-crypto/index.ts @@ -23,7 +23,7 @@ import { RUST_SDK_STORE_PREFIX } from "./constants"; import { IHttpOpts, MatrixHttpApi } from "../http-api"; export async function initRustCrypto( - http: MatrixHttpApi, + http: MatrixHttpApi, userId: string, deviceId: string, ): Promise { diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 9bb75be70ee..b48de46970b 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -28,7 +28,7 @@ import type { IToDeviceEvent } from "../sync-accumulator"; import { MatrixEvent } from "../models/event"; import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend"; import { logger } from "../logger"; -import { IHttpOpts, IRequestOpts, MatrixHttpApi, Method } from "../http-api"; +import { IHttpOpts, MatrixHttpApi, Method } from "../http-api"; import { QueryDict } from "../utils"; /** @@ -54,7 +54,7 @@ export class RustCrypto implements CryptoBackend { public constructor( private readonly olmMachine: RustSdkCryptoJs.OlmMachine, - private readonly http: MatrixHttpApi, + private readonly http: MatrixHttpApi, _userId: string, _deviceId: string, ) {} @@ -181,21 +181,21 @@ export class RustCrypto implements CryptoBackend { } } - private async rawJsonRequest( - method: Method, - path: string, - queryParams: QueryDict, - body: string, - opts: IRequestOpts = {}, - ): Promise { - // unbeknownst to HttpApi, we are sending JSON - if (!opts.headers) opts.headers = {}; - opts.headers["Content-Type"] = "application/json"; - - // we use the full prefix - if (!opts.prefix) opts.prefix = ""; - - const resp = await this.http.authedRequest(method, path, queryParams, body, opts); - return await resp.text(); + private async rawJsonRequest(method: Method, path: string, queryParams: QueryDict, body: string): Promise { + const opts = { + // inhibit the JSON stringification and parsing within HttpApi. + json: false, + + // nevertheless, we are sending, and accept, JSON. + headers: { + "Content-Type": "application/json", + "Accept": "application/json", + }, + + // we use the full prefix + prefix: "", + }; + + return await this.http.authedRequest(method, path, queryParams, body, opts); } }