-
-
Notifications
You must be signed in to change notification settings - Fork 595
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
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
de99e20
Fix hashed ID server lookups with no Olm
dbkr 2d042d5
Test the code when crypto is available
dbkr fbf50d2
Test case of no crypto available
dbkr db2066a
Move digest file to src to get it out of the way of the olm / e2e stuff
dbkr 9931a15
Fix import
dbkr 7b00707
Fix error string & doc
dbkr e955341
subtle crypto, not webcrypto
dbkr 2e703e1
Extract the base64 part
dbkr f5c1695
Fix test
dbkr 4a7bb60
Merge branch 'develop' into dbkr/fix_id_server_lookup
dbkr bd0bb05
Move test file too
dbkr 2e7fb0d
Add more doc
dbkr 7584613
Fix imports
dbkr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
dbkr marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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 "../../../src"; | ||
import { sha256 } from "../../../src/digest"; | ||
|
||
describe("sha256", () => { | ||
it("should hash a string", async () => { | ||
const hash = await sha256("test"); | ||
expect(encodeUnpaddedBase64Url(hash)).toBe("n4bQgYhMfWWaL-qgxVrQFaO_TxsrC4Is0V1sFbDwCgg"); | ||
}); | ||
|
||
it("should hash a string with emoji", async () => { | ||
const hash = await sha256("test 🍱"); | ||
expect(encodeUnpaddedBase64Url(hash)).toBe("X2aDNrrwfq3nCTOl90R9qg9ynxhHnSzsMqtrdYX-SGw"); | ||
}); | ||
|
||
it("throws if webcrypto is not available", async () => { | ||
const oldCrypto = global.crypto; | ||
try { | ||
global.crypto = {} as any; | ||
await expect(sha256("test")).rejects.toThrow(); | ||
} finally { | ||
global.crypto = oldCrypto; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,7 +299,9 @@ describe("MatrixClient", function () { | |
...(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>); | ||
|
@@ -3358,4 +3360,45 @@ describe("MatrixClient", function () { | |
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"; | ||
|
||
|
||
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" }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
/** | ||
* @param plaintext The string to hash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does it do, though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd sort of assumed this was described completely by the @return param, but I can add a body too |
||
* @returns An ArrayBuffer containing the SHA-256 hash of the input string | ||
* @throws If the subtle crypto API is not available, for example if the code is running | ||
* in a web page with an insecure context (eg. served over plain HTTP). | ||
*/ | ||
export async function sha256(plaintext: string): Promise<ArrayBuffer> { | ||
if (!globalThis.crypto.subtle) { | ||
throw new Error("Crypto.subtle is not available: insecure context?"); | ||
} | ||
const utf8 = new TextEncoder().encode(plaintext); | ||
|
||
const digest = await globalThis.crypto.subtle.digest("SHA-256", utf8); | ||
|
||
return digest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.