Elffuss BraidHub

utopia-ia/elffuss-braid public

verify: a long branch name gives its record a hashed file name

A branch name in hex is twice as long, past what a file name may hold for
a long one. A name whose hex would be longer than 200 bytes gives the file
`b3-` and the BLAKE3 hash of the name instead, which no hex name can
look like.
Changesetbf223502faa3da1262b2783e6f6914e5a1676e6fa0e699a9ce0faee2bc8e829d
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 15:54 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 15:54 +02:00
Parents9598fdbd3dec

Browse the files at this changeset

3 files changed

modified Cargo.lock +1 −0
@@ -503,6 +503,7 @@
503503 version = "0.1.1"
504504 dependencies = [
505505 "anyhow",
506+ "blake3",
506507 "clap",
507508 "elffuss-braid-engine",
508509 "elffuss-braid-format",
modified crates/cli/Cargo.toml +1 −0
@@ -22,6 +22,7 @@
2222 braid-store.workspace = true
2323 braid-wc.workspace = true
2424 anyhow.workspace = true
25+blake3.workspace = true
2526 clap.workspace = true
2627 serde.workspace = true
2728 serde_json.workspace = true
modified crates/cli/src/verify.rs +40 −2
@@ -69,6 +69,10 @@
6969 /// than this long, in seconds, before now: a day.
7070 const HISTORY_AGE: i64 = 86_400;
7171
72+/// The longest record file name made of the branch's name in hex; a longer
73+/// name is hashed instead.
74+const MAX_HEX_NAME: usize = 200;
75+
7276 /// What the watchdog says once it runs, before braid starts any test. The
7377 /// number changes with the protocol, so that another braid's watchdog is
7478 /// never taken for this one's.
@@ -273,9 +277,16 @@
273277
274278 impl Record {
275279 /// The file of `branch`: its name in hex, so that no two branches share
276- /// a file, even on a file system that ignores case.
280+ /// a file, even on a file system that ignores case, or for a name whose
281+ /// hex would be longer than [`MAX_HEX_NAME`], `b3-` and the BLAKE3 hash
282+ /// of the name, which no name in hex can look like.
277283 fn path(repo: &Repo, branch: &str) -> PathBuf {
278- let name: String = branch.bytes().map(|byte| format!("{byte:02x}")).collect();
284+ let hex: String = branch.bytes().map(|byte| format!("{byte:02x}")).collect();
285+ let name = if hex.len() <= MAX_HEX_NAME {
286+ hex
287+ } else {
288+ format!("b3-{}", blake3::hash(branch.as_bytes()).to_hex())
289+ };
279290 repo.root()
280291 .join(braid_repo::DIR)
281292 .join("verify")
@@ -1322,6 +1333,33 @@
13221333 assert!(err.contains("delete it"), "{err}");
13231334 }
13241335
1336+ #[test]
1337+ fn long_branch_names_are_hashed() {
1338+ let temp = tempfile::tempdir().unwrap();
1339+ let repo = Repo::init(temp.path()).unwrap();
1340+ let name = |branch: &str| {
1341+ Record::path(&repo, branch)
1342+ .file_name()
1343+ .unwrap()
1344+ .to_string_lossy()
1345+ .into_owned()
1346+ };
1347+ let longest = "b".repeat(MAX_HEX_NAME / 2);
1348+ assert_eq!(
1349+ name(&longest),
1350+ format!("{}.json", "62".repeat(longest.len()))
1351+ );
1352+ let longer = format!("{longest}x");
1353+ let hashed = name(&longer);
1354+ assert_eq!(
1355+ hashed,
1356+ format!("b3-{}.json", blake3::hash(longer.as_bytes()).to_hex())
1357+ );
1358+ assert_ne!(name(&format!("{longest}y")), hashed);
1359+ // Even the longest branch name a ref can have fits a file name.
1360+ assert!(name(&"z".repeat(4096)).len() < 100);
1361+ }
1362+
13251363 #[test]
13261364 fn the_hourly_cap_counts_by_each_reverts_own_time() {
13271365 let temp = tempfile::tempdir().unwrap();