Skip to content

Commit 59643fc

Browse files
committed
Bound API key file reads
Read at most one byte beyond the expected API key length. This avoids unbounded memory use for oversized or special files while still rejecting contents that are not exactly 32 bytes. This commit was created with assistance from Codex.
1 parent 572931d commit 59643fc

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

ldk-server/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod util;
1414

1515
use std::collections::HashSet;
1616
use std::fs;
17+
use std::io::Read;
1718
use std::path::{Path, PathBuf};
1819
use std::sync::Arc;
1920
use std::time::{Duration, SystemTime, UNIX_EPOCH};
@@ -895,7 +896,9 @@ fn load_or_generate_api_key(storage_dir: &Path) -> std::io::Result<String> {
895896
let api_key_path = storage_dir.join(API_KEY_FILE);
896897

897898
if api_key_path.exists() {
898-
let key_bytes = fs::read(&api_key_path)?;
899+
let file = fs::File::open(&api_key_path)?;
900+
let mut key_bytes = Vec::with_capacity(API_KEY_LEN + 1);
901+
file.take((API_KEY_LEN + 1) as u64).read_to_end(&mut key_bytes)?;
899902
if key_bytes.len() != API_KEY_LEN {
900903
return Err(std::io::Error::new(
901904
std::io::ErrorKind::InvalidData,

0 commit comments

Comments
 (0)