use ostrya_core::{Checksum, is_ref_name, is_valid_collection_id};
use super::sigv4::uri_encode;
use crate::{Error, Result};
const SINGLE_KEYS: [&str; 5] = [
"config",
"summary",
"summary.sig",
"extensions/ostrya/lock",
"extensions/ostrya/lock-probe",
];
const OBJECT_EXTENSIONS: [&str; 6] = [
"commit",
"commitmeta",
"dirtree",
"dirmeta",
"filez",
"tombstone-commit",
];
pub fn is_repository_key(key: &str) -> bool {
if SINGLE_KEYS.contains(&key) {
return true;
}
if let Some(rest) = key.strip_prefix("objects/") {
return is_object_key(rest);
}
if let Some(name) = key.strip_prefix("refs/heads/") {
return is_ref_name(name);
}
if let Some(rest) = key.strip_prefix("refs/mirrors/") {
return rest
.split_once('/')
.is_some_and(|(id, name)| is_valid_collection_id(id) && is_ref_name(name));
}
if let Some(rest) = key.strip_prefix("deltas/") {
return is_delta_key(rest);
}
if let Some(rest) = key.strip_prefix("delta-indexes/") {
return is_delta_index_key(rest);
}
if let Some(rest) = key.strip_prefix("state/") {
return rest
.strip_suffix(".commitpartial")
.is_some_and(|hex| is_lower_hex(hex, 64));
}
false
}
pub(crate) fn check_key(key: &str) -> Result<()> {
if is_repository_key(key) {
Ok(())
} else {
Err(Error::InvalidInput(format!(
"S3 key: {key:?} is not a key of a bucket repository"
)))
}
}
pub(crate) fn check_list_prefix(prefix: &str) -> Result<()> {
let refused = |why: &str| {
Err(Error::InvalidInput(format!(
"S3 list prefix: {prefix:?} {why}"
)))
};
let Some(segments) = prefix.strip_suffix('/') else {
return refused("does not end in '/'");
};
for segment in segments.split('/') {
if segment.is_empty() || segment == "." || segment == ".." {
return refused("holds an empty segment, '.', or '..'");
}
let allowed = |c: char| {
if c.is_ascii() {
c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | '_' | '~' | '+')
} else {
c.is_alphanumeric()
}
};
if !segment.chars().all(allowed) {
return refused("holds a character outside the alphabet of a key");
}
}
Ok(())
}
pub(crate) fn encode_path(key: &str) -> String {
let mut path = String::with_capacity(key.len());
uri_encode(key.as_bytes(), false, &mut path);
path
}
pub(crate) fn encode_query(value: &str) -> String {
let mut encoded = String::with_capacity(value.len());
uri_encode(value.as_bytes(), true, &mut encoded);
encoded
}
pub(crate) fn decode_listed(text: &str, encoded: bool) -> String {
if !encoded {
return text.to_owned();
}
let bytes = text.as_bytes();
let mut decoded = Vec::with_capacity(bytes.len());
let mut at = 0;
while at < bytes.len() {
match bytes[at] {
b'+' => {
decoded.push(b' ');
at += 1;
}
b'%' => {
let Some(byte) = bytes
.get(at + 1..at + 3)
.and_then(|hex| std::str::from_utf8(hex).ok())
.filter(|hex| hex.bytes().all(|b| b.is_ascii_hexdigit()))
.and_then(|hex| u8::from_str_radix(hex, 16).ok())
else {
return text.to_owned();
};
decoded.push(byte);
at += 3;
}
byte => {
decoded.push(byte);
at += 1;
}
}
}
String::from_utf8(decoded).unwrap_or_else(|_| text.to_owned())
}
fn is_lower_hex(text: &str, len: usize) -> bool {
text.len() == len
&& text
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
fn is_object_key(rest: &str) -> bool {
let Some((fan, name)) = rest.split_once('/') else {
return false;
};
let Some((hex, ext)) = name.split_once('.') else {
return false;
};
is_lower_hex(fan, 2) && is_lower_hex(hex, 62) && OBJECT_EXTENSIONS.contains(&ext)
}
fn is_split_checksum(head: &str, tail: &str) -> bool {
head.len() == 2 && Checksum::from_base64_modified(&format!("{head}{tail}")).is_ok()
}
fn is_delta_key(rest: &str) -> bool {
let mut parts = rest.split('/');
let (Some(head), Some(tail), Some(target), None) =
(parts.next(), parts.next(), parts.next(), parts.next())
else {
return false;
};
let names_delta = match tail.split_once('-') {
Some((from, to)) => {
is_split_checksum(head, from) && Checksum::from_base64_modified(to).is_ok()
}
None => is_split_checksum(head, tail),
};
names_delta && is_delta_target(target)
}
fn is_delta_target(target: &str) -> bool {
target == "superblock"
|| (!target.is_empty()
&& target.len() <= 10
&& target.bytes().all(|b| b.is_ascii_digit())
&& (target == "0" || !target.starts_with('0')))
}
fn is_delta_index_key(rest: &str) -> bool {
let Some((head, file)) = rest.split_once('/') else {
return false;
};
let Some(tail) = file.strip_suffix(".index") else {
return false;
};
!tail.contains('/') && is_split_checksum(head, tail)
}
#[cfg(test)]
mod tests {
use super::*;
fn checksum_b64() -> String {
let mut bytes = [0u8; 32];
bytes[0] = 0xfb;
bytes[1] = 0xff;
bytes[2] = 0xbf;
let b64 = Checksum::from_bytes(bytes).to_base64_modified();
assert!(b64.contains('+') && b64.contains('_'), "{b64}");
b64
}
fn other_b64() -> String {
Checksum::from_bytes([7u8; 32]).to_base64_modified()
}
#[test]
fn each_kind_of_repository_key_passes() {
let hex = "0123456789abcdef".repeat(4);
let to = checksum_b64();
let from = other_b64();
let mut keys = vec![
"config".to_owned(),
"summary".to_owned(),
"summary.sig".to_owned(),
"extensions/ostrya/lock".to_owned(),
"extensions/ostrya/lock-probe".to_owned(),
"refs/heads/main".to_owned(),
"refs/heads/exampleos/x86_64/stable".to_owned(),
"refs/heads/a-b.c_d9".to_owned(),
"refs/heads/grün".to_owned(),
"refs/mirrors/org.example.Os/main".to_owned(),
"refs/mirrors/org.example.Os/exampleos/x86_64/stable".to_owned(),
format!("refs/mirrors/org.{}/main", "a".repeat(251)),
format!("deltas/{}/{}/superblock", &to[..2], &to[2..]),
format!("deltas/{}/{}/0", &to[..2], &to[2..]),
format!("deltas/{}/{}/12", &to[..2], &to[2..]),
format!("deltas/{}/{}-{to}/superblock", &from[..2], &from[2..]),
format!("delta-indexes/{}/{}.index", &to[..2], &to[2..]),
format!("state/{hex}.commitpartial"),
];
for ext in OBJECT_EXTENSIONS {
keys.push(format!("objects/{}/{}.{ext}", &hex[..2], &hex[2..]));
}
for key in keys {
assert!(is_repository_key(&key), "{key}");
check_key(&key).unwrap();
}
}
#[test]
fn a_key_out_of_the_rules_is_refused() {
let hex = "0123456789abcdef".repeat(4);
let to = checksum_b64();
let keys = [
String::new(),
"/config".to_owned(),
"../config".to_owned(),
"config/".to_owned(),
"summary.sig.tmp".to_owned(),
"extensions/ostrya/other".to_owned(),
"extensions/ostrya/lock-probe/x".to_owned(),
"extensions/ostrya/lock-probe2".to_owned(),
format!("objects/{}/{}.commit", &hex[..2], &hex[2..63]),
format!("objects/AB/{}.commit", &hex[2..]),
format!("objects/ab/{}.commit", hex[2..].to_uppercase()),
format!("objects/{}/{}.COMMIT", &hex[..2], &hex[2..]),
format!("objects/{}/{}.file", &hex[..2], &hex[2..]),
format!("objects/{}/{}.unknown", &hex[..2], &hex[2..]),
format!("objects/zz/{}.commit", &hex[2..]),
"objects/ab/..".to_owned(),
"objects/abc".to_owned(),
"refs/heads/-x".to_owned(),
"refs/heads/".to_owned(),
"refs/heads/a//b".to_owned(),
"refs/heads/a b".to_owned(),
"refs/mirrors/x".to_owned(),
"refs/mirrors/org.example.Os/".to_owned(),
"refs/mirrors//main".to_owned(),
"refs/mirrors/nodot/main".to_owned(),
"refs/mirrors/org.example.Os/-x".to_owned(),
format!("refs/mirrors/org.{}/main", "a".repeat(252)),
"refs/remotes/origin/main".to_owned(),
format!("deltas/{}/{}/01", &to[..2], &to[2..]),
format!("deltas/{}/{}/meta", &to[..2], &to[2..]),
format!("deltas/{}/{}/", &to[..2], &to[2..]),
format!("deltas/{}/{}x/superblock", &to[..2], &to[2..]),
format!("deltas/{}/{}/superblock/x", &to[..2], &to[2..]),
format!("deltas/{}/{}/superblock", &to[..3], &to[3..]),
"deltas/AA/!!!/superblock".to_owned(),
format!("delta-indexes/{}/{}.idx", &to[..2], &to[2..]),
format!("state/{}.commitpartial", hex.to_uppercase()),
format!("state/{}.commitpartial", &hex[..63]),
format!("state/{hex}0.commitpartial"),
format!("state/{hex}.commit"),
format!("state/{hex}"),
format!("state/ab/{hex}.commitpartial"),
];
for key in keys {
assert!(!is_repository_key(&key), "{key}");
let err = check_key(&key).unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{err}");
}
}
#[test]
fn a_list_prefix_ends_in_a_slash() {
for prefix in [
"objects/",
"objects/ab/",
"refs/heads/",
"deltas/",
"refs/heads/grün/",
] {
check_list_prefix(prefix).unwrap();
}
for prefix in [
"",
"/",
"objects",
"objects//",
"./",
"../",
"a/../",
"a b/",
"a%2F/",
] {
assert!(check_list_prefix(prefix).is_err(), "{prefix}");
}
}
#[test]
fn a_path_encodes_each_byte_outside_the_unreserved_set() {
assert_eq!(encode_path("repo/config"), "repo/config");
assert_eq!(encode_path("deltas/a+/b_c~d"), "deltas/a%2B/b_c~d");
assert_eq!(encode_path("refs/heads/grün"), "refs/heads/gr%C3%BCn");
assert_eq!(encode_path("a b%"), "a%20b%25");
}
#[test]
fn a_query_value_encodes_slash_and_plus() {
assert_eq!(encode_query("repo/objects/"), "repo%2Fobjects%2F");
assert_eq!(encode_query("a+b=c/d"), "a%2Bb%3Dc%2Fd");
assert_eq!(encode_query("1ueGcx~_-."), "1ueGcx~_-.");
}
#[test]
fn a_listed_key_decodes_as_s3_encodes_it() {
assert_eq!(decode_listed("enc/a%2Bb", true), "enc/a+b");
assert_eq!(decode_listed("enc/sp+ace", true), "enc/sp ace");
assert_eq!(decode_listed("enc/%C3%BC", true), "enc/ü");
assert_eq!(decode_listed("enc/pct%2541", true), "enc/pct%41");
assert_eq!(decode_listed("enc/a+b", false), "enc/a+b");
assert_eq!(decode_listed("enc/%G1", true), "enc/%G1");
assert_eq!(decode_listed("enc/%E9", true), "enc/%E9");
assert_eq!(decode_listed("enc/%4", true), "enc/%4");
}
}