use crate::hash::{Hash, ZERO};
use core::fmt;
pub const MAGIC: [u8; 4] = *b"MKT1";
pub const SCHEMA_VERSION: u8 = 0x01;
pub const IDENTITY_MAX_LEN: u16 = 4096;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ObjectType {
Blob = 0x01,
Tree = 0x02,
Commit = 0x03,
Remix = 0x04,
ChunkedBlob = 0x05,
Delta = 0x06,
Tag = 0x07,
}
impl ObjectType {
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Blob => "blob",
Self::Tree => "tree",
Self::Commit => "commit",
Self::Remix => "remix",
Self::ChunkedBlob => "chunked_blob",
Self::Delta => "delta",
Self::Tag => "tag",
}
}
pub(crate) fn from_u8(b: u8) -> Result<Self, MkitError> {
Ok(match b {
0x01 => Self::Blob,
0x02 => Self::Tree,
0x03 => Self::Commit,
0x04 => Self::Remix,
0x05 => Self::ChunkedBlob,
0x06 => Self::Delta,
0x07 => Self::Tag,
other => return Err(MkitError::InvalidObjectType(other)),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum EntryMode {
Blob = 0x01,
Tree = 0x02,
Symlink = 0x03,
Executable = 0x04,
}
impl EntryMode {
pub(crate) fn from_u8(b: u8) -> Result<Self, MkitError> {
Ok(match b {
0x01 => Self::Blob,
0x02 => Self::Tree,
0x03 => Self::Symlink,
0x04 => Self::Executable,
other => return Err(MkitError::InvalidEntryMode(other)),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum IdentityKind {
Ed25519 = 0x01,
DidKey = 0x02,
Opaque = 0x03,
}
impl IdentityKind {
pub(crate) fn from_u8(b: u8) -> Result<Self, MkitError> {
Ok(match b {
0x01 => Self::Ed25519,
0x02 => Self::DidKey,
0x03 => Self::Opaque,
other => return Err(MkitError::UnknownIdentityKind(other)),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Identity {
pub kind: IdentityKind,
pub bytes: Vec<u8>,
}
impl Identity {
#[must_use]
pub fn ed25519(pubkey: [u8; 32]) -> Self {
Self {
kind: IdentityKind::Ed25519,
bytes: pubkey.to_vec(),
}
}
#[must_use]
pub fn opaque(bytes: impl Into<Vec<u8>>) -> Self {
Self {
kind: IdentityKind::Opaque,
bytes: bytes.into(),
}
}
#[must_use]
pub fn is_valid(&self) -> bool {
if self.bytes.is_empty() || self.bytes.len() > IDENTITY_MAX_LEN as usize {
return false;
}
match self.kind {
IdentityKind::Ed25519 => self.bytes.len() == 32,
IdentityKind::DidKey => self.bytes.iter().all(u8::is_ascii_graphic),
IdentityKind::Opaque => true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TreeEntry {
pub name: Vec<u8>,
pub mode: EntryMode,
pub object_hash: Hash,
}
impl TreeEntry {
#[must_use]
pub fn validate_name(name: &[u8]) -> bool {
if name.is_empty() || name.len() > 255 {
return false;
}
if name == b"." || name == b".." {
return false;
}
if name.iter().any(|&b| matches!(b, 0 | b'/' | b'\\')) {
return false;
}
if matches!(name.last(), Some(b'.' | b' ')) {
return false;
}
if name.eq_ignore_ascii_case(b".mkit") || name.eq_ignore_ascii_case(b".git") {
return false;
}
let stem = match name.iter().position(|&b| b == b'.') {
Some(i) => &name[..i],
None => name,
};
if is_windows_reserved_stem(stem) {
return false;
}
true
}
}
fn is_windows_reserved_stem(stem: &[u8]) -> bool {
match stem.len() {
3 => {
stem.eq_ignore_ascii_case(b"CON")
|| stem.eq_ignore_ascii_case(b"PRN")
|| stem.eq_ignore_ascii_case(b"AUX")
|| stem.eq_ignore_ascii_case(b"NUL")
}
4 => {
let head = &stem[..3];
let tail = stem[3];
let is_digit_1_9 = matches!(tail, b'1'..=b'9');
is_digit_1_9 && (head.eq_ignore_ascii_case(b"COM") || head.eq_ignore_ascii_case(b"LPT"))
}
_ => false,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RemixSource {
pub upstream_id: Hash,
pub commit_hash: Hash,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Blob {
pub data: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tree {
pub entries: Vec<TreeEntry>,
}
impl Tree {
#[must_use]
pub fn is_sorted(&self) -> bool {
self.entries
.windows(2)
.all(|w| w[0].name.as_slice() < w[1].name.as_slice())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Commit {
pub tree_hash: Hash,
pub parents: Vec<Hash>,
pub author: Identity,
pub signer: [u8; 32],
pub message: Vec<u8>,
pub timestamp: u64,
pub message_hash: Hash,
pub content_digest: Hash,
pub signature: [u8; 64],
}
impl Commit {
#[must_use]
pub fn new_unannotated(
tree_hash: Hash,
parents: Vec<Hash>,
author: Identity,
signer: [u8; 32],
message: Vec<u8>,
timestamp: u64,
signature: [u8; 64],
) -> Self {
Self {
tree_hash,
parents,
author,
signer,
message,
timestamp,
message_hash: ZERO,
content_digest: ZERO,
signature,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Remix {
pub tree_hash: Hash,
pub parents: Vec<Hash>,
pub sources: Vec<RemixSource>,
pub author: Identity,
pub signer: [u8; 32],
pub message: Vec<u8>,
pub timestamp: u64,
pub signature: [u8; 64],
}
impl Remix {
#[must_use]
pub fn sources_sorted(&self) -> bool {
self.sources.windows(2).all(|w| {
let a = &w[0];
let b = &w[1];
match a.upstream_id.cmp(&b.upstream_id) {
core::cmp::Ordering::Less => true,
core::cmp::Ordering::Greater => false,
core::cmp::Ordering::Equal => a.commit_hash < b.commit_hash,
}
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tag {
pub target: Hash,
pub target_type: ObjectType,
pub name: Vec<u8>,
pub tagger: Identity,
pub signer: [u8; 32],
pub message: Vec<u8>,
pub timestamp: u64,
pub signature: [u8; 64],
}
pub const TAG_NAME_MAX_LEN: u16 = 4096;
impl Tag {
#[must_use]
pub fn name_is_valid(&self) -> bool {
if self.name.is_empty() || self.name.len() > TAG_NAME_MAX_LEN as usize {
return false;
}
!self.name.iter().any(|&b| matches!(b, 0 | b'/' | b'\\'))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChunkedBlob {
pub total_size: u64,
pub chunk_size: u32,
pub chunks: Vec<Hash>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delta {
pub base_hash: Hash,
pub result_size: u32,
pub instructions: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Object {
Blob(Blob),
Tree(Tree),
Commit(Commit),
Remix(Remix),
ChunkedBlob(ChunkedBlob),
Delta(Delta),
Tag(Tag),
}
impl Object {
#[must_use]
pub fn object_type(&self) -> ObjectType {
match self {
Self::Blob(_) => ObjectType::Blob,
Self::Tree(_) => ObjectType::Tree,
Self::Commit(_) => ObjectType::Commit,
Self::Remix(_) => ObjectType::Remix,
Self::ChunkedBlob(_) => ObjectType::ChunkedBlob,
Self::Delta(_) => ObjectType::Delta,
Self::Tag(_) => ObjectType::Tag,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum MkitError {
#[error("input is shorter than the 6-byte v1 prologue")]
EmptyData,
#[error("object_type byte {0:#04x} is not in 0x01..=0x07")]
InvalidObjectType(u8),
#[error("magic at offset 1 is not \"MKT1\"")]
InvalidMagic,
#[error("schema_version byte is not 0x01")]
UnsupportedObjectVersion,
#[error("input ended before a complete field could be read")]
UnexpectedEof,
#[error("non-empty trailing bytes after a complete object")]
TrailingData,
#[error("tree.entry_count > 1_000_000")]
TooManyEntries,
#[error("tree entry name is empty, too long, or contains a forbidden byte")]
InvalidEntryName,
#[error("tree entry mode byte {0:#04x} is not one of 0x01..=0x04")]
InvalidEntryMode(u8),
#[error("tree entries are not lexicographically sorted / contain duplicates")]
InvalidEntryOrder,
#[error("parent_count > 1_000")]
TooManyParents,
#[error("remix.source_count > 10_000")]
TooManySources,
#[error("tag name is empty, too long, or contains a forbidden byte (\\0 / \\)")]
TagNameInvalid,
#[error("tag target_type byte {0:#04x} is not a storable object type")]
TagTargetTypeInvalid(u8),
#[error("remix sources are not sorted by (upstream_id, commit_hash)")]
InvalidSourceOrder,
#[error("chunked_blob.chunk_count > 1_000_000")]
TooManyChunks,
#[error("identity kind byte {0:#04x} is not 0x01..=0x03")]
UnknownIdentityKind(u8),
#[error("identity has zero-length payload, or is Ed25519 with len != 32")]
InvalidIdentity,
#[error("identity payload len > {}", IDENTITY_MAX_LEN)]
IdentityTooLarge,
#[error("oversized payload in field `{field}`: {len} bytes > u32::MAX")]
OversizePayload { field: &'static str, len: usize },
#[error("rng failed to produce key material")]
RngFailure,
#[error("signature verification failed")]
SignatureInvalid,
#[error("public key is not a valid Ed25519 point")]
InvalidPublicKey,
#[error("key file mode {actual:#o} is broader than 0600")]
InsecureKeyPermissions { actual: u32 },
#[error("key file owner uid {actual} does not match process euid {euid}")]
InsecureKeyOwner { actual: u32, euid: u32 },
#[error("key directory mode {actual:#o} is broader than 0700")]
InsecureKeyDir { actual: u32 },
#[error("key path {0} is a symlink — refused")]
KeyPathIsSymlink(String),
#[error("key file size {actual} is not 32 bytes (raw Ed25519 seed)")]
InvalidKeyLength { actual: usize },
#[error("key file I/O error: {0}")]
KeyIo(String),
#[error("delta length {len} exceeds u32::MAX for field `{field}`")]
DeltaLengthOverflow { field: &'static str, len: usize },
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Object::{}", self.object_type().name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn object_type_names() {
assert_eq!(ObjectType::Blob.name(), "blob");
assert_eq!(ObjectType::Tree.name(), "tree");
assert_eq!(ObjectType::Commit.name(), "commit");
assert_eq!(ObjectType::Remix.name(), "remix");
assert_eq!(ObjectType::ChunkedBlob.name(), "chunked_blob");
assert_eq!(ObjectType::Delta.name(), "delta");
assert_eq!(ObjectType::Tag.name(), "tag");
}
#[test]
fn object_type_from_u8_accepts_valid_range() {
for b in 0x01u8..=0x07 {
assert!(
ObjectType::from_u8(b).is_ok(),
"byte {b:#04x} should decode"
);
}
}
#[test]
fn object_type_from_u8_rejects_zero_and_high() {
assert!(matches!(
ObjectType::from_u8(0x00),
Err(MkitError::InvalidObjectType(0))
));
assert!(matches!(
ObjectType::from_u8(0xFF),
Err(MkitError::InvalidObjectType(0xFF))
));
assert!(matches!(
ObjectType::from_u8(0x08),
Err(MkitError::InvalidObjectType(0x08))
));
}
#[test]
fn tag_name_validity() {
let t = |name: &[u8]| Tag {
target: ZERO,
target_type: ObjectType::Commit,
name: name.to_vec(),
tagger: Identity::ed25519([0xaa; 32]),
signer: [0; 32],
message: vec![],
timestamp: 0,
signature: [0; 64],
};
assert!(t(b"v1.0.0").name_is_valid());
assert!(!t(b"").name_is_valid());
assert!(!t(b"a/b").name_is_valid());
assert!(!t(b"a\\b").name_is_valid());
assert!(!t(b"a\0b").name_is_valid());
assert!(!t(&vec![b'a'; TAG_NAME_MAX_LEN as usize + 1]).name_is_valid());
}
#[test]
fn tree_entry_name_rejects_empty() {
assert!(!TreeEntry::validate_name(b""));
}
#[test]
fn tree_entry_name_rejects_separators_and_null() {
assert!(!TreeEntry::validate_name(b"foo/bar"));
assert!(!TreeEntry::validate_name(b"foo\\bar"));
assert!(!TreeEntry::validate_name(b"fo\0o"));
}
#[test]
fn tree_entry_name_rejects_dot_and_dotdot() {
assert!(!TreeEntry::validate_name(b"."));
assert!(!TreeEntry::validate_name(b".."));
}
#[test]
fn tree_entry_name_accepts_common() {
assert!(TreeEntry::validate_name(b"file.txt"));
assert!(TreeEntry::validate_name(b"a"));
assert!(TreeEntry::validate_name(b"foo-bar_baz.rs"));
}
#[test]
fn tree_entry_name_rejects_over_255() {
let long = vec![b'a'; 256];
assert!(!TreeEntry::validate_name(&long));
}
#[test]
fn tree_entry_name_rejects_dot_mkit_and_dot_git_case_insensitive() {
assert!(!TreeEntry::validate_name(b".mkit"));
assert!(!TreeEntry::validate_name(b".git"));
assert!(!TreeEntry::validate_name(b".MKIT"));
assert!(!TreeEntry::validate_name(b".Mkit"));
assert!(!TreeEntry::validate_name(b".GIT"));
assert!(!TreeEntry::validate_name(b".Git"));
assert!(TreeEntry::validate_name(b".mkitignore"));
assert!(TreeEntry::validate_name(b".gitignore"));
}
#[test]
fn tree_entry_name_rejects_trailing_dot_or_space() {
assert!(!TreeEntry::validate_name(b"foo."));
assert!(!TreeEntry::validate_name(b"foo "));
assert!(!TreeEntry::validate_name(b"foo..."));
assert!(!TreeEntry::validate_name(b"foo "));
assert!(TreeEntry::validate_name(b"foo.bar"));
assert!(TreeEntry::validate_name(b"foo bar"));
}
#[test]
fn tree_entry_name_rejects_windows_reserved_device_names() {
for n in [
b"CON".as_slice(),
b"PRN",
b"AUX",
b"NUL",
b"COM1",
b"COM9",
b"LPT1",
b"LPT9",
b"con",
b"Nul",
b"lpt3",
b"CON.txt",
b"nul.log",
b"COM1.dat",
] {
assert!(
!TreeEntry::validate_name(n),
"expected Windows reserved name rejected: {:?}",
std::str::from_utf8(n).unwrap_or("?")
);
}
assert!(TreeEntry::validate_name(b"COM0"));
assert!(TreeEntry::validate_name(b"LPT0"));
assert!(TreeEntry::validate_name(b"COM10"));
assert!(TreeEntry::validate_name(b"CONSOLE"));
assert!(TreeEntry::validate_name(b"NULL"));
}
#[test]
fn identity_rejects_empty_payload_all_kinds() {
for kind in [
IdentityKind::Ed25519,
IdentityKind::DidKey,
IdentityKind::Opaque,
] {
assert!(
!Identity {
kind,
bytes: Vec::new()
}
.is_valid()
);
}
}
#[test]
fn identity_rejects_oversize() {
let bytes = vec![0xaa; IDENTITY_MAX_LEN as usize + 1];
assert!(
!Identity {
kind: IdentityKind::Opaque,
bytes
}
.is_valid()
);
}
#[test]
fn identity_requires_32_bytes_for_ed25519() {
assert!(
!Identity {
kind: IdentityKind::Ed25519,
bytes: vec![0xaa; 16]
}
.is_valid()
);
assert!(Identity::ed25519([0xaa; 32]).is_valid());
}
#[test]
fn didkey_requires_printable_ascii_multibase() {
let didkey = |b: &[u8]| Identity {
kind: IdentityKind::DidKey,
bytes: b.to_vec(),
};
assert!(didkey(b"z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK").is_valid());
assert!(didkey(b"mEiB1234").is_valid());
assert!(!didkey(b"z\0\x01\x02").is_valid());
assert!(!didkey(&[0xde, 0xad, 0xbe, 0xef]).is_valid());
assert!(!didkey(b"z6Mk has space").is_valid());
assert!(!didkey(b"z6Mk\n").is_valid());
}
#[test]
fn tree_is_sorted_checks() {
let e = |n: &[u8]| TreeEntry {
name: n.to_vec(),
mode: EntryMode::Blob,
object_hash: ZERO,
};
let sorted = Tree {
entries: vec![e(b"alpha"), e(b"beta"), e(b"gamma")],
};
assert!(sorted.is_sorted());
let unsorted = Tree {
entries: vec![e(b"beta"), e(b"alpha")],
};
assert!(!unsorted.is_sorted());
let dup = Tree {
entries: vec![e(b"alpha"), e(b"alpha")],
};
assert!(!dup.is_sorted());
}
#[test]
fn remix_sources_sorted_checks() {
let src = |u: u8, c: u8| RemixSource {
upstream_id: [u; 32],
commit_hash: [c; 32],
};
let r = |sources| Remix {
tree_hash: ZERO,
parents: vec![],
sources,
author: Identity::ed25519([0xaa; 32]),
signer: [0; 32],
message: vec![],
timestamp: 0,
signature: [0; 64],
};
assert!(r(vec![src(1, 1), src(1, 2), src(2, 1)]).sources_sorted());
assert!(!r(vec![src(2, 1), src(1, 1)]).sources_sorted());
assert!(!r(vec![src(1, 1), src(1, 1)]).sources_sorted());
}
}