From c92395ce3809730a6b098d4a6b7dfaf09d35306b Mon Sep 17 00:00:00 2001 From: Victor M Varela Date: Sat, 31 Oct 2026 07:30:59 +0100 Subject: [PATCH 1/2] feat: accept historical Git LFS pointer variants under 1024 bytes Extend the LFS pointer classifier to mirror git-lfs 3.8.1's decoder: the three historical version aliases (git-lfs, hawser, git-media), CRLF line endings, surrounding whitespace, blank lines and ext-* extensions are now recognized for pointers smaller than 1024 bytes. Pointers 1024 bytes or larger (padded) and a zero or malformed size still stop the run, because the git-lfs scanner cannot transfer them. Refs #5 --- README.md | 4 +- lfs_pointer.go | 105 +++++++++++++++++++++++++++++++++++++------- lfs_pointer_test.go | 22 +++++----- 3 files changed, 101 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 84fa7ba..f347e50 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Use it to keep a mirror, a staging copy or a public fork in sync with a reposito ## What it copies - Branches and tags. Matching destination refs are overwritten; destination-only refs are kept. -- Git LFS objects referenced by canonical spec-v1 pointers (see Limits). +- Git LFS objects referenced by spec-v1 pointers (see Limits). - Releases, their uploaded assets and the Latest selection. - Description, homepage and topics. - Labels. @@ -84,7 +84,7 @@ Both flags are optional and independent; without them the active account is used - **Check the direction.** Source wins for matching refs. A dry run reports what would change; it does not guarantee that server-side protections will accept the push. - **Ref renames can be blocked.** Replacing `release` with `release/v2` fails while `release` still exists in the destination, because destination-only refs are never deleted. Delete or rename it, then rerun. -- **Git LFS support is limited to canonical spec-v1 pointers.** Objects referenced by a canonical pointer (`version https://git-lfs.github.com/spec/v1`, exactly three LF lines, no extensions, smaller than 1024 bytes) are downloaded, verified and uploaded before any ref is published, using `git-lfs` 3.8.1 or newer. Anything else that looks like LFS — older version aliases, CRLF, padding, extensions, or versions it does not recognize — stops the run before any destination change, including pointers only in history. The scan reads every blob reachable from branches and tags, so it can be slow on large histories (raise `--timeout`), and it is conservative: an ordinary file containing a known LFS version line may be reported too. +- **Git LFS support covers spec-v1 pointers smaller than 1024 bytes.** Their objects are downloaded, verified and uploaded before any ref is published, using `git-lfs` 3.8.1 or newer. The three historical version aliases (`git-lfs`, `hawser`, `git-media`), CRLF line endings, surrounding whitespace, blank lines and `ext-*` extensions are accepted. Pointers 1024 bytes or larger (padded), a zero or malformed size, an unknown key, or a version it does not recognize stop the run before any destination change, including pointers only in history. The scan reads every blob reachable from branches and tags, so it can be slow on large histories (raise `--timeout`), and it is conservative: an ordinary file containing a known LFS version line may be reported too. - **Some assets need a second step.** A changed release asset is uploaded under a temporary name, verified, then swapped in, so its name is briefly missing. An interrupted run leaves either the previous asset or the verified replacement; a rerun finishes the work, and a leftover temporary asset may need deleting from the release. - **Invalid input stops the run before any write.** More than 20 topics, an empty release tag or a duplicated release tag, including drafts, abort the replication. Releases are matched by tag. - **Retries are bounded.** Read requests retry twice for selected transient failures and short rate limits; write requests are never replayed automatically, so rerun instead. diff --git a/lfs_pointer.go b/lfs_pointer.go index 20578fa..38a367e 100644 --- a/lfs_pointer.go +++ b/lfs_pointer.go @@ -1,7 +1,9 @@ package main import ( + "bufio" "bytes" + "errors" "fmt" "regexp" "strconv" @@ -20,45 +22,114 @@ type lfsPointer struct { var lfsOIDRE = regexp.MustCompile(`\Asha256:[0-9a-f]{64}\z`) +// lfsVersionAliases are the three version URLs Git LFS 3.8.1 accepts. +var lfsVersionAliases = []string{ + "http://git-media.io/v/2", + "https://hawser.github.com/spec/v1", + "https://git-lfs.github.com/spec/v1", +} + +// lfsExtRE matches an extension line key (ext-N-name), mirroring Git LFS 3.8.1. +var lfsExtRE = regexp.MustCompile(`\Aext-\d{1}-\w+\z`) + +// lfsPointerKeys is the required key order Git LFS 3.8.1 enforces. +var lfsPointerKeys = [3]string{"version", "oid", "size"} + // classifyLFSBlob inspects the first min(blobSize, 1024) bytes of a non-empty // blob, passed as prefix. Results: // - none: no known version alias or recognizable Git LFS version line. -// - confirmed: a complete, canonical v1 pointer with positive size. +// - confirmed: a Git LFS pointer the transfer path copies (any 3.8.1 alias, +// CRLF, surrounding whitespace, blank lines and extensions, canonical size). // - blocked: a candidate outside the supported subset; callers must stop. // -// shortcut: noncanonical pointers remain blocked until their transfer is proven; -// unknown future formats cannot be detected universally. Callers skip empty blobs. +// It mirrors Git LFS 3.8.1 pointer.DecodeFrom/decodeKV for everything the +// transfer path copies; oversized (>=1024 bytes) and zero/non-canonical sizes +// stay blocked because the CLI scanner cannot transfer them. Unknown future +// formats cannot be detected universally. Callers skip empty blobs. func classifyLFSBlob(prefix []byte, blobSize int64) (p lfsPointer, found bool, blocked string) { // Fail closed first: "none" is only valid for the complete committed prefix. if blobSize <= 0 || int64(len(prefix)) != min(blobSize, lfsPrefixSize) { return p, false, "prefix does not match min(blob size, 1024) bytes" } - candidate := isLFSPointer(prefix) + if !isLFSPointer(prefix) && !hasFutureLFSVersion(prefix) { + return lfsPointer{}, false, "" + } + if blobSize >= lfsPrefixSize { + return p, false, "Git LFS pointer is too large for the supported format" + } + p, err := decodeLFSKV(bytes.TrimSpace(prefix)) + if err != nil { + return lfsPointer{}, false, err.Error() + } + return p, true, "" +} + +// hasFutureLFSVersion detects a recognizable future Git LFS version line so a +// newer pointer blocks instead of being mistaken for ordinary content. +func hasFutureLFSVersion(prefix []byte) bool { for _, line := range bytes.Split(prefix, []byte("\n")) { if bytes.HasPrefix(bytes.TrimSpace(line), []byte("version https://git-lfs.github.com/spec/")) { - candidate = true + return true } } - if !candidate { - return lfsPointer{}, false, "" + return false +} + +// decodeLFSKV mirrors Git LFS 3.8.1 pointer.decodeKV over already-trimmed data: +// version/oid/size in order, extension lines allowed before any of them, a +// canonical positive size, and nothing after size. Extension values are not +// needed to transfer (the stored object is keyed by the main OID), so they are +// accepted by key only. +func decodeLFSKV(data []byte) (lfsPointer, error) { + kvps := make(map[string]string, 3) + line := 0 + sc := bufio.NewScanner(bytes.NewReader(data)) + for sc.Scan() { + text := sc.Text() + if len(text) == 0 { + continue + } + if len(lfsPointerKeys) <= line { + return lfsPointer{}, errors.New("Git LFS pointer has content after size") + } + key, value, ok := strings.Cut(text, " ") + if !ok { + return lfsPointer{}, errors.New("Git LFS pointer line is missing a value") + } + if key != lfsPointerKeys[line] { + if !lfsExtRE.MatchString(key) { + return lfsPointer{}, fmt.Errorf("unsupported Git LFS pointer key %q", key) + } + continue // extension line; value is irrelevant to transfer + } + kvps[key] = value + line++ } - if blobSize >= lfsPrefixSize { - return p, false, "Git LFS pointer is too large for the supported format" + if err := sc.Err(); err != nil { + return lfsPointer{}, err } - lines := strings.Split(string(prefix), "\n") - if len(lines) != 4 || lines[0] != "version https://git-lfs.github.com/spec/v1" || lines[3] != "" { - return p, false, "unsupported or noncanonical Git LFS pointer" + if !validLFSVersion(kvps["version"]) { + return lfsPointer{}, errors.New("unsupported Git LFS version") } - oid, ok := strings.CutPrefix(lines[1], "oid ") + oid, ok := kvps["oid"] if !ok || !lfsOIDRE.MatchString(oid) { - return p, false, "invalid pointer oid" + return lfsPointer{}, errors.New("invalid Git LFS pointer object id") } - sizeText, ok := strings.CutPrefix(lines[2], "size ") + sizeText, ok := kvps["size"] size, err := strconv.ParseInt(sizeText, 10, 64) if !ok || err != nil || size <= 0 || strconv.FormatInt(size, 10) != sizeText { - return p, false, "invalid or empty pointer size" + return lfsPointer{}, errors.New("invalid Git LFS pointer size") + } + return lfsPointer{OID: strings.TrimPrefix(oid, "sha256:"), Size: size}, nil +} + +func validLFSVersion(v string) bool { + for _, a := range lfsVersionAliases { + if v == a { + return true + } } - return lfsPointer{OID: strings.TrimPrefix(oid, "sha256:"), Size: size}, true, "" + return false } // lfsInventory maps each OID to its size so conflicting pointers are caught. diff --git a/lfs_pointer_test.go b/lfs_pointer_test.go index 43d3776..96874b3 100644 --- a/lfs_pointer_test.go +++ b/lfs_pointer_test.go @@ -34,18 +34,18 @@ func lfsCases() []lfsCase { long := "version " + ptrV1 + "\n" + ptrBody return []lfsCase{ {name: "lfs", data: long, found: true}, - {name: "hawser", data: "version " + hawser + "\n" + ptrBody, blocked: true}, - {name: "git-media", data: "version " + media + "\n" + ptrBody, blocked: true}, - {name: "crlf", data: crlf(long), blocked: true}, - {name: "leading whitespace", data: " \n\t" + long, blocked: true}, - {name: "no trailing newline", data: strings.TrimSuffix(long, "\n"), blocked: true}, - {name: "padded 1023", data: pad(long, 1023), blocked: true}, + {name: "hawser", data: "version " + hawser + "\n" + ptrBody, found: true}, + {name: "git-media", data: "version " + media + "\n" + ptrBody, found: true}, + {name: "crlf", data: crlf(long), found: true}, + {name: "leading whitespace", data: " \n\t" + long, found: true}, + {name: "no trailing newline", data: strings.TrimSuffix(long, "\n"), found: true}, + {name: "padded 1023", data: pad(long, 1023), found: true}, {name: "padded 1024", data: pad(long, 1024), blocked: true}, {name: "padded 1025", data: pad(long, 1025), blocked: true}, {name: "padded 2127", data: pad(long, 2127), blocked: true}, - {name: "blank lines between", data: "version " + ptrV1 + "\n\n" + ptrBody, blocked: true}, - {name: "extension", data: "version " + ptrV1 + "\next-0-foo " + ptrOID + "\n" + ptrBody, blocked: true}, - {name: "extension before version", data: "ext-0-foo " + ptrOID + "\n" + long, blocked: true}, + {name: "blank lines between", data: "version " + ptrV1 + "\n\n" + ptrBody, found: true}, + {name: "extension", data: "version " + ptrV1 + "\next-0-foo " + ptrOID + "\n" + ptrBody, found: true}, + {name: "extension before version", data: "ext-0-foo " + ptrOID + "\n" + long, found: true}, {name: "swapped order", data: "version " + ptrV1 + "\nsize 12345\noid " + ptrOID + "\n", blocked: true}, {name: "missing size", data: "version " + ptrV1 + "\noid " + ptrOID + "\n", blocked: true}, {name: "zero size", data: "version " + ptrV1 + "\noid " + ptrOID + "\nsize 0\n", blocked: true}, @@ -203,10 +203,10 @@ func TestLFSInventoryHistoryTagsAndObsolete(t *testing.T) { func TestLFSInventoryFailsClosed(t *testing.T) { r := &replicator{ctx: context.Background(), timeout: time.Minute} conflict := lfsWorkRepo(t, map[string][]byte{"x": lfsPtr('a', "10"), "y": lfsPtr('a', "11")}) - ext := lfsWorkRepo(t, map[string][]byte{"x": []byte("version " + ptrV1 + "\next-0-foo " + ptrOID + "\n" + ptrBody)}) + upper := lfsWorkRepo(t, map[string][]byte{"x": []byte("version " + ptrV1 + "\noid sha256:" + strings.ToUpper(ptrHex) + "\nsize 5\n")}) for name, c := range map[string]struct{ dir, want string }{ "conflicting sizes": {conflict, "conflicting Git LFS sizes"}, - "extension": {ext, "unsupported or noncanonical"}, + "uppercase oid": {upper, "invalid Git LFS pointer object id"}, } { t.Run(name, func(t *testing.T) { inv, err := invNow(r, c.dir) From f2756abb078152a825767f68d9ef9e9e9f5106ea Mon Sep 17 00:00:00 2001 From: Victor M Varela Date: Sat, 31 Oct 2026 08:52:57 +0100 Subject: [PATCH 2/2] docs: clarify unrecognized LFS version handling in limits --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f347e50..e5f8bc9 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Both flags are optional and independent; without them the active account is used - **Check the direction.** Source wins for matching refs. A dry run reports what would change; it does not guarantee that server-side protections will accept the push. - **Ref renames can be blocked.** Replacing `release` with `release/v2` fails while `release` still exists in the destination, because destination-only refs are never deleted. Delete or rename it, then rerun. -- **Git LFS support covers spec-v1 pointers smaller than 1024 bytes.** Their objects are downloaded, verified and uploaded before any ref is published, using `git-lfs` 3.8.1 or newer. The three historical version aliases (`git-lfs`, `hawser`, `git-media`), CRLF line endings, surrounding whitespace, blank lines and `ext-*` extensions are accepted. Pointers 1024 bytes or larger (padded), a zero or malformed size, an unknown key, or a version it does not recognize stop the run before any destination change, including pointers only in history. The scan reads every blob reachable from branches and tags, so it can be slow on large histories (raise `--timeout`), and it is conservative: an ordinary file containing a known LFS version line may be reported too. +- **Git LFS support covers spec-v1 pointers smaller than 1024 bytes.** Their objects are downloaded, verified and uploaded before any ref is published, using `git-lfs` 3.8.1 or newer. The three historical version aliases (`git-lfs`, `hawser`, `git-media`), CRLF line endings, surrounding whitespace, blank lines and `ext-*` extensions are accepted. Pointers 1024 bytes or larger (padded), a zero or malformed size, an unknown key, or a newer `git-lfs.github.com` version stop the run before any destination change, including pointers only in history. The scan reads every blob reachable from branches and tags, so it can be slow on large histories (raise `--timeout`), and it is conservative: an ordinary file containing a known LFS version line may be reported too. - **Some assets need a second step.** A changed release asset is uploaded under a temporary name, verified, then swapped in, so its name is briefly missing. An interrupted run leaves either the previous asset or the verified replacement; a rerun finishes the work, and a leftover temporary asset may need deleting from the release. - **Invalid input stops the run before any write.** More than 20 topics, an empty release tag or a duplicated release tag, including drafts, abort the replication. Releases are matched by tag. - **Retries are bounded.** Read requests retry twice for selected transient failures and short rate limits; write requests are never replayed automatically, so rerun instead.