| @@ -0,0 +1,605 @@ |
| 1 | use std::{ |
| 2 | cell::RefCell, |
| 3 | collections::BTreeMap, |
| 4 | rc::Rc, |
| 5 | time::{Duration, Instant}, |
| 6 | }; |
| 7 | |
| 8 | use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; |
| 9 | use matrix_sdk::ruma::OwnedRoomId; |
| 10 | use sha2::{Digest, Sha256}; |
| 11 | use weechat::{ |
| 12 | buffer::Buffer, |
| 13 | hooks::{CommandRun, CommandRunCallback}, |
| 14 | Prefix, ReturnCode, Weechat, |
| 15 | }; |
| 16 | |
| 17 | use crate::{ |
| 18 | room::{thread_root_from_buffer, RoomHandle}, |
| 19 | Servers, |
| 20 | }; |
| 21 | |
| 22 | const MAX_UPLOAD_BYTES: usize = 10 * 1024 * 1024; |
| 23 | const MAX_ENCODED_CHUNK_BYTES: usize = 32 * 1024; |
| 24 | const MAX_ACTIVE_UPLOADS: usize = 4; |
| 25 | const UPLOAD_IDLE_TIMEOUT: Duration = Duration::from_secs(60); |
| 26 | const PNG_SIGNATURE: &[u8] = b"\x89PNG\r\n\x1a\n"; |
| 27 | |
| 28 | struct MediaUpload { |
| 29 | filename: String, |
| 30 | mime: mime::Mime, |
| 31 | declared_size: usize, |
| 32 | expected_sha256: [u8; 32], |
| 33 | next_chunk: u32, |
| 34 | data: Vec<u8>, |
| 35 | } |
| 36 | |
| 37 | impl MediaUpload { |
| 38 | fn begin( |
| 39 | encoded_filename: &str, |
| 40 | mime: &str, |
| 41 | declared_size: &str, |
| 42 | sha256: &str, |
| 43 | ) -> Result<Self, &'static str> { |
| 44 | let filename = decode_filename(encoded_filename)?; |
| 45 | if !valid_filename(&filename) { |
| 46 | return Err("filename must be a single non-empty file name"); |
| 47 | } |
| 48 | |
| 49 | if mime.len() > 127 || mime.chars().any(char::is_control) { |
| 50 | return Err("MIME type is invalid or too long"); |
| 51 | } |
| 52 | let mime: mime::Mime = |
| 53 | mime.parse().map_err(|_| "MIME type is invalid")?; |
| 54 | |
| 55 | let declared_size = declared_size |
| 56 | .parse::<usize>() |
| 57 | .map_err(|_| "declared size must be a decimal byte count")?; |
| 58 | |
| 59 | if declared_size == 0 || declared_size > MAX_UPLOAD_BYTES { |
| 60 | return Err("declared size is outside the allowed range"); |
| 61 | } |
| 62 | |
| 63 | let expected_sha256 = parse_sha256(sha256)?; |
| 64 | |
| 65 | Ok(Self { |
| 66 | filename, |
| 67 | mime, |
| 68 | declared_size, |
| 69 | expected_sha256, |
| 70 | next_chunk: 0, |
| 71 | data: Vec::with_capacity(declared_size), |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | fn append_chunk( |
| 76 | &mut self, |
| 77 | chunk_index: &str, |
| 78 | encoded: &str, |
| 79 | ) -> Result<(), &'static str> { |
| 80 | if encoded.len() > MAX_ENCODED_CHUNK_BYTES { |
| 81 | return Err("encoded chunk exceeds the allowed size"); |
| 82 | } |
| 83 | |
| 84 | let chunk_index = chunk_index |
| 85 | .parse::<u32>() |
| 86 | .map_err(|_| "chunk index must be an unsigned integer")?; |
| 87 | |
| 88 | if chunk_index != self.next_chunk { |
| 89 | return Err("chunk index is out of order"); |
| 90 | } |
| 91 | |
| 92 | let chunk = URL_SAFE_NO_PAD |
| 93 | .decode(encoded) |
| 94 | .map_err(|_| "chunk is not valid base64")?; |
| 95 | let new_len = self |
| 96 | .data |
| 97 | .len() |
| 98 | .checked_add(chunk.len()) |
| 99 | .ok_or("decoded bytes exceed the declared size")?; |
| 100 | |
| 101 | if new_len > self.declared_size { |
| 102 | return Err("decoded bytes exceed the declared size"); |
| 103 | } |
| 104 | |
| 105 | self.data.extend_from_slice(&chunk); |
| 106 | self.next_chunk = self |
| 107 | .next_chunk |
| 108 | .checked_add(1) |
| 109 | .ok_or("too many upload chunks")?; |
| 110 | Ok(()) |
| 111 | } |
| 112 | |
| 113 | fn finish(self) -> Result<(String, mime::Mime, Vec<u8>), &'static str> { |
| 114 | if self.data.len() != self.declared_size { |
| 115 | return Err("decoded byte count does not match the declared size"); |
| 116 | } |
| 117 | |
| 118 | if self.mime == mime::IMAGE_PNG && !self.data.starts_with(PNG_SIGNATURE) |
| 119 | { |
| 120 | return Err("image/png payload does not have a PNG signature"); |
| 121 | } |
| 122 | |
| 123 | let actual_sha256: [u8; 32] = Sha256::digest(&self.data).into(); |
| 124 | if actual_sha256 != self.expected_sha256 { |
| 125 | return Err("payload hash does not match the declared SHA-256"); |
| 126 | } |
| 127 | |
| 128 | Ok((self.filename, self.mime, self.data)) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | struct PendingMediaUpload { |
| 133 | room: RoomHandle, |
| 134 | room_id: OwnedRoomId, |
| 135 | thread_root: Option<matrix_sdk::ruma::OwnedEventId>, |
| 136 | upload: MediaUpload, |
| 137 | last_activity: Instant, |
| 138 | } |
| 139 | |
| 140 | #[derive(Default)] |
| 141 | struct UploadState { |
| 142 | pending: BTreeMap<String, PendingMediaUpload>, |
| 143 | } |
| 144 | |
| 145 | impl UploadState { |
| 146 | fn prune_expired(&mut self, now: Instant) { |
| 147 | self.pending.retain(|_, pending| { |
| 148 | !upload_is_expired(pending.last_activity, now) |
| 149 | }); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | pub struct MatrixUploadCommand { |
| 154 | servers: Servers, |
| 155 | state: Rc<RefCell<UploadState>>, |
| 156 | } |
| 157 | |
| 158 | impl MatrixUploadCommand { |
| 159 | pub fn create(servers: &Servers) -> Result<CommandRun, ()> { |
| 160 | CommandRun::new( |
| 161 | "/matrix-upload", |
| 162 | MatrixUploadCommand { |
| 163 | servers: servers.clone(), |
| 164 | state: Rc::new(RefCell::new(UploadState::default())), |
| 165 | }, |
| 166 | ) |
| 167 | } |
| 168 | |
| 169 | fn report_error(buffer: &Buffer, error: &str) { |
| 170 | buffer.print(&format!( |
| 171 | "{}matrix-upload: {}", |
| 172 | Weechat::prefix(Prefix::Error), |
| 173 | error |
| 174 | )); |
| 175 | } |
| 176 | |
| 177 | fn current_context( |
| 178 | &self, |
| 179 | buffer: &Buffer, |
| 180 | ) -> Result< |
| 181 | (RoomHandle, Option<matrix_sdk::ruma::OwnedEventId>), |
| 182 | &'static str, |
| 183 | > { |
| 184 | let room = self |
| 185 | .servers |
| 186 | .find_room(buffer) |
| 187 | .ok_or("command must be run in a Matrix room or thread buffer")?; |
| 188 | Ok((room, thread_root_from_buffer(buffer))) |
| 189 | } |
| 190 | |
| 191 | fn matches_pending( |
| 192 | pending: &PendingMediaUpload, |
| 193 | room: &RoomHandle, |
| 194 | thread_root: &Option<matrix_sdk::ruma::OwnedEventId>, |
| 195 | ) -> bool { |
| 196 | pending.room_id == room.room_id() |
| 197 | && pending.thread_root.as_ref() == thread_root.as_ref() |
| 198 | } |
| 199 | |
| 200 | fn begin(&self, buffer: &Buffer, args: &[&str]) { |
| 201 | if args.len() != 6 { |
| 202 | Self::report_error(buffer, "usage: /matrix-upload begin <id> <filename-base64url> <mime> <bytes> <sha256>"); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if !valid_transfer_id(args[1]) { |
| 207 | Self::report_error( |
| 208 | buffer, |
| 209 | "transfer id must be 1-64 URL-safe characters", |
| 210 | ); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | let Ok((room, thread_root)) = self.current_context(buffer) else { |
| 215 | Self::report_error( |
| 216 | buffer, |
| 217 | "command must be run in a Matrix room or thread buffer", |
| 218 | ); |
| 219 | return; |
| 220 | }; |
| 221 | |
| 222 | let upload = |
| 223 | match MediaUpload::begin(args[2], args[3], args[4], args[5]) { |
| 224 | Ok(upload) => upload, |
| 225 | Err(error) => { |
| 226 | Self::report_error(buffer, error); |
| 227 | return; |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | let mut state = self.state.borrow_mut(); |
| 232 | state.prune_expired(Instant::now()); |
| 233 | if state.pending.contains_key(args[1]) { |
| 234 | Self::report_error(buffer, "transfer id is already active"); |
| 235 | return; |
| 236 | } |
| 237 | if state.pending.len() >= MAX_ACTIVE_UPLOADS { |
| 238 | Self::report_error(buffer, "too many active media uploads"); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | state.pending.insert( |
| 243 | args[1].to_owned(), |
| 244 | PendingMediaUpload { |
| 245 | room_id: room.room_id().to_owned(), |
| 246 | room, |
| 247 | thread_root, |
| 248 | upload, |
| 249 | last_activity: Instant::now(), |
| 250 | }, |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | fn chunk(&self, buffer: &Buffer, args: &[&str]) { |
| 255 | if args.len() != 4 { |
| 256 | Self::report_error( |
| 257 | buffer, |
| 258 | "usage: /matrix-upload chunk <id> <index> <base64url>", |
| 259 | ); |
| 260 | return; |
| 261 | } |
| 262 | if !valid_transfer_id(args[1]) { |
| 263 | Self::report_error( |
| 264 | buffer, |
| 265 | "transfer id must be 1-64 URL-safe characters", |
| 266 | ); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | let Ok((room, thread_root)) = self.current_context(buffer) else { |
| 271 | Self::report_error(buffer, "command must be run in the Matrix buffer that began the upload"); |
| 272 | return; |
| 273 | }; |
| 274 | |
| 275 | let mut state = self.state.borrow_mut(); |
| 276 | state.prune_expired(Instant::now()); |
| 277 | let error = { |
| 278 | let Some(pending) = state.pending.get_mut(args[1]) else { |
| 279 | Self::report_error(buffer, "no active upload"); |
| 280 | return; |
| 281 | }; |
| 282 | |
| 283 | if !Self::matches_pending(pending, &room, &thread_root) { |
| 284 | Self::report_error( |
| 285 | buffer, |
| 286 | "upload buffer or thread does not match transfer id", |
| 287 | ); |
| 288 | return; |
| 289 | } else { |
| 290 | let result = pending.upload.append_chunk(args[2], args[3]); |
| 291 | if result.is_ok() { |
| 292 | pending.last_activity = Instant::now(); |
| 293 | } |
| 294 | result.err() |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | if let Some(error) = error { |
| 299 | state.pending.remove(args[1]); |
| 300 | Self::report_error(buffer, error); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | fn commit(&self, buffer: &Buffer, args: &[&str]) { |
| 305 | if args.len() != 2 { |
| 306 | Self::report_error(buffer, "usage: /matrix-upload commit <id>"); |
| 307 | return; |
| 308 | } |
| 309 | if !valid_transfer_id(args[1]) { |
| 310 | Self::report_error( |
| 311 | buffer, |
| 312 | "transfer id must be 1-64 URL-safe characters", |
| 313 | ); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | let Ok((room, thread_root)) = self.current_context(buffer) else { |
| 318 | Self::report_error(buffer, "command must be run in the Matrix buffer that began the upload"); |
| 319 | return; |
| 320 | }; |
| 321 | |
| 322 | let pending = { |
| 323 | let mut state = self.state.borrow_mut(); |
| 324 | state.prune_expired(Instant::now()); |
| 325 | let Some(pending) = state.pending.get(args[1]) else { |
| 326 | Self::report_error(buffer, "no active upload"); |
| 327 | return; |
| 328 | }; |
| 329 | if !Self::matches_pending(pending, &room, &thread_root) { |
| 330 | Self::report_error( |
| 331 | buffer, |
| 332 | "upload buffer or thread does not match transfer id", |
| 333 | ); |
| 334 | return; |
| 335 | } |
| 336 | state.pending.remove(args[1]) |
| 337 | }; |
| 338 | let Some(pending) = pending else { |
| 339 | Self::report_error(buffer, "no active upload"); |
| 340 | return; |
| 341 | }; |
| 342 | |
| 343 | let (filename, mime, data) = match pending.upload.finish() { |
| 344 | Ok(upload) => upload, |
| 345 | Err(error) => { |
| 346 | Self::report_error(buffer, error); |
| 347 | return; |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | let room = pending.room; |
| 352 | let thread_root = pending.thread_root; |
| 353 | Weechat::spawn(async move { |
| 354 | room.send_attachment_bytes(filename, mime, data, thread_root) |
| 355 | .await |
| 356 | }) |
| 357 | .detach(); |
| 358 | } |
| 359 | |
| 360 | fn cancel(&self, buffer: &Buffer, args: &[&str]) { |
| 361 | if args.len() != 2 { |
| 362 | Self::report_error(buffer, "usage: /matrix-upload cancel <id>"); |
| 363 | return; |
| 364 | } |
| 365 | if !valid_transfer_id(args[1]) { |
| 366 | Self::report_error( |
| 367 | buffer, |
| 368 | "transfer id must be 1-64 URL-safe characters", |
| 369 | ); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | let Ok((room, thread_root)) = self.current_context(buffer) else { |
| 374 | Self::report_error(buffer, "command must be run in the Matrix buffer that began the upload"); |
| 375 | return; |
| 376 | }; |
| 377 | |
| 378 | let mut state = self.state.borrow_mut(); |
| 379 | state.prune_expired(Instant::now()); |
| 380 | let Some(pending) = state.pending.get(args[1]) else { |
| 381 | Self::report_error(buffer, "no active upload"); |
| 382 | return; |
| 383 | }; |
| 384 | if !Self::matches_pending(pending, &room, &thread_root) { |
| 385 | Self::report_error( |
| 386 | buffer, |
| 387 | "upload buffer or thread does not match transfer id", |
| 388 | ); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | state.pending.remove(args[1]); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | impl CommandRunCallback for MatrixUploadCommand { |
| 397 | fn callback( |
| 398 | &mut self, |
| 399 | _: &Weechat, |
| 400 | buffer: &Buffer, |
| 401 | cmd: std::borrow::Cow<str>, |
| 402 | ) -> ReturnCode { |
| 403 | let Some(arguments) = cmd.strip_prefix("/matrix-upload") else { |
| 404 | return ReturnCode::Ok; |
| 405 | }; |
| 406 | let args = arguments.split_whitespace().collect::<Vec<_>>(); |
| 407 | |
| 408 | match args.first().copied() { |
| 409 | Some("begin") => self.begin(buffer, &args), |
| 410 | Some("chunk") => self.chunk(buffer, &args), |
| 411 | Some("commit") => self.commit(buffer, &args), |
| 412 | Some("cancel") => self.cancel(buffer, &args), |
| 413 | _ => Self::report_error( |
| 414 | buffer, |
| 415 | "usage: /matrix-upload <begin|chunk|commit|cancel>", |
| 416 | ), |
| 417 | } |
| 418 | |
| 419 | ReturnCode::Ok |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | fn valid_filename(filename: &str) -> bool { |
| 424 | !filename.is_empty() |
| 425 | && filename.len() <= 255 |
| 426 | && !filename.contains(['/', '\\', '\0']) |
| 427 | && !filename.chars().any(char::is_control) |
| 428 | } |
| 429 | |
| 430 | fn decode_filename(encoded: &str) -> Result<String, &'static str> { |
| 431 | let bytes = URL_SAFE_NO_PAD |
| 432 | .decode(encoded) |
| 433 | .map_err(|_| "filename must be URL-safe base64 without padding")?; |
| 434 | String::from_utf8(bytes).map_err(|_| "filename must be valid UTF-8") |
| 435 | } |
| 436 | |
| 437 | fn valid_transfer_id(id: &str) -> bool { |
| 438 | !id.is_empty() |
| 439 | && id.len() <= 64 |
| 440 | && id.bytes().all(|byte| { |
| 441 | byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-' |
| 442 | }) |
| 443 | } |
| 444 | |
| 445 | fn upload_is_expired(last_activity: Instant, now: Instant) -> bool { |
| 446 | now.saturating_duration_since(last_activity) >= UPLOAD_IDLE_TIMEOUT |
| 447 | } |
| 448 | |
| 449 | fn parse_sha256(value: &str) -> Result<[u8; 32], &'static str> { |
| 450 | if value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit()) |
| 451 | { |
| 452 | return Err("SHA-256 must be 64 hexadecimal characters"); |
| 453 | } |
| 454 | |
| 455 | let mut hash = [0_u8; 32]; |
| 456 | for (index, byte) in hash.iter_mut().enumerate() { |
| 457 | *byte = u8::from_str_radix(&value[index * 2..index * 2 + 2], 16) |
| 458 | .map_err(|_| "SHA-256 must be hexadecimal")?; |
| 459 | } |
| 460 | Ok(hash) |
| 461 | } |
| 462 | |
| 463 | #[cfg(test)] |
| 464 | mod tests { |
| 465 | use super::*; |
| 466 | |
| 467 | fn png(bytes: &[u8]) -> Vec<u8> { |
| 468 | [PNG_SIGNATURE, bytes].concat() |
| 469 | } |
| 470 | |
| 471 | fn sha256(bytes: &[u8]) -> String { |
| 472 | format!("{:x}", Sha256::digest(bytes)) |
| 473 | } |
| 474 | |
| 475 | fn upload_for(bytes: &[u8], filename: &str, mime: &str) -> MediaUpload { |
| 476 | MediaUpload::begin( |
| 477 | &URL_SAFE_NO_PAD.encode(filename), |
| 478 | mime, |
| 479 | &bytes.len().to_string(), |
| 480 | &sha256(bytes), |
| 481 | ) |
| 482 | .unwrap() |
| 483 | } |
| 484 | |
| 485 | #[test] |
| 486 | fn rejects_out_of_bounds_begin_metadata() { |
| 487 | assert!(MediaUpload::begin( |
| 488 | &URL_SAFE_NO_PAD.encode("image.png"), |
| 489 | "image/png", |
| 490 | &(MAX_UPLOAD_BYTES + 1).to_string(), |
| 491 | &"0".repeat(64), |
| 492 | ) |
| 493 | .is_err()); |
| 494 | assert!(MediaUpload::begin( |
| 495 | &URL_SAFE_NO_PAD.encode("../image.png"), |
| 496 | "image/png", |
| 497 | "1", |
| 498 | &"0".repeat(64), |
| 499 | ) |
| 500 | .is_err()); |
| 501 | assert!(MediaUpload::begin( |
| 502 | &URL_SAFE_NO_PAD.encode("payload.bin"), |
| 503 | "not a mime", |
| 504 | "1", |
| 505 | &"0".repeat(64), |
| 506 | ) |
| 507 | .is_err()); |
| 508 | } |
| 509 | |
| 510 | #[test] |
| 511 | fn rejects_malformed_or_out_of_order_chunks() { |
| 512 | let bytes = png(b"payload"); |
| 513 | let mut upload = upload_for(&bytes, "image.png", "image/png"); |
| 514 | assert!(upload.append_chunk("1", "AA==").is_err()); |
| 515 | assert!(upload.append_chunk("0", "not-base64").is_err()); |
| 516 | assert!(upload |
| 517 | .append_chunk("0", &"A".repeat(MAX_ENCODED_CHUNK_BYTES + 1)) |
| 518 | .is_err()); |
| 519 | } |
| 520 | |
| 521 | #[test] |
| 522 | fn preserves_exact_bytes_across_chunks() { |
| 523 | let bytes = png(b"split pixels"); |
| 524 | let mut upload = upload_for(&bytes, "image.png", "image/png"); |
| 525 | let split = 10; |
| 526 | upload |
| 527 | .append_chunk("0", &URL_SAFE_NO_PAD.encode(&bytes[..split])) |
| 528 | .unwrap(); |
| 529 | upload |
| 530 | .append_chunk("1", &URL_SAFE_NO_PAD.encode(&bytes[split..])) |
| 531 | .unwrap(); |
| 532 | let (_, content_type, actual) = upload.finish().unwrap(); |
| 533 | assert_eq!(content_type, mime::IMAGE_PNG); |
| 534 | assert_eq!(actual, bytes); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn rejects_hash_mismatch_and_wrong_png_signature() { |
| 539 | let bytes = png(b"payload"); |
| 540 | let mut upload = MediaUpload::begin( |
| 541 | &URL_SAFE_NO_PAD.encode("image.png"), |
| 542 | "image/png", |
| 543 | &bytes.len().to_string(), |
| 544 | &"0".repeat(64), |
| 545 | ) |
| 546 | .unwrap(); |
| 547 | upload |
| 548 | .append_chunk("0", &URL_SAFE_NO_PAD.encode(&bytes)) |
| 549 | .unwrap(); |
| 550 | assert!(upload.finish().is_err()); |
| 551 | |
| 552 | let bytes = b"not a png"; |
| 553 | let mut upload = upload_for(bytes, "image.png", "image/png"); |
| 554 | upload |
| 555 | .append_chunk("0", &URL_SAFE_NO_PAD.encode(bytes)) |
| 556 | .unwrap(); |
| 557 | assert!(upload.finish().is_err()); |
| 558 | } |
| 559 | |
| 560 | #[test] |
| 561 | fn accepts_pdf_and_octet_stream_without_png_validation() { |
| 562 | for (filename, mime, bytes) in [ |
| 563 | ("report.pdf", "application/pdf", b"%PDF-1.7\n".as_slice()), |
| 564 | ( |
| 565 | "archive.bin", |
| 566 | "application/octet-stream", |
| 567 | b"\x00\x01\x02".as_slice(), |
| 568 | ), |
| 569 | ] { |
| 570 | let mut upload = upload_for(bytes, filename, mime); |
| 571 | upload |
| 572 | .append_chunk("0", &URL_SAFE_NO_PAD.encode(bytes)) |
| 573 | .unwrap(); |
| 574 | let (actual_filename, actual_mime, actual_bytes) = |
| 575 | upload.finish().unwrap(); |
| 576 | assert_eq!(actual_filename, filename); |
| 577 | assert_eq!(actual_mime.as_ref(), mime); |
| 578 | assert_eq!(actual_bytes, bytes); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | #[test] |
| 583 | fn accepts_only_url_safe_ids_and_filenames() { |
| 584 | assert!(valid_transfer_id("upload-01_A")); |
| 585 | assert!(!valid_transfer_id("")); |
| 586 | assert!(!valid_transfer_id("contains space")); |
| 587 | assert!(!valid_transfer_id(&"a".repeat(65))); |
| 588 | assert_eq!( |
| 589 | decode_filename(&URL_SAFE_NO_PAD.encode("attachment image.png")) |
| 590 | .unwrap(), |
| 591 | "attachment image.png" |
| 592 | ); |
| 593 | assert!(decode_filename("a==").is_err()); |
| 594 | } |
| 595 | |
| 596 | #[test] |
| 597 | fn expires_abandoned_transfers_after_one_minute() { |
| 598 | let started = Instant::now(); |
| 599 | assert!(!upload_is_expired( |
| 600 | started, |
| 601 | started + UPLOAD_IDLE_TIMEOUT - Duration::from_millis(1) |
| 602 | )); |
| 603 | assert!(upload_is_expired(started, started + UPLOAD_IDLE_TIMEOUT)); |
| 604 | } |
| 605 | } |