| 1 |
|
| 2 |
|
| 3 |
use super::client::S3Client; |
| 4 |
use super::file_type::FileType; |
| 5 |
use crate::constants; |
| 6 |
|
| 7 |
use crate::db::{ItemId, ProjectId, SyncAppId, UserId, VersionId}; |
| 8 |
use crate::error::{AppError, Result}; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#[derive(Debug, Clone, PartialEq, Eq, Hash, sqlx::Type)] |
| 27 |
#[sqlx(transparent)] |
| 28 |
pub struct S3Key(String); |
| 29 |
|
| 30 |
impl S3Key { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub fn from_stored(key: impl AsRef<str>) -> Self { |
| 35 |
S3Key(key.as_ref().to_string()) |
| 36 |
} |
| 37 |
|
| 38 |
pub fn as_str(&self) -> &str { |
| 39 |
&self.0 |
| 40 |
} |
| 41 |
|
| 42 |
pub fn into_string(self) -> String { |
| 43 |
self.0 |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl std::ops::Deref for S3Key { |
| 48 |
type Target = str; |
| 49 |
fn deref(&self) -> &str { |
| 50 |
&self.0 |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl AsRef<str> for S3Key { |
| 55 |
fn as_ref(&self) -> &str { |
| 56 |
&self.0 |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl std::fmt::Display for S3Key { |
| 61 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 62 |
f.write_str(&self.0) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl PartialEq<&str> for S3Key { |
| 67 |
fn eq(&self, other: &&str) -> bool { |
| 68 |
self.0 == *other |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
const ARTIFACT_EXTENSIONS: &[&str] = &[ |
| 81 |
"rpm", "xml", "zst", "gz", "xz", "bz2", "sqlite", "asc", "key", "sig", "yaml", "tar", |
| 82 |
]; |
| 83 |
|
| 84 |
impl S3Client { |
| 85 |
|
| 86 |
|
| 87 |
pub fn generate_key( |
| 88 |
user_id: UserId, |
| 89 |
item_id: ItemId, |
| 90 |
file_type: FileType, |
| 91 |
filename: &str, |
| 92 |
) -> S3Key { |
| 93 |
let safe_filename = sanitize_filename(filename); |
| 94 |
S3Key(format!( |
| 95 |
"{}/{}/{}/{}", |
| 96 |
user_id, |
| 97 |
item_id, |
| 98 |
file_type.as_str(), |
| 99 |
safe_filename |
| 100 |
)) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
pub fn generate_staging_key(filename: &str) -> S3Key { |
| 117 |
S3Key(format!( |
| 118 |
"staging/{}/{}", |
| 119 |
uuid::Uuid::new_v4(), |
| 120 |
sanitize_filename(filename) |
| 121 |
)) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
pub fn generate_artifact_key(path: &str) -> Result<S3Key> { |
| 151 |
let bad = |msg: &str| AppError::BadRequest(format!("invalid artifact path: {msg}")); |
| 152 |
|
| 153 |
if path.is_empty() { |
| 154 |
return Err(bad("empty")); |
| 155 |
} |
| 156 |
if path.len() > constants::ARTIFACT_MAX_KEY_BYTES { |
| 157 |
return Err(bad(&format!( |
| 158 |
"longer than {} bytes", |
| 159 |
constants::ARTIFACT_MAX_KEY_BYTES |
| 160 |
))); |
| 161 |
} |
| 162 |
if path.starts_with('/') { |
| 163 |
return Err(bad("must be relative, not absolute")); |
| 164 |
} |
| 165 |
|
| 166 |
let segments: Vec<&str> = path.split('/').collect(); |
| 167 |
if segments.len() > constants::ARTIFACT_MAX_KEY_SEGMENTS { |
| 168 |
return Err(bad(&format!( |
| 169 |
"more than {} path segments", |
| 170 |
constants::ARTIFACT_MAX_KEY_SEGMENTS |
| 171 |
))); |
| 172 |
} |
| 173 |
|
| 174 |
for segment in &segments { |
| 175 |
if segment.is_empty() { |
| 176 |
return Err(bad("empty path segment")); |
| 177 |
} |
| 178 |
if *segment == "." || *segment == ".." { |
| 179 |
return Err(bad("`.` and `..` are not path segments")); |
| 180 |
} |
| 181 |
if segment.starts_with('.') || segment.starts_with('-') { |
| 182 |
return Err(bad("a path segment may not start with `.` or `-`")); |
| 183 |
} |
| 184 |
if !segment |
| 185 |
.chars() |
| 186 |
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '+' | '~' | '-')) |
| 187 |
{ |
| 188 |
return Err(bad( |
| 189 |
"a path segment may hold only letters, digits, and `.` `_` `+` `~` `-`", |
| 190 |
)); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
let filename = segments.last().copied().unwrap_or_default(); |
| 197 |
let ext = filename |
| 198 |
.rsplit_once('.') |
| 199 |
.map(|(_, ext)| ext.to_ascii_lowercase()) |
| 200 |
.ok_or_else(|| bad("the final path segment needs a file extension"))?; |
| 201 |
if !ARTIFACT_EXTENSIONS.contains(&ext.as_str()) { |
| 202 |
return Err(bad(&format!( |
| 203 |
"`.{ext}` is not served from the artifact store. Allowed: {}", |
| 204 |
ARTIFACT_EXTENSIONS.join(", ") |
| 205 |
))); |
| 206 |
} |
| 207 |
|
| 208 |
Ok(S3Key(path.to_string())) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
pub fn content_key(user_id: UserId, sha256: &str, ext: &str) -> S3Key { |
| 219 |
S3Key(format!("{user_id}/c/{sha256}.{ext}")) |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
pub fn generate_version_key( |
| 230 |
user_id: UserId, |
| 231 |
item_id: ItemId, |
| 232 |
version_id: VersionId, |
| 233 |
filename: &str, |
| 234 |
) -> S3Key { |
| 235 |
let safe_filename = sanitize_filename(filename); |
| 236 |
S3Key(format!( |
| 237 |
"{}/{}/{}/{}/{}", |
| 238 |
user_id, |
| 239 |
item_id, |
| 240 |
FileType::Download.as_str(), |
| 241 |
version_id, |
| 242 |
safe_filename |
| 243 |
)) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
pub fn generate_insertion_key(user_id: UserId, filename: &str) -> S3Key { |
| 249 |
let safe_filename = sanitize_filename(filename); |
| 250 |
S3Key(format!("{user_id}/insertions/{safe_filename}")) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
pub fn generate_media_key(user_id: UserId, folder: &str, filename: &str) -> S3Key { |
| 256 |
let safe_filename = sanitize_filename(filename); |
| 257 |
let safe_folder = sanitize_folder(folder); |
| 258 |
if safe_folder.is_empty() { |
| 259 |
S3Key(format!("{user_id}/media/{safe_filename}")) |
| 260 |
} else { |
| 261 |
S3Key(format!("{user_id}/media/{safe_folder}/{safe_filename}")) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
pub fn generate_project_image_key(project_id: ProjectId, filename: &str) -> S3Key { |
| 268 |
let safe_filename = sanitize_filename(filename); |
| 269 |
S3Key(format!("projects/{project_id}/image/{safe_filename}")) |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
pub fn generate_ota_artifact_key( |
| 279 |
app_id: SyncAppId, |
| 280 |
version: &str, |
| 281 |
target: &str, |
| 282 |
arch: &str, |
| 283 |
) -> S3Key { |
| 284 |
S3Key(format!("ota/{app_id}/{version}/{target}/{arch}/artifact")) |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
pub fn generate_synckit_blob_key(app_id: SyncAppId, user_id: UserId, hash: &str) -> S3Key { |
| 292 |
S3Key(format!("{app_id}/{user_id}/{hash}")) |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
pub fn generate_content_export_key(user_id: UserId, timestamp: &str) -> S3Key { |
| 299 |
S3Key(format!("{user_id}/exports/content-{timestamp}.zip")) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
pub fn generate_item_gallery_key( |
| 307 |
user_id: UserId, |
| 308 |
item_id: ItemId, |
| 309 |
image_uuid: uuid::Uuid, |
| 310 |
filename: &str, |
| 311 |
) -> S3Key { |
| 312 |
let safe_filename = sanitize_filename(filename); |
| 313 |
S3Key(format!( |
| 314 |
"{user_id}/{item_id}/gallery/{image_uuid}/{safe_filename}" |
| 315 |
)) |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
pub fn generate_project_gallery_key( |
| 321 |
project_id: ProjectId, |
| 322 |
image_uuid: uuid::Uuid, |
| 323 |
filename: &str, |
| 324 |
) -> S3Key { |
| 325 |
let safe_filename = sanitize_filename(filename); |
| 326 |
S3Key(format!( |
| 327 |
"projects/{project_id}/gallery/{image_uuid}/{safe_filename}" |
| 328 |
)) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
#[allow(dead_code)] |
| 355 |
pub(crate) fn extension_for(filename: &str) -> String { |
| 356 |
let ext: String = std::path::Path::new(filename) |
| 357 |
.extension() |
| 358 |
.and_then(|s| s.to_str()) |
| 359 |
.unwrap_or("") |
| 360 |
.chars() |
| 361 |
.filter(char::is_ascii_alphanumeric) |
| 362 |
.map(|c| c.to_ascii_lowercase()) |
| 363 |
.take(16) |
| 364 |
.collect(); |
| 365 |
if ext.is_empty() { |
| 366 |
"bin".to_string() |
| 367 |
} else { |
| 368 |
ext |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
pub(crate) fn key_extension(key: &str) -> &str { |
| 376 |
key.rsplit('/') |
| 377 |
.next() |
| 378 |
.and_then(|base| base.rsplit_once('.').map(|(_, ext)| ext)) |
| 379 |
.filter(|ext| !ext.is_empty()) |
| 380 |
.unwrap_or("bin") |
| 381 |
} |
| 382 |
|
| 383 |
pub(crate) fn sanitize_filename(filename: &str) -> String { |
| 384 |
let sanitized: String = filename |
| 385 |
.chars() |
| 386 |
.filter(|c| c.is_alphanumeric() || *c == '.' || *c == '-' || *c == '_') |
| 387 |
.collect(); |
| 388 |
|
| 389 |
let stem = std::path::Path::new(&sanitized) |
| 390 |
.file_stem() |
| 391 |
.and_then(|s| s.to_str()) |
| 392 |
.unwrap_or(""); |
| 393 |
if stem.is_empty() { |
| 394 |
let ext = std::path::Path::new(&sanitized) |
| 395 |
.extension() |
| 396 |
.and_then(|s| s.to_str()) |
| 397 |
.unwrap_or(""); |
| 398 |
if ext.is_empty() { |
| 399 |
"file".to_string() |
| 400 |
} else { |
| 401 |
format!("file.{ext}") |
| 402 |
} |
| 403 |
} else { |
| 404 |
sanitized |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
pub fn sanitize_folder(folder: &str) -> String { |
| 411 |
let trimmed = folder.trim(); |
| 412 |
if trimmed.is_empty() { |
| 413 |
return String::new(); |
| 414 |
} |
| 415 |
|
| 416 |
if trimmed.contains("..") { |
| 417 |
return String::new(); |
| 418 |
} |
| 419 |
trimmed |
| 420 |
.chars() |
| 421 |
.filter(|c| c.is_alphanumeric() || *c == '-' || *c == '_') |
| 422 |
.collect() |
| 423 |
} |
| 424 |
|
| 425 |
#[cfg(test)] |
| 426 |
mod artifact_key_tests { |
| 427 |
use super::*; |
| 428 |
|
| 429 |
#[test] |
| 430 |
fn the_mirror_and_the_hotfix_repo_are_both_signable() { |
| 431 |
for path in [ |
| 432 |
"base/fedora-bootc-43-amd64.tar", |
| 433 |
"base/fedora-bootc-43-arm64.tar", |
| 434 |
"hotfix/f43/x86_64/alloy-1.0.0-1.fc43.x86_64.rpm", |
| 435 |
"hotfix/f43/x86_64/repodata/abc-primary.xml.zst", |
| 436 |
"hotfix/f43/x86_64/repodata/repomd.xml.asc", |
| 437 |
] { |
| 438 |
assert!( |
| 439 |
S3Client::generate_artifact_key(path).is_ok(), |
| 440 |
"{path} is store content and was refused" |
| 441 |
); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
#[test] |
| 449 |
fn registry_shaped_keys_are_refused() { |
| 450 |
for path in [ |
| 451 |
"v2/alloy/base/manifests/43", |
| 452 |
"v2/alloy/base/blobs/sha256:3f786850e387550fdab836ed7e6dc881de23001b", |
| 453 |
"base/blobs/sha256/3f786850e387550fdab836ed7e6dc881de23001b", |
| 454 |
"base/oci-layout", |
| 455 |
] { |
| 456 |
assert!( |
| 457 |
S3Client::generate_artifact_key(path).is_err(), |
| 458 |
"{path} is registry shape and the store does not serve one" |
| 459 |
); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
#[test] |
| 464 |
fn an_extension_the_store_does_not_serve_is_refused() { |
| 465 |
assert!(S3Client::generate_artifact_key("base/payload.sh").is_err()); |
| 466 |
assert!(S3Client::generate_artifact_key("base/index.html").is_err()); |
| 467 |
} |
| 468 |
} |
| 469 |
|