| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use std::path::Path; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
fn walk(dir: &Path, f: &mut impl FnMut(&Path, &str)) { |
| 11 |
let Ok(entries) = std::fs::read_dir(dir) else { |
| 12 |
return; |
| 13 |
}; |
| 14 |
for entry in entries.flatten() { |
| 15 |
let path = entry.path(); |
| 16 |
if path.is_dir() { |
| 17 |
walk(&path, f); |
| 18 |
} else if path.extension().is_some_and(|e| e == "rs") |
| 19 |
&& let Ok(contents) = std::fs::read_to_string(&path) |
| 20 |
{ |
| 21 |
f(&path, &contents); |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[cfg(test)] |
| 37 |
mod delete_seal_guard { |
| 38 |
use super::walk; |
| 39 |
use std::path::Path; |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn routes_never_delete_s3_directly() { |
| 43 |
let routes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/routes"); |
| 44 |
let mut offenders = Vec::new(); |
| 45 |
walk(&routes_dir, &mut |path, contents| { |
| 46 |
for (i, line) in contents.lines().enumerate() { |
| 47 |
|
| 48 |
if line.trim_start().starts_with("//") { |
| 49 |
continue; |
| 50 |
} |
| 51 |
if line.contains(".delete_object(") |
| 52 |
|| line.contains(".delete_objects(") |
| 53 |
|| line.contains(".delete_prefix(") |
| 54 |
|| line.contains("S3DeleteAuthority") |
| 55 |
{ |
| 56 |
offenders.push(format!("{}:{}: {}", path.display(), i + 1, line.trim())); |
| 57 |
} |
| 58 |
} |
| 59 |
}); |
| 60 |
assert!( |
| 61 |
offenders.is_empty(), |
| 62 |
"CHRONIC B' seal violated, route code must enqueue via \ |
| 63 |
routes::storage::enqueue_s3_orphan, never delete S3 directly or mint an \ |
| 64 |
S3DeleteAuthority. Offending lines:\n{}", |
| 65 |
offenders.join("\n") |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#[cfg(test)] |
| 88 |
mod served_key_seal_guard { |
| 89 |
use super::walk; |
| 90 |
use std::path::Path; |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn routes_never_mint_served_keys() { |
| 94 |
let routes_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/routes"); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
const FORBIDDEN: &[&str] = &[ |
| 102 |
"S3Client::generate_key(", |
| 103 |
"S3Client::generate_version_key(", |
| 104 |
"S3Client::generate_insertion_key(", |
| 105 |
"S3Client::generate_media_key(", |
| 106 |
"S3Client::generate_project_image_key(", |
| 107 |
"S3Client::generate_ota_artifact_key(", |
| 108 |
"S3Client::generate_item_gallery_key(", |
| 109 |
"S3Client::generate_project_gallery_key(", |
| 110 |
"S3Client::content_key(", |
| 111 |
]; |
| 112 |
let mut offenders = Vec::new(); |
| 113 |
walk(&routes_dir, &mut |path, contents| { |
| 114 |
for (i, line) in contents.lines().enumerate() { |
| 115 |
if line.trim_start().starts_with("//") { |
| 116 |
continue; |
| 117 |
} |
| 118 |
for needle in FORBIDDEN { |
| 119 |
if line.contains(needle) { |
| 120 |
offenders.push(format!("{}:{}: {}", path.display(), i + 1, line.trim())); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
}); |
| 125 |
assert!( |
| 126 |
offenders.is_empty(), |
| 127 |
"C1 seal violated, route handlers must presign only `generate_staging_key`; \ |
| 128 |
the served/content key is minted solely by the scan worker's promote step. \ |
| 129 |
Offending lines:\n{}", |
| 130 |
offenders.join("\n") |
| 131 |
); |
| 132 |
} |
| 133 |
} |
| 134 |
|