Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hashed ID server lookups with no Olm #4333

Merged
merged 13 commits into from
Aug 1, 2024
29 changes: 29 additions & 0 deletions spec/unit/crypto/digest.spec.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this out of crypto too, please.

dbkr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { sha256Base64UrlUnpadded } from "../../../src/crypto/digest";

describe("sha256Base64UrlUnpadded", () => {
it("should hash a string", async () => {
const hash = await sha256Base64UrlUnpadded("test");
expect(hash).toBe("n4bQgYhMfWWaL-qgxVrQFaO_TxsrC4Is0V1sFbDwCgg");
});

it("should hash a string with emoji", async () => {
const hash = await sha256Base64UrlUnpadded("test 🍱");
expect(hash).toBe("X2aDNrrwfq3nCTOl90R9qg9ynxhHnSzsMqtrdYX-SGw");
});
});
45 changes: 44 additions & 1 deletion spec/unit/matrix-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@
...(opts || {}),
});
// FIXME: We shouldn't be yanking http like this.
client.http = (["authedRequest", "getContentUri", "request", "uploadContent"] as const).reduce((r, k) => {
client.http = (
["authedRequest", "getContentUri", "request", "uploadContent", "idServerRequest"] as const
).reduce((r, k) => {
r[k] = jest.fn();
return r;
}, {} as MatrixHttpApi<any>);
Expand Down Expand Up @@ -3035,4 +3037,45 @@
expect(httpLookups.length).toEqual(0);
});
});

describe("identityHashedLookup", () => {
it("should return hashed lookup results", async () => {
const ID_ACCESS_TOKEN = "hello_id_server_please_let_me_make_a_request";
Dismissed Show dismissed Hide dismissed

client.http.idServerRequest = jest.fn().mockImplementation((method, path, params) => {
if (method === "GET" && path === "/hash_details") {
return { algorithms: ["sha256"], lookup_pepper: "carrot" };
} else if (method === "POST" && path === "/lookup") {
return {
mappings: {
"WHA-MgrrsZACDI9F8OaVagpiyiV2sjZylGHJteT4OMU": "@bob:homeserver.dummy",
},
};
}

throw new Error("Test impl doesn't know about this request");
});

const lookupResult = await client.identityHashedLookup([["[email protected]", "email"]], ID_ACCESS_TOKEN);

expect(client.http.idServerRequest).toHaveBeenCalledWith(
"GET",
"/hash_details",
undefined,
"/_matrix/identity/v2",
ID_ACCESS_TOKEN,
);

expect(client.http.idServerRequest).toHaveBeenCalledWith(
"POST",
"/lookup",
{ pepper: "carrot", algorithm: "sha256", addresses: ["WHA-MgrrsZACDI9F8OaVagpiyiV2sjZylGHJteT4OMU"] },
"/_matrix/identity/v2",
ID_ACCESS_TOKEN,
);

expect(lookupResult).toHaveLength(1);
expect(lookupResult[0]).toEqual({ address: "[email protected]", mxid: "@bob:homeserver.dummy" });
});
});
});
27 changes: 13 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ import { KnownMembership, Membership } from "./@types/membership";
import { RoomMessageEventContent, StickerEventContent } from "./@types/events";
import { ImageInfo } from "./@types/media";
import { Capabilities, ServerCapabilities } from "./serverCapabilities";
import { sha256Base64UrlUnpadded } from "./crypto/digest";

export type Store = IStore;

Expand Down Expand Up @@ -9302,20 +9303,18 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

// When picking an algorithm, we pick the hashed over no hashes
if (hashes["algorithms"].includes("sha256")) {
// Abuse the olm hashing
const olmutil = new global.Olm.Utility();
params["addresses"] = addressPairs.map((p) => {
const addr = p[0].toLowerCase(); // lowercase to get consistent hashes
const med = p[1].toLowerCase();
const hashed = olmutil
.sha256(`${addr} ${med} ${params["pepper"]}`)
.replace(/\+/g, "-")
.replace(/\//g, "_"); // URL-safe base64
// Map the hash to a known (case-sensitive) address. We use the case
// sensitive version because the caller might be expecting that.
localMapping[hashed] = p[0];
return hashed;
});
params["addresses"] = await Promise.all(
addressPairs.map(async (p) => {
const addr = p[0].toLowerCase(); // lowercase to get consistent hashes
const med = p[1].toLowerCase();
const hashed = await sha256Base64UrlUnpadded(`${addr} ${med} ${params["pepper"]}`);

// Map the hash to a known (case-sensitive) address. We use the case
// sensitive version because the caller might be expecting that.
localMapping[hashed] = p[0];
return hashed;
}),
);
params["algorithm"] = "sha256";
} else if (hashes["algorithms"].includes("none")) {
params["addresses"] = addressPairs.map((p) => {
Expand Down
32 changes: 32 additions & 0 deletions src/crypto/digest.ts
richvdh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { encodeUnpaddedBase64Url } from "../base64";

/**
* @param plaintext The string to hash
* @returns Unpadded base64-url string representing the sha256 hash of the input
*/
export async function sha256Base64UrlUnpadded(plaintext: string): Promise<string> {
if (!globalThis.crypto.subtle) {
throw new Error("No WebCrypto available");
richvdh marked this conversation as resolved.
Show resolved Hide resolved
}
dbkr marked this conversation as resolved.
Show resolved Hide resolved
const utf8 = new TextEncoder().encode(plaintext);

const digest = await globalThis.crypto.subtle.digest("SHA-256", utf8);

return encodeUnpaddedBase64Url(digest);
}
9 changes: 2 additions & 7 deletions src/oidc/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
validateIdToken,
validateStoredUserState,
} from "./validate";
import { sha256Base64UrlUnpadded } from "../crypto/digest";

// reexport for backwards compatibility
export type { BearerTokenResponse };
Expand Down Expand Up @@ -61,14 +62,8 @@ const generateCodeChallenge = async (codeVerifier: string): Promise<string> => {
logger.warn("A secure context is required to generate code challenge. Using plain text code challenge");
return codeVerifier;
}
const utf8 = new TextEncoder().encode(codeVerifier);

const digest = await globalThis.crypto.subtle.digest("SHA-256", utf8);

return btoa(String.fromCharCode(...new Uint8Array(digest)))
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
return sha256Base64UrlUnpadded(codeVerifier);
};

/**
Expand Down