Repository navigation
commands,lfs: handle malformed pointers - #1796
Conversation
| 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) |
There was a problem hiding this comment.
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! ✨
| // 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) { |
There was a problem hiding this comment.
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.
Backport #1796 for v1.5.x: commands,lfs: handle malformed pointers
|
Thanks @ttaylorr ! This works great. One minor nit. The |
@larsxschneider: thanks for pointing this out! If my memory serves, the LFS I'll open an issue. |
|
Unfortunately I found a bug if the invalid files exceed a certain size threshold. I tested it on |
|
I can definitely reproduce this when cloning that same repository myself. What's
curious to me is that this seems unrelated to the `blobSizeCutoff` const, since
we have integration tests for 1023, 1024 and 1025-byte malformed pointers.
Perhaps this has to do with the implementation of `io.Copy`, and buffering
between Git<->LFS.
@larsxschneider - do you know of a particular filesize (or range) that causes
this behavior to occur?
|
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.
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.
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.
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:len(pbuf) < 1024: We received the entire contents, no join necessary.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.DecodePointerHasMorewhich mimicslfs.DecodePointer, but returns an additionalbool, dictating whether or not more data is available to be read (in other words, where the(*git.PktlineReader) Read()error was== io.EOFor not). When using this in thecommandspackage, we just check if there wasmoredata, and join the reader as necessary:As an added bonus, I added some warning messages that get printed to
os.Stderrand include the filenames of all malformed pointers. When usingfilter.lfs.process, the files are collected together and printed once at the end. When using thefilter.lfs.cleanmode, 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:
lfs.DecodePointerHasMoreand use it incommands.smudge()to determine when to correctly join and copy the readers.filter.lfs.cleaninstead of the process filter)./cc @git-lfs/core #1729