From 2153be3f2418579926fd3b25cb3a4492ed3dbdb1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 5 Oct 2026 20:09:59 +0000 Subject: [PATCH] account: download the avatar from its own link The official TS6 client fetches the avatar map's names as they are (MyTeamSpeak::Account::request_own_myts_avatar hands each name to Avatar_Cache::request_avatar_from_urls, which GETs it without headers). requestAvatarSignedUrl signs upload links: its reply only goes to upload_avatar, an Http_Client::put. Voelin fetched such a link with a GET and was refused with HTTP 403. A refused download is now its own error ("avatar download returned HTTP ..."), apart from the account service's. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VZBWxxMsxUzp4PSTKVzbZN --- crates/voelin-myts/src/lib.rs | 24 ++++++--------- crates/voelin-myts/src/profile.rs | 48 +++++++++-------------------- crates/voelin-myts/src/tests.rs | 42 +++++++++++++++++++++++++ crates/voelin-myts/src/transport.rs | 6 ++-- crates/voelin-ui/src/myts.rs | 2 +- docs/research/myteamspeak.md | 29 ++++++++++++----- 6 files changed, 92 insertions(+), 59 deletions(-) diff --git a/crates/voelin-myts/src/lib.rs b/crates/voelin-myts/src/lib.rs index dc59167..5931b31 100644 --- a/crates/voelin-myts/src/lib.rs +++ b/crates/voelin-myts/src/lib.rs @@ -74,8 +74,10 @@ pub enum Error { MissingRenewalCredentials, #[error("account service refused the request (code {0})")] Refused(i32), - #[error("account service returned no usable avatar link")] + #[error("the account's avatar is not an HTTPS link")] AvatarUrl, + #[error("avatar download returned HTTP {0}")] + Download(u16), } impl Error { pub fn status_code(&self) -> Option { @@ -214,19 +216,13 @@ impl Client { profile::from_account_data(wire::decode(bytes)?) } - /// The picture of an avatar file of the signed-in user (one of - /// [`Profile::avatars`]): the service signs a download link - /// (`requestAvatarSignedUrl` at `/user`), which is then fetched. - pub async fn avatar(&self, token: &SessionToken, file_name: &str) -> Result, Error> { - transport::check_input_size(&[token.as_str(), file_name])?; - let request = api::user::AvatarSignedUrlRequest { - session: token.as_str().into(), - file_names: vec![file_name.into()], - }; - let bytes = - self.transport.call("user", "requestAvatarSignedUrl", &wire::encode(&request)).await?; - let url = profile::signed_url(wire::decode(bytes)?, file_name)?; - self.transport.download(&url, profile::MAX_AVATAR_BYTES).await + /// The picture of an avatar of the signed-in user (one of + /// [`Profile::avatars`]). An avatar's "file name" is its download link, + /// fetched as it is: the official client GETs it without headers. + /// (`requestAvatarSignedUrl` signs upload links, not download links.) + pub async fn avatar(&self, file_name: &str) -> Result, Error> { + let url = profile::avatar_link(file_name)?; + self.transport.download(url, profile::MAX_AVATAR_BYTES).await } pub async fn validate_session(&self, token: &SessionToken) -> Result<(), Error> { diff --git a/crates/voelin-myts/src/profile.rs b/crates/voelin-myts/src/profile.rs index 534c6d4..4deff05 100644 --- a/crates/voelin-myts/src/profile.rs +++ b/crates/voelin-myts/src/profile.rs @@ -21,7 +21,8 @@ pub struct Profile { /// The last sign-in before this one, Unix seconds (0: not told). pub last_login: i64, pub badges: Vec, - /// The avatar's file names, the one for "online" first. + /// The avatar's file names, the one for "online" first: each is the + /// picture's download link. pub avatars: Vec, /// Devices signed in to the account. pub devices: Vec, @@ -97,24 +98,16 @@ pub(crate) fn from_account_data(data: user::UserAccountData) -> Result Result { - check(response.return_code.as_ref())?; - let url = response - .signed_urls - .into_iter() - .find(|u| u.file_name == file_name || u.file_name.is_empty()) - .map(|u| u.signed_url) - .ok_or(Error::AvatarUrl)?; +/// An avatar file name as a download link: the official client +/// (`Avatar_Cache::request_avatar_from_urls`) GETs the names of the avatar +/// map as they are. Only HTTPS (and loopback HTTP in tests). +pub(crate) fn avatar_link(name: &str) -> Result<&str, Error> { let allowed = - url.starts_with("https://") || (cfg!(test) && url.starts_with("http://127.0.0.1:")); - if !allowed || url.len() > 4096 { + name.starts_with("https://") || (cfg!(test) && name.starts_with("http://127.0.0.1:")); + if !allowed || name.len() > 4096 { return Err(Error::AvatarUrl); } - Ok(url) + Ok(name) } /// A refusal: `success` unset with an error code. (A reply without a return @@ -204,26 +197,15 @@ mod tests { }; let error = from_account_data(refused).unwrap_err(); assert!(error.is_invalid_session()); - let response = |url: &str| user::AvatarSignedUrlResponse { - return_code: Some(user::ReturnCode { success: true, ..Default::default() }), - signed_urls: vec![user::avatar_signed_url_response::SignedUrl { - file_name: "a.png".into(), - signed_url: url.into(), - }], - }; assert_eq!( - signed_url(response("https://cdn.example.test/a.png?sig=1"), "a.png").unwrap(), + avatar_link("https://cdn.example.test/a.png?sig=1").unwrap(), "https://cdn.example.test/a.png?sig=1" ); - assert!(matches!( - signed_url(response("http://cdn.example.test/a"), "a.png"), - Err(Error::AvatarUrl) - )); - assert!(matches!( - signed_url(response("file:///etc/passwd"), "a.png"), - Err(Error::AvatarUrl) - )); - assert!(matches!(signed_url(response("https://x"), "b.png"), Err(Error::AvatarUrl))); + for bad in ["http://cdn.example.test/a", "file:///etc/passwd", "a.png", ""] { + assert!(matches!(avatar_link(bad), Err(Error::AvatarUrl)), "{bad}"); + } + let long = format!("https://cdn.example.test/{}", "a".repeat(4096)); + assert!(matches!(avatar_link(&long), Err(Error::AvatarUrl))); } #[test] diff --git a/crates/voelin-myts/src/tests.rs b/crates/voelin-myts/src/tests.rs index 406d699..5bc02f4 100644 --- a/crates/voelin-myts/src/tests.rs +++ b/crates/voelin-myts/src/tests.rs @@ -357,3 +357,45 @@ async fn unsupported_identity_encryption_preserves_successful_account_login() { assert_eq!(login.identity.unwrap_err(), IdentityError::UnsupportedKeyVersion); server.await.unwrap(); } + +/// One GET answered with `status` and `body`; the request's head comes back. +async fn mock_get(status: u16, body: &'static [u8]) -> (String, tokio::task::JoinHandle) { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}/avatars/a.png?sig=1", listener.local_addr().unwrap()); + let task = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + let mut request = Vec::new(); + while !request.ends_with(b"\r\n\r\n") { + let mut byte = [0]; + stream.read_exact(&mut byte).await.unwrap(); + request.push(byte[0]); + assert!(request.len() < 8192); + } + let head = format!( + "HTTP/1.1 {status} Test\r\nContent-Type: image/png\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ); + let _ = stream.write_all(head.as_bytes()).await; + let _ = stream.write_all(body).await; + String::from_utf8(request).unwrap() + }); + (url, task) +} + +#[tokio::test] +async fn an_avatar_is_fetched_from_its_own_link() { + let client = + Client { transport: transport::Transport::mock(String::new(), Duration::from_secs(2)) }; + // The file name is the link: a plain GET of it, nothing of the session. + let (url, task) = mock_get(200, b"\x89PNG picture").await; + assert_eq!(client.avatar(&url).await.unwrap(), b"\x89PNG picture"); + let head = task.await.unwrap(); + assert!(head.starts_with("GET /avatars/a.png?sig=1 HTTP/1.1\r\n"), "{head}"); + assert!(!head.to_ascii_lowercase().contains("authorization"), "{head}"); + // A refused download says so, apart from the account service's errors. + let (url, _task) = mock_get(403, b"AccessDenied").await; + let error = client.avatar(&url).await.unwrap_err(); + assert!(matches!(error, Error::Download(403)), "{error:?}"); + // A name that is not a link is never fetched. + assert!(matches!(client.avatar("online.png").await, Err(Error::AvatarUrl))); +} diff --git a/crates/voelin-myts/src/transport.rs b/crates/voelin-myts/src/transport.rs index 800f403..6f9a5f4 100644 --- a/crates/voelin-myts/src/transport.rs +++ b/crates/voelin-myts/src/transport.rs @@ -49,13 +49,13 @@ impl Transport { } } - /// GET a file from a link the service handed out (a signed avatar link), - /// at most `max` bytes. + /// GET a file from a link the service handed out (an avatar's), at most + /// `max` bytes. pub(crate) async fn download(&self, url: &str, max: usize) -> Result, Error> { tokio::time::timeout(self.timeout, async { let mut response = self.client.get(url).send().await?; if !response.status().is_success() { - return Err(Error::Http(response.status().as_u16())); + return Err(Error::Download(response.status().as_u16())); } if response.content_length().is_some_and(|length| length > max as u64) { return Err(Error::ResponseTooLarge); diff --git a/crates/voelin-ui/src/myts.rs b/crates/voelin-ui/src/myts.rs index e8ea1c2..af7739d 100644 --- a/crates/voelin-ui/src/myts.rs +++ b/crates/voelin-ui/src/myts.rs @@ -446,7 +446,7 @@ impl App { }); // The picture only when it changed (or was never fetched). let Some(name) = avatar.filter(|name| *name != shown || !has_picture) else { return }; - match client.avatar(&token, &name).await { + match client.avatar(&name).await { Ok(bytes) => later(move |app| { if app.myts.generation == generation && app.myts.signed_in { app.myts.set_avatar(name, &bytes); diff --git a/docs/research/myteamspeak.md b/docs/research/myteamspeak.md index 53c0128..22bc286 100644 --- a/docs/research/myteamspeak.md +++ b/docs/research/myteamspeak.md @@ -100,14 +100,27 @@ After a sign-in or a validated saved session the app asks the user service, client next to `/authentication` and `/session`), for `getAccountData` with the basic info, avatars, description, badges and authenticated devices, and shows them in Settings → My Account (and the avatar in the sidebar's profile when -the server has none for us). `requestAvatarSignedUrl` turns the avatar's file -name (the "online" one first) into a signed HTTPS link, which is fetched -(at most 4 MiB) and kept in `/account/avatar` for the next start; it is -fetched again only when the file name changes. The login response's own -avatar file names and description are used until then. What is kept goes into -the same secret-store bundle as the session (`profile`), never the token into -a URL. Neither call has been run against the live service from this -environment. +the server has none for us). An avatar's file name (the "online" one first) +is its download link: it is fetched as it is with a plain GET, HTTPS only, at +most 4 MiB, and kept in `/account/avatar` for the next start; it is +fetched again only when the name changes. The login response's own avatar +file names and description are used until then. What is kept goes into the +same secret-store bundle as the session (`profile`), never the token into a +URL. + +`requestAvatarSignedUrl` is not a download call: it signs *upload* links. +In the TS6 6.0.0-beta4.1 Linux client, `Account_Manager_Impl:: +request_avatar_signed_urls`'s reply goes only to `upload_avatar`, whose +upload (`Cloud_Sync_Impl::upload_avatar_to_intermediate_bucket`) is an +`http_v2::Http_Client::put`, followed by `requestUploadAvatar`. The client's +own avatar comes from `MyTeamSpeak::Account::request_own_myts_avatar` +(0x1ae1a50): it parses the stored `AvatarData` and hands each map entry's +state and name, unchanged, to `Avatar_Cache::request_avatar_from_urls` +(0x12d6e20), which passes the name as the URL to `Http_Client::get` with an +empty header list (0x12d78c1). An early Voelin fetched the signed link with +a GET instead and was refused with HTTP 403 by the live service; a refused +download is now logged as "avatar download returned HTTP …", apart from +the account service's errors. Sign-out returns to login, clears local account material and attempts remote session deletion; storage or remote errors are reported. A late completion