From ab2874e4cd668de2b473c8f0bdd8483cd8c79532 Mon Sep 17 00:00:00 2001 From: OrbitZore Date: Mon, 2 Nov 2026 02:37:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20/commit=20=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E6=8C=89=E4=BB=93=E5=BA=93=20oid=20=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=AE=8C=E6=95=B4=E9=95=BF=E5=BA=A6,?= =?UTF-8?q?=E6=B6=88=E9=99=A4=2064-hex=20=E5=89=8D=E7=BC=80=E5=88=AB?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题(#6):pathmap::is_full_oid 同时接受 40 与 64 位小写 hex,而 SHA-1 仓库上非实验构建的 git_oid_fromstr 恰好解析 40 个字符、无视余下 24 位 ——/commit/<64-hex> 被静默解析为其前 40 位前缀命中的 commit(实测命中 HEAD),违反 RFC 0000 §3.1"完整小写 oid,长度须与仓库对象格式一致"。 实现:resolve_locked 的 Ns::Commit 分支在 is_full_oid 之外叠加按 repo_->oid_type() 的长度校验(SHA-1=40 / 其他=64,三元式与 rawobj.cpp:29 既有惯例一致,不引入版本敏感的 git_oid_hexsize);长度 不符即 ENOENT。其余逻辑(tree 漫游、无 peeling、replace refs 不生效) 不动。附注:SHA-256 仓库本就无法挂载(git_repository_open 返回 "unknown object format"),64-hex 形态当前只能表现为本别名缺陷。 测试:tests/integration/run_tests.sh 追加三例——40-hex 真实 commit 目录可列举(回归)、64-hex(40-hex+24 个 0)oid 与深层路径均 ENOENT; 红/绿验证:无修复时新用例 2 FAIL(113 passed / 2 failed),加修复后 115 passed / 0 failed(本机 /dev/fuse + fusermount3 实跑)。 CHANGELOG:Unreleased/Fixed 记录。 Closes #6 --- CHANGELOG.md | 13 +++++++++++++ src/gitmount.cpp | 9 ++++++--- tests/integration/run_tests.sh | 9 +++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b96f654..d3e4d8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,19 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed +- `/commit/` no longer silently truncates a 64-hex component to its + 40-char prefix on SHA-1 repositories. The component length is now + validated against the repository's object format per RFC 0000 §3.1 + (a full oid is exactly 40 hex on SHA-1, 64 on SHA-256); a + wrong-length string returns ENOENT instead of resolving to whatever + commit its 40-char prefix names. Note: SHA-256 repositories remain + unmountable altogether — libgit2 rejects them at open time with + `unknown object format` — so on current builds the 64-hex form could + only ever manifest as this prefix alias. + ## [0.0.4] - 2026-09-26 ### Fixed diff --git a/src/gitmount.cpp b/src/gitmount.cpp index e750f38..0f7c987 100644 --- a/src/gitmount.cpp +++ b/src/gitmount.cpp @@ -473,9 +473,12 @@ int Gitmount::resolve_locked(const std::string& path, Node* out) { case Ns::Remote: return resolve_remote_locked(p.comps, out); case Ns::Commit: { - // Full lowercase oid only, pointing at a commit object itself — - // no peeling, replace refs never honored (RFC 0000 §3.1). - if (!pathmap::is_full_oid(p.comps[0])) return -ENOENT; + // Full lowercase oid only, matching the repository's object format + // (RFC 0000 §3.1): a 64-hex string on a SHA-1 repository is not a + // full oid — libgit2's git_oid_fromstr would silently parse only the + // first 40 chars (prefix alias). + const std::size_t hexsz = repo_->oid_type() == GIT_OID_SHA1 ? 40 : 64; + if (p.comps[0].size() != hexsz || !pathmap::is_full_oid(p.comps[0])) return -ENOENT; git_oid oid{}; if (git_oid_fromstr(&oid, p.comps[0].c_str()) != 0) return -ENOENT; if (!repo_->is_commit_object(oid)) return -ENOENT; diff --git a/tests/integration/run_tests.sh b/tests/integration/run_tests.sh index 2c6e826..61dfe81 100755 --- a/tests/integration/run_tests.sh +++ b/tests/integration/run_tests.sh @@ -201,6 +201,15 @@ zz" "$(ls -1 "$MNT/tag/foo")" check "/commit rejects uppercase oid" \ test ! -e "$MNT/commit/$(echo "$MAIN_TIP" | tr a-z A-Z)" check "/commit rejects short prefix" test ! -e "$MNT/commit/${MAIN_TIP:0:8}" + # 64-hex is not a full oid on this SHA-1 fixture: before the length check + # git_oid_fromstr silently parsed the first 40 chars (prefix alias, #6). + OVERLONG_OID="${MAIN_TIP}000000000000000000000000" # 40 + 24 = 64 hex + check "/commit 40-hex commit dir lists (SHA-1 full oid)" \ + ls "$MNT/commit/$MAIN_TIP/src" + check "/commit rejects 64-hex oid (SHA-1 prefix alias)" \ + test ! -e "$MNT/commit/$OVERLONG_OID" + check "/commit rejects 64-hex deep path" \ + test ! -e "$MNT/commit/$OVERLONG_OID/src/hello.txt" # --- refs/replace not honored (§3.1) --- check_eq "replace refs ignored (original R1 content)" "original R1" \