Skip to content

Commit 9d0d0ef

Browse files
committed
Create private log files
Create new log files with mode 0600 on Unix during logger initialization and when a missing file is recreated during a later write. Existing log files are left unchanged. This keeps peer IDs, channel IDs, payment hashes, and payment amounts from being readable by other local users when log files are created under a permissive umask. Non-Unix platforms retain their existing log file creation behavior. This commit was created with assistance from Codex.
1 parent 0abcdd5 commit 9d0d0ef

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

src/logger.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use core::fmt;
1111
use std::fs;
1212
use std::io::Write;
13+
#[cfg(unix)]
14+
use std::os::unix::fs::OpenOptionsExt;
1315
use std::path::Path;
1416
use std::sync::Arc;
1517

@@ -24,6 +26,14 @@ use log::{Level as LogFacadeLevel, Record as LogFacadeRecord};
2426

2527
use crate::io::utils::create_dir_all_private;
2628

29+
fn open_log_file(file_path: &str) -> std::io::Result<fs::File> {
30+
let mut options = fs::OpenOptions::new();
31+
options.create(true).append(true);
32+
#[cfg(unix)]
33+
options.mode(0o600);
34+
options.open(file_path)
35+
}
36+
2737
/// A unit of logging output with metadata to enable filtering `module_path`,
2838
/// `file`, and `line` to inform on log's source.
2939
#[cfg(not(feature = "uniffi"))]
@@ -210,10 +220,7 @@ impl LogWriter for Writer {
210220
context,
211221
);
212222

213-
fs::OpenOptions::new()
214-
.create(true)
215-
.append(true)
216-
.open(file_path)
223+
open_log_file(file_path)
217224
.expect("Failed to open log file")
218225
.write_all(log.as_bytes())
219226
.expect("Failed to write to log file")
@@ -267,10 +274,7 @@ impl Logger {
267274
.map_err(|e| eprintln!("ERROR: Failed to create log parent directory: {}", e))?;
268275

269276
// make sure the file exists.
270-
fs::OpenOptions::new()
271-
.create(true)
272-
.append(true)
273-
.open(&file_path)
277+
open_log_file(&file_path)
274278
.map_err(|e| eprintln!("ERROR: Failed to open log file: {}", e))?;
275279
}
276280

@@ -310,6 +314,23 @@ mod tests {
310314
use std::sync::Mutex;
311315

312316
use super::*;
317+
#[cfg(unix)]
318+
use crate::io::test_utils::random_storage_path;
319+
320+
#[cfg(unix)]
321+
#[test]
322+
fn creates_private_log_file() {
323+
use std::os::unix::fs::PermissionsExt;
324+
325+
let log_dir = random_storage_path();
326+
let log_path = log_dir.join("ldk_node.log");
327+
let _logger =
328+
Logger::new_fs_writer(log_path.to_str().unwrap().to_string(), LogLevel::Info).unwrap();
329+
330+
let mode = log_path.metadata().unwrap().permissions().mode();
331+
assert_eq!(mode & 0o077, 0);
332+
fs::remove_dir_all(log_dir).unwrap();
333+
}
313334

314335
/// A minimal log facade logger that captures log output for testing.
315336
struct TestLogger {

0 commit comments

Comments
 (0)