close
Skip to content

fix(sdk): Use the server name from the user id as a fallback URL for fetching the well-known info#5996

Merged
jmartinesp merged 3 commits into
mainfrom
fix/use-server-name-from-user-id-to-fetch-well-known-if-everything-else-fails
Jan 7, 2026
Merged

fix(sdk): Use the server name from the user id as a fallback URL for fetching the well-known info#5996
jmartinesp merged 3 commits into
mainfrom
fix/use-server-name-from-user-id-to-fetch-well-known-if-everything-else-fails

Conversation

@jmartinesp
Copy link
Copy Markdown
Contributor

@jmartinesp jmartinesp commented Jan 5, 2026

When using Client::fetch_client_well_known, use the server name in the user id as a fallback value if the server name is missing. If it's not there or this first attempt fails, use the homeserver url instead as we were doing so far.

Fixes #5877 (or workarounds it).

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 5, 2026

Codecov Report

❌ Patch coverage is 91.30435% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.97%. Comparing base (cd9f433) to head (81705e8).
⚠️ Report is 26 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/client/mod.rs 91.30% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5996      +/-   ##
==========================================
+ Coverage   88.59%   88.97%   +0.38%     
==========================================
  Files         364      359       -5     
  Lines      104341    99318    -5023     
  Branches   104341    99318    -5023     
==========================================
- Hits        92438    88370    -4068     
+ Misses       7537     6956     -581     
+ Partials     4366     3992     -374     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 5, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing fix/use-server-name-from-user-id-to-fetch-well-known-if-everything-else-fails (81705e8) with main (cc622a5)

Summary

✅ 50 untouched benchmarks

@jmartinesp jmartinesp marked this pull request as ready for review January 5, 2026 12:18
@jmartinesp jmartinesp requested a review from a team as a code owner January 5, 2026 12:18
@jmartinesp jmartinesp requested review from poljar and removed request for a team January 5, 2026 12:18
Copy link
Copy Markdown
Contributor

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Thanks. One question!

Comment thread crates/matrix-sdk/src/client/mod.rs Outdated
.to_owned();

// First try using the server name or homeserver url
match self.fetch_client_well_known_with_url(server_url.to_string()).await {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it make sense to try with the server name first? We know that this can fail, and when this is failing, this is precisely why we need a .well-known file (to discover the homeserver API endpoint).

Maybe this is useful for the first attempt, when login in, and the userId is not known yet? In this case server_url is not resolved yet to the CS endpoint?

The .well-known URL should always be computed from the userId domain part, and this should never fail if the file exists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's the alternative I mentioned in the PR description. I kept the previous structure adding this 2nd attempt, but maybe it's better to always try first with the server name, explicit or implicit, then the homeserver url as the fallback 🤔 .

Copy link
Copy Markdown
Contributor Author

@jmartinesp jmartinesp Jan 5, 2026

Choose a reason for hiding this comment

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

That said, if we extract the server name from the user id and we have no other url to check... which scheme should we add for the constructed URL? Should we enforce https, or just default to plain http, just in case the server does not use HTTPS?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd go for https only

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd agree with that, but I think I'll try to reuse the scheme in the homeserver url, since otherwise we won't be able to test this behaviour (the network mocking layer in the tests doesn't work with HTTPS if I'm not mistaken).

@jmartinesp jmartinesp force-pushed the fix/use-server-name-from-user-id-to-fetch-well-known-if-everything-else-fails branch from d64ecfc to 0ad1350 Compare January 5, 2026 14:27
Comment thread crates/matrix-sdk/src/client/mod.rs Outdated
Copy link
Copy Markdown
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Looks good to me. I thought we were already doing that… anyway. Thanks for the tests too!

Can you explain improve the comments to explain why we are doing this? I believe the explanations must live in the documentation of the method itself rather than an inline comment.

Finally, the CHANGELOG.md expects a new entry 😃.

Comment on lines +2052 to +2057
// Use the server name, either an explicit one or an implicit one taken from
// the user id
let server_url = self
.server()
.unwrap_or(
// Sometimes people configure their well-known directly on the homeserver so use
// this as a fallback when the server name is unknown.
&self.homeserver(),
)
.to_string();
.map(|server| server.to_string())
.or_else(|| self.user_id().map(|id| format!("{}://{}", scheme, id.server_name())));
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 believe this part deserves a bit more explanations about why we are doing this, otherwise it looks good!

@jmartinesp
Copy link
Copy Markdown
Contributor Author

Done, thanks: I added some more inline comments as well as doc comments. I also added the changelog entry, I usually do that once the approach used in the PR has been validated, so I don't have to also change it every time the logic in the PR does 😅 .

@jmartinesp jmartinesp requested a review from Hywan January 7, 2026 09:24
@andybalaam
Copy link
Copy Markdown
Member

For reference, the spec for this behaviour is at https://spec.matrix.org/v1.17/client-server-api/#well-known-uris I think. If you haven't, it would be worth reading through and checking we comply with it.

I looked it up because I thought we weren't allowed to guess hostname from Matrix ID, but it looks like in this case we are.

Copy link
Copy Markdown
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Great!

@jmartinesp jmartinesp force-pushed the fix/use-server-name-from-user-id-to-fetch-well-known-if-everything-else-fails branch from 5e5700f to 81705e8 Compare January 7, 2026 10:07
@jmartinesp jmartinesp merged commit 48d1d1f into main Jan 7, 2026
52 checks passed
@jmartinesp jmartinesp deleted the fix/use-server-name-from-user-id-to-fetch-well-known-if-everything-else-fails branch January 7, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent HomeserverConfig::discover from performing the discover request if previous server info exists

5 participants