Skip to content

Commit e66d98a

Browse files
committed
sanitize oci tag
1 parent 3f9dc5e commit e66d98a

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

sbuild/src/ghcr.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,27 @@ pub fn ghcr_path(
214214
)
215215
}
216216

217+
/// Sanitize a string for use as an OCI tag
218+
/// OCI tags only allow: [a-zA-Z0-9_.-]
219+
pub fn sanitize_oci_tag(s: &str) -> String {
220+
s.chars()
221+
.map(|c| {
222+
if c.is_ascii_alphanumeric() || c == '_' || c == '.' || c == '-' {
223+
c
224+
} else {
225+
'-'
226+
}
227+
})
228+
.collect::<String>()
229+
// Remove consecutive dashes
230+
.split('-')
231+
.filter(|s| !s.is_empty())
232+
.collect::<Vec<_>>()
233+
.join("-")
234+
}
235+
217236
/// Generate GHCR tag from version and architecture
218237
pub fn ghcr_tag(version: &str, arch: &str) -> String {
219-
format!("{}-{}", version, arch.to_lowercase())
238+
let sanitized_version = sanitize_oci_tag(version);
239+
format!("{}-{}", sanitized_version, arch.to_lowercase())
220240
}

sbuild/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use colored::Colorize;
2121
use sbuild::{
2222
builder::Builder,
2323
checksum,
24-
ghcr::{GhcrClient, PackageAnnotations},
24+
ghcr::{sanitize_oci_tag, GhcrClient, PackageAnnotations},
2525
signing::Signer,
2626
types::SoarEnv,
2727
};
@@ -607,7 +607,8 @@ async fn post_build_processing(
607607

608608
// Get architecture
609609
let arch = format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS);
610-
let tag = format!("{}-{}", version, arch.to_lowercase());
610+
// Sanitize version for OCI tag (removes invalid chars like @)
611+
let tag = format!("{}-{}", sanitize_oci_tag(&version), arch.to_lowercase());
611612

612613
// Get pkg_family and recipe_name from recipe URL
613614
let (pkg_family, recipe_name) = recipe_url

0 commit comments

Comments
 (0)