Skip to content

Commit fb63e63

Browse files
committed
fix: update tests to use strict() policy for image whitelist tests
- test_validate_image_blocked now uses strict() policy - test_default_policy_blocks_malicious_images renamed and uses strict() - Added test_default_allows_all_images and test_default_policy_allows_all_images - test_full_config_validation no longer tests malicious image (moved to separate test) - test_policy_enforcement uses strict() policy - Added test_default_policy_allows_images for broker
1 parent 0d30bfa commit fb63e63

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

crates/secure-container-runtime/src/broker.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,8 @@ mod tests {
929929

930930
#[tokio::test]
931931
async fn test_policy_enforcement() {
932-
// Test that policy validation works without Docker
933-
let policy = SecurityPolicy::default();
932+
// Test that strict policy blocks non-whitelisted images
933+
let policy = SecurityPolicy::strict();
934934

935935
let config = ContainerConfig {
936936
image: "malicious/image:latest".to_string(),
@@ -941,4 +941,19 @@ mod tests {
941941

942942
assert!(policy.validate(&config).is_err());
943943
}
944+
945+
#[tokio::test]
946+
async fn test_default_policy_allows_images() {
947+
// Default policy allows all images
948+
let policy = SecurityPolicy::default();
949+
950+
let config = ContainerConfig {
951+
image: "any/image:latest".to_string(),
952+
challenge_id: "test".to_string(),
953+
owner_id: "test".to_string(),
954+
..Default::default()
955+
};
956+
957+
assert!(policy.validate(&config).is_ok());
958+
}
944959
}

crates/secure-container-runtime/src/policy.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ impl Default for SecurityPolicy {
7676
impl SecurityPolicy {
7777
/// Create a strict policy for production with image whitelist
7878
pub fn strict() -> Self {
79-
let mut policy = Self::default();
80-
// Only allow platform images in strict mode
81-
policy.allowed_image_prefixes = vec!["ghcr.io/platformnetwork/".to_string()];
82-
policy
79+
Self {
80+
// Only allow platform images in strict mode
81+
allowed_image_prefixes: vec!["ghcr.io/platformnetwork/".to_string()],
82+
..Default::default()
83+
}
8384
}
8485

8586
/// Create a more permissive policy for development
@@ -301,13 +302,25 @@ mod tests {
301302

302303
#[test]
303304
fn test_validate_image_blocked() {
304-
let policy = SecurityPolicy::default();
305+
// Use strict() policy which has whitelist enabled
306+
let policy = SecurityPolicy::strict();
305307
assert!(policy
306308
.validate_image("docker.io/malicious/image:latest")
307309
.is_err());
308310
assert!(policy.validate_image("alpine:latest").is_err());
309311
}
310312

313+
#[test]
314+
fn test_default_allows_all_images() {
315+
// Default policy has no whitelist, allows all images
316+
let policy = SecurityPolicy::default();
317+
assert!(policy.validate_image("docker.io/any/image:latest").is_ok());
318+
assert!(policy.validate_image("alpine:latest").is_ok());
319+
assert!(policy
320+
.validate_image("alexgshaw/code-from-image:20251031")
321+
.is_ok());
322+
}
323+
311324
#[test]
312325
fn test_validate_docker_socket_blocked() {
313326
let policy = SecurityPolicy::default();

crates/secure-container-runtime/tests/integration_tests.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ fn test_default_policy_blocks_docker_socket() {
4242
}
4343

4444
#[test]
45-
fn test_default_policy_blocks_malicious_images() {
46-
let policy = SecurityPolicy::default();
45+
fn test_strict_policy_blocks_non_whitelisted_images() {
46+
// Use strict() which has whitelist enabled
47+
let policy = SecurityPolicy::strict();
4748

48-
// Blocked images
49+
// Blocked images (not in whitelist)
4950
let blocked = vec![
5051
"alpine:latest",
5152
"ubuntu:22.04",
@@ -59,7 +60,7 @@ fn test_default_policy_blocks_malicious_images() {
5960
assert!(result.is_err(), "Image should be blocked: {}", image);
6061
}
6162

62-
// Allowed images
63+
// Allowed images (in whitelist)
6364
let allowed = vec![
6465
"ghcr.io/platformnetwork/term-challenge:latest",
6566
"ghcr.io/platformnetwork/validator:v1.0.0",
@@ -72,6 +73,24 @@ fn test_default_policy_blocks_malicious_images() {
7273
}
7374
}
7475

76+
#[test]
77+
fn test_default_policy_allows_all_images() {
78+
// Default policy has empty whitelist = allow all
79+
let policy = SecurityPolicy::default();
80+
81+
let images = vec![
82+
"alpine:latest",
83+
"ubuntu:22.04",
84+
"alexgshaw/code-from-image:20251031",
85+
"ghcr.io/platformnetwork/term-challenge:latest",
86+
];
87+
88+
for image in images {
89+
let result = policy.validate_image(image);
90+
assert!(result.is_ok(), "Image should be allowed: {}", image);
91+
}
92+
}
93+
7594
#[test]
7695
fn test_policy_enforces_resource_limits() {
7796
let policy = SecurityPolicy::default();
@@ -157,7 +176,7 @@ fn test_policy_container_limits() {
157176
fn test_full_config_validation() {
158177
let policy = SecurityPolicy::default();
159178

160-
// Valid config
179+
// Valid config (default allows all images)
161180
let valid = ContainerConfig {
162181
image: TEST_IMAGE.to_string(),
163182
challenge_id: "test-challenge".to_string(),
@@ -183,8 +202,13 @@ fn test_full_config_validation() {
183202
..Default::default()
184203
};
185204
assert!(policy.validate(&missing_owner).is_err());
205+
}
206+
207+
#[test]
208+
fn test_strict_policy_blocks_malicious_image() {
209+
// Use strict() policy to test image whitelist
210+
let policy = SecurityPolicy::strict();
186211

187-
// Malicious image
188212
let malicious = ContainerConfig {
189213
image: MALICIOUS_IMAGE.to_string(),
190214
challenge_id: "test-challenge".to_string(),

0 commit comments

Comments
 (0)