Skip to content

commands,lfs: handle malformed pointers - #1796

Merged
ttaylorr merged 10 commits into
masterfrom
malformed-pointers
Dec 26, 2016
Merged

ttaylorr merged 10 commits into
masterfrom
malformed-pointers

Conversation

@ttaylorr

Copy link
Copy Markdown
Contributor

This pull-request fixes #1729 by streaming out the entire contents of a malformed pointer, instead of truncating the first 1024 bytes.

Previously, if LFS was unable to parse file that it thought was a pointer, it would "smudge" the file by just emitting it as is. The catch, however, was that the only data that it could hold onto was the first 1024 bytes. Since (*git.PktlineReader) Read()s block if there is no data on the pipe, buffering a fixed amount of data was the best that we could do. There were two cases:

  1. len(pbuf) < 1024: We received the entire contents, no join necessary.
  2. len(pbuf) == 1024: We might have received the entire contents, or there were more than 1024 bytes and we just read the first 1024 of them.

To handle this better, I introduced lfs.DecodePointerHasMore which mimics lfs.DecodePointer, but returns an additional bool, dictating whether or not more data is available to be read (in other words, where the (*git.PktlineReader) Read() error was == io.EOF or not). When using this in the commands package, we just check if there was more data, and join the reader as necessary:

var r io.Reader = bytes.NewBuffer(pbuf)
if more {
        r = io.MultiReader(r, src)
}

As an added bonus, I added some warning messages that get printed to os.Stderr and include the filenames of all malformed pointers. When using filter.lfs.process, the files are collected together and printed once at the end. When using the filter.lfs.clean mode, we print out an error once per file, instead of bundling them up. Short of storing the list on disk in .git/, this is the best that we can do.

To wrap things up, I audited all uses of lfs.DecodePointer, lfs.DecodeFromFile, et. al., and determined that there were no other instances in the code where we would only output partially buffered data.

Hooray!


