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

ruma rejects Authorization: X-Matrix ... headers that include optional whitespace around parameter pairs #1801

Open
anoadragon453 opened this issue May 8, 2024 · 1 comment · May be fixed by #1805

Comments

@anoadragon453
Copy link

The spec states that the format of the Authorization header on the Federation API is given by RFC 7235. Part of that specification allows for zero or more spaces or tabs around the commas in the parameter list. For example, the following should be a valid Authorization header:

Authorization: X-Matrix origin=foo , key="ed25519:1",  sig="sig",		destination="bar"

Yet upon looking at ruma's source code, I believe it would reject this header:

Pair names or values are not trimmed for whitespace:

fn parse_xmatrix_field<'a>(tokens: &mut impl Tokens<Item = &'a u8>) -> Option<(String, Vec<u8>)> {
tokens.optional(|t| {
let name = parse_token(t).and_then(|name| {
let name = std::str::from_utf8(&name).ok()?.to_ascii_lowercase();
match name.as_str() {
"origin" | "destination" | "key" | "sig" => Some(name),
name => {
debug!(
"Encountered an invalid field name {} at location {}",
name,
t.location().offset()
);
None
}
}
})?;

And space or tab is not allowed in the name field:

fn is_tchar(c: u8) -> bool {
const TOKEN_CHARS: [u8; 15] =
[b'!', b'#', b'$', b'%', b'&', b'\'', b'*', b'+', b'-', b'.', b'^', b'_', b'`', b'|', b'~'];
is_alpha(c) || is_digit(c) || TOKEN_CHARS.contains(&c)
}

This fact was omitted from the summary of RFC 7235 in the spec, which is likely why it didn't make it into ruma as well. That is now being corrected: matrix-org/matrix-spec#1818

The code should be updated to allow whitespace in the parameter list in the header, such that any homeserver implementations following the spec would not have their federation requests rejected by homeservers that rely on ruma.

See matrix-org/matrix-spec#1817 for more context.

@zecakeh
Copy link
Contributor

zecakeh commented May 8, 2024

I am wondering if we should just use the http-auth crate instead of reimplementing a parser ourselves. It has a parser for RFC 7235 and all the features we don't need are behind cargo features.

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

Successfully merging a pull request may close this issue.

2 participants