Skip to content

Rust Fed Client: Build the X-Matrix Authorization Header in Rust - #20545

Draft
devonh wants to merge 2 commits into
developfrom
fed-client/pr01-request-signing
Draft

devonh wants to merge 2 commits into
developfrom
fed-client/pr01-request-signing

Conversation

@devonh

@devonh devonh commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

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
@devonh
devonh force-pushed the fed-client/pr01-request-signing branch from 1d423e6 to 355ff18 Compare November 20, 2026 23:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FqDpJcwqvswQeVKt2FkwAo
@devonh
devonh requested a review from erikjohnston November 21, 2026 20:23

@erikjohnston erikjohnston left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall structure looks good, just a few nits

Comment on lines +15 to +16
//! The Python edge: every pyclass, and every conversion between Python and
//! the plain-Rust modules.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
//! 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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Suggested change
fn ascii<'a>(name: &str, bytes: &'a [u8]) -> PyResult<&'a str> {
fn to_ascii<'a>(name: &str, bytes: &'a [u8]) -> PyResult<&'a str> {

Comment on lines +37 to +38
#[pyclass(frozen, name = "RequestSigner")]
struct PyRequestSigner {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These could all use some docstrings, even if they're just links to the other classes.

Comment on lines +74 to +78
let header_destination = [input.destination, input.destination_is]
.into_iter()
.flatten()
.find(|d| !d.is_empty())
.ok_or(SigningError::NoDestination)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

May as well also check if both are set.

Suggested change
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,
))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this a drive-by fix or did we need it for some reason? Feels like a separate PR naively

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants