mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-12-04 15:46:07 +00:00
fix: keep the Telegram bot token out of logs and off TLS fallback
The Telegram bot API carries the bot token in the URL path. Telegram requests went through post_request, which logs the failed reqwest error, URL included, at warn level before retrying and then returns it, and the 2FA path logs it again. Whenever api.telegram.org was unreachable, each 2FA login wrote the token to the log file. On a TLS error post_request also retried with invalid certificates accepted, so whoever presented a certificate on the path received the token and the 2FA code. Send Telegram requests once with verified rustls, without fallback, and drop the URL from their errors. Other post_request callers are unchanged. Revert the Zeroizing session password: the plaintext password is also held by the Dart UI, the login dialog path and argv, so wiping the preset copy changed nothing observable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JAxkkig1bcC5Xrr1uwX47b
This commit is contained in:
1 parent
8f2e25f837
commit
047bbcc7ca
7 files changed
+63
-16
No files matched your search
Generated
-1
@@ -7173,7 +7173,6 @@ dependencies = [
|
||||
"x11rb 0.12.0",
|
||||
"xkbcommon-dl",
|
||||
"xxhash-rust",
|
||||
"zeroize",
|
||||
"zstd",
|
||||
]
|
||||
|
||||
|
||||
@@ -61,7 +61,6 @@ serde_repr = "0.1"
|
||||
cfg-if = "1.0"
|
||||
lazy_static = "1.4"
|
||||
sha2 = "0.10"
|
||||
zeroize = "1.8"
|
||||
repng = "0.2"
|
||||
zstd = "0.13"
|
||||
xxhash-rust = { version = "0.8", features = ["xxh3"] }
|
||||
|
||||
+53
-5
@@ -4,10 +4,11 @@ use hbb_common::{
|
||||
config::Config,
|
||||
get_time,
|
||||
password_security::{decrypt_vec_or_original, encrypt_vec_or_original},
|
||||
tls::TlsType,
|
||||
ResultType,
|
||||
};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::{fmt, sync::Mutex};
|
||||
use std::{fmt, sync::Mutex, time::Duration};
|
||||
use totp_rs::{Algorithm, Secret, TOTP};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
@@ -164,18 +165,39 @@ impl TelegramBot {
|
||||
}
|
||||
}
|
||||
|
||||
// The URL carries the bot token: never fall back to accepting an invalid certificate, and drop
|
||||
// the URL from errors so the token stays out of the log.
|
||||
async fn post_telegram(url: String, body: String) -> ResultType<String> {
|
||||
let resp = crate::hbbs_http::create_http_client_async(TlsType::Rustls, false)
|
||||
.post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body)
|
||||
.timeout(Duration::from_secs(12))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| anyhow!("{:?}", e.without_url()))?;
|
||||
resp.text()
|
||||
.await
|
||||
.map_err(|e| anyhow!("{:?}", e.without_url()))
|
||||
}
|
||||
|
||||
// https://gist.github.com/dideler/85de4d64f66c1966788c1b2304b9caf1
|
||||
pub async fn send_2fa_code_to_telegram(text: &str, bot: TelegramBot) -> ResultType<()> {
|
||||
let url = format!("https://api.telegram.org/bot{}/sendMessage", bot.token_str);
|
||||
let params = serde_json::json!({"chat_id": bot.chat_id, "text": text});
|
||||
crate::post_request(url, params.to_string(), "").await?;
|
||||
post_telegram(url, params.to_string()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>> {
|
||||
let url = format!("https://api.telegram.org/bot{}/getUpdates", bot_token);
|
||||
// because caller is in tokio runtime, so we must call post_request_sync in new thread.
|
||||
let handle = std::thread::spawn(move || crate::post_request_sync(url, "".to_owned(), ""));
|
||||
// because caller is in tokio runtime, so we must run the request in a new thread.
|
||||
let handle = std::thread::spawn(move || -> ResultType<String> {
|
||||
hbb_common::tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?
|
||||
.block_on(post_telegram(url, String::new()))
|
||||
});
|
||||
let resp = handle.join().map_err(|_| anyhow!("Thread panicked"))??;
|
||||
let value = serde_json::from_str::<serde_json::Value>(&resp).map_err(|e| anyhow!(e))?;
|
||||
|
||||
@@ -215,7 +237,12 @@ pub fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::TelegramBot;
|
||||
use super::{post_telegram, TelegramBot};
|
||||
use hbb_common::tokio::{self, net::TcpListener};
|
||||
use std::sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn telegram_bot_debug_redacts_both_token_fields() {
|
||||
@@ -230,4 +257,25 @@ mod tests {
|
||||
assert!(!debug.contains(&format!("{:?}", bot.token)));
|
||||
assert!(debug.contains(&bot.chat_id));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn telegram_request_has_no_tls_fallback_and_hides_the_token() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let attempts = Arc::new(AtomicUsize::new(0));
|
||||
let counter = attempts.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Ok((stream, _)) = listener.accept().await {
|
||||
counter.fetch_add(1, Ordering::SeqCst);
|
||||
drop(stream);
|
||||
}
|
||||
});
|
||||
let token = "123456:test-telegram-token";
|
||||
let url = format!("https://127.0.0.1:{port}/bot{token}/sendMessage");
|
||||
|
||||
let err = post_telegram(url, String::new()).await.unwrap_err();
|
||||
|
||||
assert!(!err.to_string().contains(token), "{err}");
|
||||
assert_eq!(attempts.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
}
|
||||
@@ -1732,7 +1732,7 @@ impl<T: InvokeUiSession> Remote<T> {
|
||||
if !self
|
||||
.handler
|
||||
.handle_hash(
|
||||
self.handler.password.as_str(),
|
||||
&self.handler.password.clone(),
|
||||
hash,
|
||||
&mut self.trusted_device,
|
||||
peer,
|
||||
|
||||
+6
-4
@@ -1345,15 +1345,17 @@ pub fn session_add(
|
||||
|
||||
LocalConfig::set_remote_id(&id);
|
||||
|
||||
let (preset_password, shared_password) = if is_shared_password {
|
||||
let mut preset_password = password.clone();
|
||||
let shared_password = if is_shared_password {
|
||||
// To achieve a flexible password application order, we don't treat shared password as a preset password.
|
||||
(String::new(), Some(password))
|
||||
preset_password = Default::default();
|
||||
Some(password)
|
||||
} else {
|
||||
(password, None)
|
||||
None
|
||||
};
|
||||
|
||||
let session: Session<FlutterHandler> = Session {
|
||||
password: preset_password.into(),
|
||||
password: preset_password,
|
||||
server_keyboard_enabled: Arc::new(RwLock::new(true)),
|
||||
server_file_transfer_enabled: Arc::new(RwLock::new(true)),
|
||||
server_clipboard_enabled: Arc::new(RwLock::new(true)),
|
||||
|
||||
+1
-1
@@ -597,7 +597,7 @@ impl SciterSession {
|
||||
pub fn new(cmd: String, id: String, password: String, args: Vec<String>) -> Self {
|
||||
let force_relay = args.contains(&"--relay".to_string());
|
||||
let session: Session<SciterHandler> = Session {
|
||||
password: password.into(),
|
||||
password: password.clone(),
|
||||
args,
|
||||
server_keyboard_enabled: Arc::new(RwLock::new(true)),
|
||||
server_file_transfer_enabled: Arc::new(RwLock::new(true)),
|
||||
|
||||
@@ -40,7 +40,6 @@ use std::{
|
||||
time::SystemTime,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::client::io_loop::Remote;
|
||||
use crate::client::{
|
||||
@@ -61,7 +60,7 @@ const CHANGE_RESOLUTION_VALID_TIMEOUT_SECS: u64 = 15;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Session<T: InvokeUiSession> {
|
||||
pub password: Zeroizing<String>,
|
||||
pub password: String,
|
||||
pub args: Vec<String>,
|
||||
pub lc: Arc<RwLock<LoginConfigHandler>>,
|
||||
pub sender: Arc<RwLock<Option<mpsc::UnboundedSender<Data>>>>,
|
||||
@@ -2092,7 +2091,7 @@ async fn start_one_port_forward<T: InvokeUiSession>(
|
||||
) {
|
||||
if let Err(err) = crate::port_forward::listen(
|
||||
handler.get_id(),
|
||||
handler.password.as_str().to_owned(),
|
||||
handler.password.clone(),
|
||||
port,
|
||||
handler.clone(),
|
||||
receiver,
|
||||
|
||||
Reference in new issue
Block a user