Repository navigation
Conversation
The X-Matrix Authorization header for outbound federation requests is built by a Rust RequestSigner from the same canonical body bytes that are sent, so each request body is canonical-encoded once instead of twice. The header is byte-identical to the one signedjson built. Destinations whose port is not 1-5 ASCII digits up to 65535 are now refused with FederationDeniedError before the retry limiter, as the spec's server-name grammar requires; parse_and_validate_server_name accepts any port int() does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FqDpJcwqvswQeVKt2FkwAo
1d423e6 to
355ff18
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FqDpJcwqvswQeVKt2FkwAo
erikjohnston
left a comment
There was a problem hiding this comment.
Overall structure looks good, just a few nits
| //! The Python edge: every pyclass, and every conversion between Python and | ||
| //! the plain-Rust modules. |
There was a problem hiding this comment.
| //! The Python edge: every pyclass, and every conversion between Python and | |
| //! the plain-Rust modules. | |
| //! The Python bindings |
| } | ||
| } | ||
|
|
||
| fn ascii<'a>(name: &str, bytes: &'a [u8]) -> PyResult<&'a str> { |
There was a problem hiding this comment.
Generally conversion methods like this are prefixed with to_*, c.f. https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
| fn ascii<'a>(name: &str, bytes: &'a [u8]) -> PyResult<&'a str> { | |
| fn to_ascii<'a>(name: &str, bytes: &'a [u8]) -> PyResult<&'a str> { |
| #[pyclass(frozen, name = "RequestSigner")] | ||
| struct PyRequestSigner { |
There was a problem hiding this comment.
These could all use some docstrings, even if they're just links to the other classes.
| let header_destination = [input.destination, input.destination_is] | ||
| .into_iter() | ||
| .flatten() | ||
| .find(|d| !d.is_empty()) | ||
| .ok_or(SigningError::NoDestination)?; |
There was a problem hiding this comment.
May as well also check if both are set.
| let header_destination = [input.destination, input.destination_is] | |
| .into_iter() | |
| .flatten() | |
| .find(|d| !d.is_empty()) | |
| .ok_or(SigningError::NoDestination)?; | |
| // Extract the destination from exactly one of the fields. | |
| let header_destination = match (input.destination, input.destination_is) { | |
| (Some(d), None) if !d.is_empty() => Ok(d), | |
| (None, Some(d)) if !d.is_empty() => Ok(d), | |
| _ => Err(SigningError::NoDestination), | |
| }; |
| .serialize(&mut serde_json::Serializer::with_formatter( | ||
| &mut fields, | ||
| CanonicalFormatter, | ||
| )) |
There was a problem hiding this comment.
I think you want to use to_vec_canonical? Otherwise you don't get the full canonical json rules?
| .expect("a struct of strings always serializes"); | ||
|
|
||
| let message = match input.content { | ||
| Some(content) => [b"{\"content\":".as_slice(), content, b",", &fields[1..]].concat(), |
There was a problem hiding this comment.
Hmmmmmm. I guess this is fine for now, but only on the assumption that we'll want to get rid of this in a later round of PRs, which I think we will?
|
|
||
| use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _}; | ||
| use http::HeaderValue; | ||
| use ring::signature::Ed25519KeyPair; |
There was a problem hiding this comment.
I'd use ed25519-dalek here. It's what we use in https://github.com/erikjohnston/rust-signed-json and is a lot more lightweight than ring. There's also some subtleties with verification around malleability, which we'll need to worry about when we come to doing any form of verification. I don't think ring supports that. c.f. https://docs.rs/ed25519-dalek/latest/ed25519_dalek/struct.VerifyingKey.html#method.verify_strict
| and port <= 65535 | ||
| ): | ||
| raise ValueError("Invalid port in destination '%s'" % (destination,)) | ||
| return host, port |
There was a problem hiding this comment.
Is this a drive-by fix or did we need it for some reason? Feels like a separate PR naively
This PR is the first step towards doing outbound federation requests in rust.
It builds out the functionality required to add the X-Matrix authentication header to requests, and replaces the existing python usage.
There are tests in place to ensure direct compatibility with the existing python implementation.
Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.