Commit 167ad8aae5
Unsigned
Layout: unified · split
src/server.rs +57 −8
| @@ -107,6 +107,16 @@ use crate::{ | ||
| 107 | 107 | ConfigHandle, Servers, PLUGIN_NAME, |
| 108 | 108 | }; |
| 109 | 109 | |
| 110 | fn secure_set_token_command(name: &str, token: &str) -> Option<String> { | |
| 111 | let safe = !token.is_empty() | |
| 112 | && token.chars().all(|character| { | |
| 113 | character.is_ascii_alphanumeric() | |
| 114 | || matches!(character, '-' | '.' | '_' | '~' | '+' | '/' | '=') | |
| 115 | }); | |
| 116 | ||
| 117 | safe.then(|| format!("/secure set {name} {token}")) | |
| 118 | } | |
| 119 | ||
| 110 | 120 | fn with_entered_runtime_until_final_drop<F>( |
| 111 | 121 | runtime: Rc<tokio::runtime::Runtime>, |
| 112 | 122 | f: F, |
| @@ -1374,15 +1384,17 @@ impl InnerServer { | ||
| 1374 | 1384 | return; |
| 1375 | 1385 | }; |
| 1376 | 1386 | |
| 1377 | let quote = |value: &str| { | |
| 1378 | format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\"")) | |
| 1387 | let Some(access_command) = | |
| 1388 | secure_set_token_command(&access_name, &tokens.access_token) | |
| 1389 | else { | |
| 1390 | self.print_error( | |
| 1391 | "Refusing to persist an invalid Matrix access token", | |
| 1392 | ); | |
| 1393 | return; | |
| 1379 | 1394 | }; |
| 1380 | 1395 | |
| 1381 | 1396 | let commands = [ |
| 1382 | format!( | |
| 1383 | "/secure set {access_name} {}", | |
| 1384 | quote(&tokens.access_token) | |
| 1385 | ), | |
| 1397 | access_command, | |
| 1386 | 1398 | format!( |
| 1387 | 1399 | "/set matrix-rust.server.{}.access_token \ |
| 1388 | 1400 | \"${{sec.data.{access_name}}}\"", |
| @@ -1400,8 +1412,17 @@ impl InnerServer { | ||
| 1400 | 1412 | } |
| 1401 | 1413 | |
| 1402 | 1414 | if let Some(refresh_token) = tokens.refresh_token { |
| 1415 | let Some(refresh_command) = | |
| 1416 | secure_set_token_command(&refresh_name, &refresh_token) | |
| 1417 | else { | |
| 1418 | self.print_error( | |
| 1419 | "Refusing to persist an invalid Matrix refresh token", | |
| 1420 | ); | |
| 1421 | return; | |
| 1422 | }; | |
| 1423 | ||
| 1403 | 1424 | let commands = [ |
| 1404 | format!("/secure set {refresh_name} {}", quote(&refresh_token)), | |
| 1425 | refresh_command, | |
| 1405 | 1426 | format!( |
| 1406 | 1427 | "/set matrix-rust.server.{}.refresh_token \ |
| 1407 | 1428 | \"${{sec.data.{refresh_name}}}\"", |
| @@ -2381,7 +2402,7 @@ impl InnerServer { | ||
| 2381 | 2402 | #[cfg(test)] |
| 2382 | 2403 | mod tests { |
| 2383 | 2404 | use super::{ |
| 2384 | create_room_request, missing_alias_action, | |
| 2405 | create_room_request, missing_alias_action, secure_set_token_command, | |
| 2385 | 2406 | with_entered_runtime_until_drop, InnerServer, MissingAliasAction, |
| 2386 | 2407 | }; |
| 2387 | 2408 | use matrix_sdk::ruma::{OwnedRoomAliasId, OwnedUserId}; |
| @@ -2411,6 +2432,34 @@ mod tests { | ||
| 2411 | 2432 | } |
| 2412 | 2433 | } |
| 2413 | 2434 | |
| 2435 | #[test] | |
| 2436 | fn secure_token_command_does_not_store_literal_quotes() { | |
| 2437 | assert_eq!( | |
| 2438 | secure_set_token_command( | |
| 2439 | "matrix_access_token", | |
| 2440 | "syt_Abc-123._~+/=" | |
| 2441 | ) | |
| 2442 | .as_deref(), | |
| 2443 | Some("/secure set matrix_access_token syt_Abc-123._~+/=") | |
| 2444 | ); | |
| 2445 | } | |
| 2446 | ||
| 2447 | #[test] | |
| 2448 | fn secure_token_command_rejects_command_delimiters() { | |
| 2449 | assert_eq!( | |
| 2450 | secure_set_token_command("matrix_access_token", "\"syt_token\""), | |
| 2451 | None | |
| 2452 | ); | |
| 2453 | assert_eq!( | |
| 2454 | secure_set_token_command("matrix_access_token", "syt token"), | |
| 2455 | None | |
| 2456 | ); | |
| 2457 | assert_eq!( | |
| 2458 | secure_set_token_command("matrix_access_token", "syt\n/set x y"), | |
| 2459 | None | |
| 2460 | ); | |
| 2461 | } | |
| 2462 | ||
| 2414 | 2463 | #[test] |
| 2415 | 2464 | fn keeps_absolute_download_path_for_display() { |
| 2416 | 2465 | let path = Path::new("/tmp/matrix-media"); |