Here's a quick break-down of what was done:

  1. cfcba6f: Introduce lfs.DecodePointerHasMore and use it in commands.smudge() to determine when to correctly join and copy the readers.
  2. b3bab24: Keep track of the malformed pointers seen in a given pull/checkout/clone and emit them at the end (or once per file, if we're using filter.lfs.clean instead of the process filter).

/cc @git-lfs/core #1729

@ttaylorr ttaylorr added this to the v1.5.4 milestone Dec 22, 2016
case "smudge":
w = git.NewPktlineWriter(os.Stdout, smudgeFilterBufferCapacity)
err = filterSmudge(w, req.Payload, req.Header["pathname"], skip, filter)
err = smudge(w, req.Payload, req.Header["pathname"], skip, filter)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an added benefit, I got rid of the intermediate filterSmudge function, so now the legacy filter.lfs.{clean,smudge} and filter.lfs.process share the exact same implementation! ✨

Comment thread lfs/pointer.go Outdated
// If the pointer could not be decoded, a buffer of the data read so far, along
// with whether or not there was more data not yet consumed will be returned
// instead.
func DecodeFromHasMore(reader io.Reader) ([]byte, bool, *Pointer, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for posterity, I mentioned in video chat that the DecodeFromHasMore return signature feels clunky. My suggestion was to change the DecodeFrom interface to (io.Reader, *Pointer, error), and let the caller just drain the returned reader if it wants. Callers shouldn't have to worry about how to join the read buffer with the io reader.

@ttaylorr
ttaylorr merged commit 39cbf99 into master Dec 26, 2016
@ttaylorr
ttaylorr deleted the malformed-pointers branch December 26, 2016 23:02
ttaylorr pushed a commit that referenced this pull request Dec 27, 2016
@ttaylorr ttaylorr modified the milestones: v1.5.4, v2.0.0 Dec 27, 2016
ttaylorr pushed a commit that referenced this pull request Dec 27, 2016
Backport #1796 for v1.5.x: commands,lfs: handle malformed pointers
@larsxschneider

Copy link
Copy Markdown
Member

Thanks @ttaylorr ! This works great. One minor nit. The Encountered 1 file(s) that should have been pointers, but weren't: does not work with git lfs clone. Consider this:

$ git clone --branch largefile [email protected]:larsxschneider/lfstest-individual-files.git smudge-error
Cloning into 'smudge-error'...
remote: Counting objects: 40, done.
remote: Total 40 (delta 0), reused 0 (delta 0), pack-reused 40
Receiving objects: 100% (40/40), 6.75 KiB | 0 bytes/s, done.
Resolving deltas: 100% (10/10), done.
Encountered 1 file(s) that should have been pointers, but weren't:
    big-file1.bin

$ git lfs clone --branch largefile [email protected]:larsxschneider/lfstest-individual-files.git smudge-error
Cloning into 'smudge-error'...
remote: Counting objects: 40, done.
remote: Total 40 (delta 0), reused 0 (delta 0), pack-reused 40
Receiving objects: 100% (40/40), 6.75 KiB | 0 bytes/s, done.
Resolving deltas: 100% (10/10), done.

@ttaylorr

ttaylorr commented Jan 5, 2017

Copy link
Copy Markdown
Contributor Author

One minor nit. The Encountered 1 file(s) that should have been pointers, but weren't: does not work with git lfs clone.

@larsxschneider: thanks for pointing this out! If my memory serves, the LFS clone command uses commands.pull() to do the checkout, which receives *lfs.WrappedPointers from the *lfs.gitscanner here. I think in order to make this work I'd just need to make lfs.DecodePointer return an error, or teach the gitscanner how to emit the invalid pointer errors.

I'll open an issue.

@larsxschneider

Copy link
Copy Markdown
Member

Unfortunately I found a bug if the invalid files exceed a certain size threshold.
The following clone will hang here:

git clone --branch largefile2 [email protected]:larsxschneider/lfstest-individual-files.git smudge-error2

I tested it on git version 2.11.0 with git-lfs/1.5.4 (GitHub; darwin amd64; go 1.7.4) and git lfs lastest master: a0d9487e.

@ttaylorr

ttaylorr commented Jan 9, 2017 via email

Copy link
Copy Markdown
Contributor Author

chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 11, 2021
When the "clean" filter processes a pre-existing LFS pointer
blob, the copyToTemp() function returns a CleanPointerError;
it does this when DecodeFrom() successfully reads and parses
the pointer blob and when the entirety of the blob's contents
have been read (i.e., there is no additional blob data to be
read).

This latter check was originally introduced in commit
e09e5e1 in PR git-lfs#271, when
the relevant code was in the Clean() method in the
pointer/clean.go file; it used the same 512 byte maximum
to determine if all the blob content had been read.  This
was aligned with the size of the byte array used in
DecodeFrom() in the pointer/pointer.go file.  The 512-byte
buffer created in DecodeFrom() was returned to Clean(),
which then checked its length to see if it had been fully
read or if there might still be additional data.

In commit f58db7f in
PR git-lfs#684 the size of the read buffer in DecodeFrom() was
changed from 512 bytes to the value of blobSizeCutoff, which
was 1024 (and remains so since).  However, the check in
Clean()'s copyToTemp() function was not changed at the same
time.  (Note that Clean() had been refactored and the check
was in copyToTemp(), where it remains.)

In commit 6f54232 in
PR git-lfs#1796 the DecodeFrom() method was changed to return an
io.Reader instead of a byte array, and copyToTemp() would
then read from that into a byte array using io.ReadAll().
That introduced a bug when handling large malformed pointers,
fixed in commit dcc0581 in
PR git-lfs#1852 by reading into a byte array in copyToTemp() with
blobSizeCutoff length.  However, the check for a resultant
data length of less than 512 bytes still remained in place.

In order to keep this blob size check in sync with the
allocated byte array in copyToTemp(), we therefore change
copyToTemp() so it checks if a pointer was successfully
read and the blob size is below blobSizeCutoff.  This
implies there is no more data to be read and we can
return a CleanPointerError (to signal a clean pointer was
found) and the byte array, which will then be written out,
leaving the blob's contents unchanged.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 11, 2021
When the "clean" filter processes a pre-existing LFS pointer
blob, the copyToTemp() function returns a CleanPointerError;
it does this when DecodeFrom() successfully reads and parses
the pointer blob and when the entirety of the blob's contents
have been read (i.e., there is no additional blob data to be
read).

This latter check was originally introduced in commit
e09e5e1 in PR git-lfs#271, when
the relevant code was in the Clean() method in the
pointer/clean.go file; it used the same 512 byte maximum
to determine if all the blob content had been read.  This
was aligned with the size of the byte array used in
DecodeFrom() in the pointer/pointer.go file.  The 512-byte
buffer created in DecodeFrom() was adjusted and returned to
Clean(), which then checked its length to see if it had been
populated to the 512-byte maximum, meaning there might still
be additional data to be read.

In commit f58db7f in
PR git-lfs#684 the size of the read buffer in DecodeFrom() was
changed from 512 bytes to the value of blobSizeCutoff, which
was 1024 (and remains so since).  However, the check in
Clean()'s copyToTemp() function was not changed at the same
time.  (Note that Clean() had been refactored and the check
was in copyToTemp(), where it remains.)

In commit 6f54232 in
PR git-lfs#1796 the DecodeFrom() method was changed to return an
io.Reader instead of a byte array, and copyToTemp() would
then read from that into a byte array using ioutil.ReadAll().
That introduced a bug when handling large malformed pointers,
fixed in commit dcc0581 in
PR git-lfs#1852 by reading into a byte array in copyToTemp() with
blobSizeCutoff length.  However, the check for a resultant
data length of less than 512 bytes still remained in place.

In order to keep this blob size check in sync with the
allocated byte array in copyToTemp(), we therefore change
copyToTemp() so it checks if a pointer was successfully
read and the blob size is below blobSizeCutoff.  This
implies there is no more data to be read and we can
return a CleanPointerError (to signal a clean pointer was
found) containing the byte array, which will then be written
out, leaving the blob's contents unchanged.
pcal43 pushed a commit to pcal43/git-lfs-hack that referenced this pull request Jul 22, 2021
When the "clean" filter processes a pre-existing LFS pointer
blob, the copyToTemp() function returns a CleanPointerError;
it does this when DecodeFrom() successfully reads and parses
the pointer blob and when the entirety of the blob's contents
have been read (i.e., there is no additional blob data to be
read).

This latter check was originally introduced in commit
e09e5e1 in PR git-lfs#271, when
the relevant code was in the Clean() method in the
pointer/clean.go file; it used the same 512 byte maximum
to determine if all the blob content had been read.  This
was aligned with the size of the byte array used in
DecodeFrom() in the pointer/pointer.go file.  The 512-byte
buffer created in DecodeFrom() was adjusted and returned to
Clean(), which then checked its length to see if it had been
populated to the 512-byte maximum, meaning there might still
be additional data to be read.

In commit f58db7f in
PR git-lfs#684 the size of the read buffer in DecodeFrom() was
changed from 512 bytes to the value of blobSizeCutoff, which
was 1024 (and remains so since).  However, the check in
Clean()'s copyToTemp() function was not changed at the same
time.  (Note that Clean() had been refactored and the check
was in copyToTemp(), where it remains.)

In commit 6f54232 in
PR git-lfs#1796 the DecodeFrom() method was changed to return an
io.Reader instead of a byte array, and copyToTemp() would
then read from that into a byte array using ioutil.ReadAll().
That introduced a bug when handling large malformed pointers,
fixed in commit dcc0581 in
PR git-lfs#1852 by reading into a byte array in copyToTemp() with
blobSizeCutoff length.  However, the check for a resultant
data length of less than 512 bytes still remained in place.

In order to keep this blob size check in sync with the
allocated byte array in copyToTemp(), we therefore change
copyToTemp() so it checks if a pointer was successfully
read and the blob size is below blobSizeCutoff.  This
implies there is no more data to be read and we can
return a CleanPointerError (to signal a clean pointer was
found) containing the byte array, which will then be written
out, leaving the blob's contents unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decoding a pointer is a fatal error - should we just warn?

3 participants