Skip to content

Commit ba7672c

Browse files
committed
security: remediate UnboundedAllocation findings
1 parent 2dc10ba commit ba7672c

19 files changed

Lines changed: 29 additions & 29 deletions

File tree

crates/januskey-cli/src/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl KeyManager {
531531

532532
fn load_store_raw(&self) -> Result<KeyStoreData> {
533533
let path = self.store_path.join("keystore.jks");
534-
let content = fs::read_to_string(&path)?;
534+
let content = ({ use std::io::Read; std::fs::File::open(&path).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })?;
535535
let store: KeyStoreData = serde_json::from_str(&content)?;
536536
Ok(store)
537537
}

crates/januskey-cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Config {
7878
pub fn load(dir: &std::path::Path) -> Self {
7979
let config_path = dir.join(".januskey").join("config.json");
8080
if config_path.exists() {
81-
if let Ok(content) = std::fs::read_to_string(&config_path) {
81+
if let Ok(content) = ({ use std::io::Read; std::fs::File::open(&config_path).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }) {
8282
if let Ok(config) = serde_json::from_str(&content) {
8383
return config;
8484
}

crates/januskey-cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fn cmd_modify(
392392
// Preview changes
393393
let mut changes = Vec::new();
394394
for file in &files {
395-
let content = fs::read_to_string(file)?;
395+
let content = ({ use std::io::Read; std::fs::File::open(file).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })?;
396396
let new_content = if global {
397397
content.replace(&search, &replace)
398398
} else {

crates/januskey-cli/src/obliteration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl ObliterationManager {
146146
/// Create or open an obliteration manager
147147
pub fn new(log_path: PathBuf) -> Result<Self> {
148148
let log = if log_path.exists() {
149-
let content = fs::read_to_string(&log_path)?;
149+
let content = ({ use std::io::Read; std::fs::File::open(&log_path).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })?;
150150
serde_json::from_str(&content)
151151
.map_err(|e| JanusError::MetadataCorrupted(e.to_string()))?
152152
} else {

crates/januskey-cli/src/operations.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ mod tests {
513513
executor.undo(&delete_meta.id).unwrap();
514514

515515
assert!(test_file.exists());
516-
assert_eq!(fs::read_to_string(&test_file).unwrap(), "hello world");
516+
assert_eq!(({ use std::io::Read; std::fs::File::open(&test_file).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }).unwrap(), "hello world");
517517
}
518518

519519
#[test]
@@ -533,13 +533,13 @@ mod tests {
533533
})
534534
.unwrap();
535535

536-
assert_eq!(fs::read_to_string(&test_file).unwrap(), "modified content");
536+
assert_eq!(({ use std::io::Read; std::fs::File::open(&test_file).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }).unwrap(), "modified content");
537537

538538
// Undo the modify
539539
let mut executor = OperationExecutor::new(&content_store, &mut metadata_store);
540540
executor.undo(&modify_meta.id).unwrap();
541541

542-
assert_eq!(fs::read_to_string(&test_file).unwrap(), "original content");
542+
assert_eq!(({ use std::io::Read; std::fs::File::open(&test_file).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }).unwrap(), "original content");
543543
}
544544

545545
#[test]

crates/januskey-cli/tests/aspect_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn obliterated_key_record_marked_revoked() {
134134

135135
// Verify key record reflects revocation
136136
let key_content =
137-
fs::read_to_string(base.join(".jk/keys").join(format!("{}.json", key_id)))
137+
({ use std::io::Read; std::fs::File::open(base.join(".jk/keys").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }).join(format!("{}.json", key_id)))
138138
.expect("Read key record");
139139
assert!(
140140
key_content.contains("revoked"),

crates/januskey-cli/tests/concurrency_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn transaction_isolation_uncommitted_invisible() {
164164

165165
// Reader may see the file (filesystem is not transactional),
166166
// but we verify the transaction itself is still "active" not "committed"
167-
let tx_read = fs::read_to_string(base.join(".jk/transactions/001.json"))
167+
let tx_read = ({ use std::io::Read; std::fs::File::open(base.join(".jk/transactions/001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }))
168168
.expect("Read transaction");
169169
assert!(
170170
tx_read.contains("\"active\""),
@@ -404,7 +404,7 @@ fn concurrent_commit_rollback_no_corruption() {
404404
// Verify each transaction is in a valid terminal state
405405
for entry in fs::read_dir(base.join(".jk/transactions")).expect("Read dir") {
406406
let entry = entry.expect("Dir entry");
407-
let content = fs::read_to_string(entry.path()).expect("Read tx file");
407+
let content = ({ use std::io::Read; std::fs::File::open(entry.path().and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })).expect("Read tx file");
408408
let is_valid = content.contains("\"committed\"") || content.contains("\"rolled_back\"");
409409
assert!(
410410
is_valid,

crates/januskey-cli/tests/e2e_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ fn full_key_lifecycle_single_key() {
8181
assert_eq!(retrieved, key_material, "Retrieved content must match original");
8282

8383
// Step 5: Verify attestation references the key
84-
let attest_read = fs::read_to_string(base.join(".jk/attestation/0001.json"))
84+
let attest_read = ({ use std::io::Read; std::fs::File::open(base.join(".jk/attestation/0001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }))
8585
.expect("Read attestation");
8686
assert!(attest_read.contains(key_id), "Attestation must contain key ID");
8787
assert!(attest_read.contains(&key_hash), "Attestation must contain content hash");
8888

8989
// Verify key record exists
90-
let key_read = fs::read_to_string(base.join(".jk/keys/001.json"))
90+
let key_read = ({ use std::io::Read; std::fs::File::open(base.join(".jk/keys/001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }))
9191
.expect("Read key record");
9292
assert!(key_read.contains(key_id), "Key record must contain ID");
9393
}
@@ -167,7 +167,7 @@ fn full_key_lifecycle_multi_key_transaction() {
167167

168168
// Verify transaction is committed
169169
let tx_read =
170-
fs::read_to_string(base.join(".jk/transactions/001.json")).expect("Read tx");
170+
({ use std::io::Read; std::fs::File::open(base.join(".jk/transactions/001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })).expect("Read tx");
171171
assert!(tx_read.contains("committed"), "Transaction must be committed");
172172
}
173173

@@ -359,7 +359,7 @@ fn corrupted_attestation_entry_detected() {
359359
.expect("Write malformed entry");
360360

361361
// Attempt to read and parse
362-
let read_result = fs::read_to_string(base.join(".jk/attestation/0001.json"))
362+
let read_result = ({ use std::io::Read; std::fs::File::open(base.join(".jk/attestation/0001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }))
363363
.expect("Read file");
364364
let parse_result = serde_json::from_str::<serde_json::Value>(&read_result);
365365

crates/januskey-cli/tests/p2p_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn key_operation_creates_attestation_entry() {
101101
std::fs::write(attest_path.join("0001.json"), &entry).unwrap();
102102

103103
// Verify attestation references the key
104-
let read_back = std::fs::read_to_string(attest_path.join("0001.json")).unwrap();
104+
let read_back = ({ use std::io::Read; std::fs::File::open(attest_path.join("0001.json").and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })).unwrap();
105105
assert!(
106106
read_back.contains(key_id),
107107
"Attestation must reference key ID"
@@ -132,7 +132,7 @@ fn attestation_chain_integrity() {
132132

133133
// Verify chain: each entry's content hashes to the next's prev_hash
134134
let entries: Vec<String> = (0..3)
135-
.map(|i| std::fs::read_to_string(attest_path.join(format!("{:04}.json", i))).unwrap())
135+
.map(|i| ({ use std::io::Read; std::fs::File::open(attest_path.join(format!("{:04}.json", i).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) }))).unwrap())
136136
.collect();
137137

138138
for i in 1..entries.len() {

crates/reversible-core/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl MetadataStore {
256256
/// Create or open a metadata store
257257
pub fn new(path: PathBuf) -> Result<Self> {
258258
let log = if path.exists() {
259-
let content = fs::read_to_string(&path)?;
259+
let content = ({ use std::io::Read; std::fs::File::open(&path).and_then(|mut f| { let mut buf = String::new(); f.take(10 * 1024 * 1024).read_to_string(&mut buf)?; Ok(buf) }) })?;
260260
serde_json::from_str(&content)
261261
.map_err(|e| ReversibleError::MetadataCorrupted(e.to_string()))?
262262
} else {

0 commit comments

Comments
 (0)