Repository navigation
Enhanced transfers part 5: external custom transfer adapters - #1367
Conversation
Server should just pick the 'best' one it recognises, simplifies things and in practice this will be most appropriate anyway
Also update docs to indicate custom adapter does not need to check SHAs.
Need to move the custom adapter init into a sync.Once which happens JIT instead of during init(). Loading Config (via AllGitConfig()) that early causes problems I don't quite understand
| var args string | ||
| var concurrent bool | ||
| var direction string | ||
| // retrieve other values |
There was a problem hiding this comment.
I would change the operation on the substring match, so it continues if there's no match. This reduces the level of indentation.
Also, it wasn't immediately clear what the 3 vars are for, until we got to the closure below. I think a quick comment would be helpful, since vars are unusual.
There was a problem hiding this comment.
I keep forgetting Ruby style ;)
Actually the explicit var declarations aren't needed any more; they were at one point but the code iterated out of needing them without me realising, I'll take them out.
|
This looks good, once the last comments have been addressed 🤘 |
| the final completion message: | ||
|
|
||
| ```json | ||
| { "id":"progress", "oid": "22ab5f63670800cc7be06dbed816012b0dc411e774754c7579467d2536a9cf3e", "bytesSoFar": 1234, "bytesSinceLast": 64 } |
There was a problem hiding this comment.
Until now, I don't think any of our API JSON has had multi-word keys. Here we're camelcasing them, e.g. bytesSoFar, but in the locking proposal we've got snakecase going on. Should we standardize on one or the other? I have no opinion of one over the other.
There was a problem hiding this comment.
Dang, good catch. I prefer snake casing for JSON. But, arguments can be made for using camel casing. Go fields typically use camel case. Javascript fields do too, and it's where JSON came from (even if the spec doesn't really specify a preference).
There was a problem hiding this comment.
I'm with Richard Hendrix in preferring not to waste all those bytes on underscores ;)
I've mostly seen style guides that state camelCase (like Googles) but I've seen real-world examples that use underscores or dashes too. Guess it's still a personal thing - as the first person to hit multi-word properties I'm temped to stick my flag in for camelCase but won't bust a gasket if you want me to change it.
Used to need to but code has changed since then
|
|
||
| * `event`: Always "complete" to identify this message | ||
| * `oid`: the identifier of the LFS object | ||
| * `error`: Should contain a `code` and `message` explaining the error |
There was a problem hiding this comment.
This documentation for error is slightly different than the documentation for error on the download complete message. This one doesn't say what error should be if there is no error, the other doesn't say what error should be if there is an error. And if there is no error must it be null or can it also be absent?
There was a problem hiding this comment.
error can always be absent if there is no error - I tried to make it clearer by showing 2 versions of the complete message (it's the same in both the upload and download sections) - it first shows the struct if the upload/download is complete (with no error) and then what it should look like with an error. In practice it's just one structure with error (and path in the case of download) both being optional depending on the circumstances, but I thought it was clearer showing an explicit example in each case. I might be wrong 😄
There was a problem hiding this comment.
Helps if I read what I actually wrote vs what I think I wrote - actually it's the other error doc which is wrong and shouldn't be talking about null.
|
Are we saying that all LFS servers MUST support the basic protocol? Let's say I'm using a custom adapter to store my objects somewhere else entirely and there is no manner in which I can access my objects using the basic http protocols. What does it look like if my client does not have the custom adapter installed and thus cannot negotiate it? Along with its preferred negotiated transfer type should the server return information about all the transfer types it supports so we could give better error messages? |
|
Is there an intended way for a custom adapter to get any configuration options or is that left up to each adapter? I'm thinking things like service auth keys, preferred local tmp/download directories, stuff like that. |
I considered passing the git-lfs config or even the entire gitconfig to the process in the init struct at first, but I decided against it and pared it down to a minimum, because it would put unnecessary overhead on parsing the init struct (gitconfigs can get pretty large), and it might not even include the settings an adapter might need anyway. So beyond |
The So we're not saying LFS servers have to support |
|
Looking good. It was raining last night, limiting my Pokemon hunting, so I wrote my own toy adapter to see how it'd go. Pretty straight forward to implement and test using the protocol as documented. I dig it. My adapter used minio as a backend, which is all http so a custom adapter isn't really necessary, but it's what I happened to be looking at so I went with it. Some code is here. |
|
@rubyist nice, glad you found it easy enough to implement, that was my hope 👍 Although HTTP get/put is pretty easy & ubiquitous I like that this gets you out of a bind whether you want to do something just a little more specific but still over HTTP (outside just changing url/headers), or if you want to do something more wacky with totally different protocols. I can think of cases of both but this way we don't have to worry about all the variations. |
|
🎉 |
In commit 357703e of PR git-lfs#1367 we implemented the original version of the configureCustomAdapters() function in what is now our "tq" package, and in that commit we first defined the regular expression we intend to match any Git configuration options of the form "lfs.customtransfer.<name>.path". However, this regular expression does not allow for case-insensitive matching of the "customtransfer" prefix of the sub-section portion of the configuration option's key. Git treats the entire sub-section portion of a key case-sensitively, so we do as well, specifically in the caseFoldKey() method of our GitFetcher structure in our "config" package. Thus when we subsequently read the other "lfs.customtransfer.<name>.*" keys using this method, via the Get() and Bool() methods of the "environment" interface type from our "config" package, they also match case-sensitively against the "customtransfer.<name>" sub-section portion of the keys. As we would like to match case-insensitively against the "customtransfer" portion in particular, while treating the name of the custom transfer adapter case-sensitively, we now adjust our regular expression to extract the whole sub-section as a case-insensitive match. We then use this matched string in the subsequent calls to the Get() and Bool() methods when we search for any other "lfs.customtransfer.<name>.*" keys. One consequence of this change is that while we now support option names such as "lfs.customTransfer.myAdapter.path", we expect all the related options to have exactly the same sub-section portion of their names, e.g., "lfs.customTransfer.myAdapter.args" rather than "lfs.customtransfer.myadapter.args". Note, though, that this was always true in the past as well, except that we did not match any keys unless the prefix of their sub-section name was the all-lowercase string "customtransfer". Regardless, we now update our git-lfs-config(5) manual page to explicitly clarify that the sub-section portions of these related options must all be identical. In addition to these changes, we also fix a bug in the regular expression which has persisted since its introduction, namely that it matched any character in the instances where we intended it to match only dot (i.e., period) characters. Thus we might previously have matched against an option named "lfs.customtransfer-foo-path", but will no longer do so.
In previous commit in this PR we resolved several problems with our legacy test of the Git LFS client's support for HTTP/1.1 chunked transfer encoding during Git LFS object uploads. One of these problems resulted from a mismatch between the name of the repository created by the previous version of the test and the URL path prefix that was expected by the testingChunkedTransferEncoding() function in our "lfstest-gitserver" test utility program. When the "chunked transfer encoding" test was first introduced, it was located in a shell script named "test-chunked-transfer-encoding.sh" in a directory named "test". The test passed the name of its containing script to the basename(1) command and then used the result as the name of the test's primary Git repository. Our "lfstest-gitserver" utility's testingChunkedTransferEncoding() function then checked for a URL path which began with the prefix "/test-chunked-transfer-encoding" in order to match the name of the test's repository. In PR #3125 we renamed the name of our shell test directory from "test" to "t", and in commit 219b7e0 of that PR we renamed all our shell test scripts to begin with "t-" rather than "test-". An unintended consequence of this change, though, was that the testingChunkedTransferEncoding() function would no longer recognize the repository name from the "chunked transfer encoding" test. Due to the design of the shell test, it continued to pass, but without exercising the behaviour of the Git LFS client it was intended to verify. Fortunately, the legacy "chunked transfer encoding" test was the only test affected in this way by the shell script name changes from PR #3125. While the "lfstest-gitserver" utility at that point had several functions similar to the testingChunkedTransferEncoding() function, each of which checked for a specific repository name prefix in the URL path of a request, the tests which relied on these functions to recognize their repositories all used fixed names for their repositories rather than deriving them from the names of the enclosing scripts. To help resolve the problems with the "chunked transfer encoding" test, we replaced it with a new "batch storage HTTP upload with chunked transfer encoding" test in our "t/t-batch-storage-encoding.sh" script. This test uses a fixed name for its repository rather than one dynamically determined from the name of the containing script. We then updated the testingChunkedTransferEncoding() function in our server utility to check that the current request's repository name exactly matched that fixed string. We also revised the testingChunkedTransferEncoding() function so that its sole parameter is a repository name string rather than a Request structure from the Go standard library's "http" package. This change resolved another problem with the legacy "chunked transfer encoding" test that was due to the fact that the URL paths of requests to upload individual Git LFS objects do not begin with the repository name as an initial path component, and the original test's implementation did not take account of this. Although the testingChunkedTransferEncoding() function was called while handling an object upload, it could only ever return "false" in this case because the URL path would never begin with a repository name. There are three other functions in our "lfstest-gitserver" utility which were all modelled on the testingChunkedTransferEncoding() function, and as such all check the URL path of a Request structure against a specific prefix. These functions are named testingTusUploadInBatchReq(), testingTusUploadInterruptedInBatchReq(), and testingCustomTransfer(). As we would like to reduce the chance that we might in the future accidentally introduce problems similar to those we found with the "chunked transfer encoding" test, we now revise these three functions so their input parameters are repository names rather than Request structures. The functions are all called from the lfsBatchHandler() function of the server utility, which has already extracted a repository name from the initial path component of the current request, so in each case we can just pass the "repo" variable to the three functions. We also update the repository names used by the associated shell tests to eliminate the legacy "test-" prefix and to align the repository names more closely with the names of the tests as they presently exist. For instance, since PR #6207 the tests which rely on the testingTusUploadInBatchReq() function and the testingTusUploadInterruptedInBatchReq() function are now in the "t/t-batch-storage-upload-tus.sh" script, but were originally introduced in a "test/test-resume-tus.sh" script in PR #1303. Meanwhile, the tests which rely on the testingCustomTransfer() function are now in the "t/t-custom-transfers.sh" script but were first introduced in a "test/test-custom-transfers.sh" script in PR #1367. Finally, we update the testingTusUploadInterruptedInBatchReq() function so it performs a more constrained check of the given repository name, as it only needs to return "true" if the name exactly matches the string "batch-storage-upload-tus-interrupt".
In commit 357703e of PR #1367 we implemented the original version of the configureCustomAdapters() function in what is now our "tq" package, and in that commit we first defined the regular expression we intend to match any Git configuration options of the form "lfs.customtransfer.<name>.path". However, this regular expression is not required to match the entirety of a given configuration option's name, and so it will match configuration options with leading components other than "lfs" and will also match configuration options with trailing components other than "path". Such options may be defined in an ".lfsconfig" file, and as a consequence, the Git LFS client may be induced to run a potentially malicious executable when fetching objects. This vulnerability has been assigned the identifier CVE-2026-105803. To address this vulnerability, we add constraints to the regular expression used by the configureCustomAdapters() function to match "lfs.customtransfer.<name>.path" configuration options. These constraints now require the expression's pattern to match the entirety of a given configuration option's name. We then add a new Go test function and a set of new shell integration tests to confirm that we have resolved the vulnerability. Both types of tests check that custom transfer adapter programs can no longer be configured using options whose names match the pattern "lfs.customtransfer.<name>.path" but do not match that pattern exclusively. Our new tests specifically verify that options of the form "lfs.<url>.access" and "remote.<name>.lfsurl", both of which are permitted in ".lfsconfig" files, can not be used to configure a custom transfer adapter. In each of our new shell integration tests, we clone a repository with an ".lfsconfig" file that contains an option matching either the "lfs.*.access" pattern or the "remote.*.lfsurl" pattern, but whose central portion has the form "lfs.customtransfer.<name>.path". The value of these options is a relative path to a local file named "adapter" (with an appropriate ".exe" file extension on Windows systems), and the repository includes a copy of our "lfstest-exploitcheck" test utility with that name. As we described in the prior commit in which we renamed the "lfstest-exploitcheck" utility from its previous name of "lfstest-badpathcheck", this program creates an empty sentinel file named "exploit" in the current working directory. Before exiting, it then writes the string "exploit" to its standard output and error file descriptors. In our new shell tests, after we clone the repository containing an ".lfsconfig" file and a copy of the "lfstest-exploitcheck" utility, we check that no sentinel file has been created, as it would be without the change in this commit to the regular expression used by the configureCustomAdapters() function. Absent that change, the Git LFS client would execute the utility as a custom transfer adapter because a configuration option in the ".lfsconfig" file matched the regular expression. If the client were to execute the utility as a custom transfer adapter, it would write a JSON message to the utility's standard input and expect a JSON-formatted response from its standard output. Since the utility only writes the string "exploit" to its standard output, the client would then fail with an error when it was unable to parse the string as JSON. Our new shell tests therefore also check that the clone operation is successful and that the Git LFS client fetched an object without an error. Finally, our shell tests verify that the client did not output any trace log messages which would indicate that it executed a custom transfer adapter with the name "adapter". Note that we could adjust our "lfstest-exploitcheck" utility so that it more fully simulated a custom transfer adapter by returning valid JSON messages and writing Git LFS object files. However, the effect on our shell tests would be that if the utility was invoked due to a regression in the client, then fewer secondary artifacts would serve as indications that the client had behaved improperly. For instance, the clone operation would succeed, and a Git LFS object file would be present in the cloned repository. With our current design, our tests can check for these conditions and expect that they will not occur unless the "lfstest-exploitcheck" utility was invoked. Note also that we perform two shell tests with each of the "lfs.<url>.access" and "remote.<name>.lfsurl" configuration options. For each type of option, the first test attempts to inject a custom transfer adapter configuration in which the adapter is used for individual object transfers but the client still makes initial HTTP requests to the Git LFS Batch API. In these tests, we use the "testcustom" adapter name in the configuration options, and we include the prefix "custom-transfer-" in the names of the test repositories. Our "lfstest-gitserver" test utility then recognizes that prefix and selects the "testcustom" adapter in its replies to Batch API requests from the client. Meanwhile, the second test for each of the two types of options attempts to inject a custom transfer adapter configuration in which the adapter is used in "standalone" mode. In this mode, the client defers to the adapter to handle object transfers on its own, without any requests to the Git LFS Batch API. For these tests, we use the "lfs-standalone-file" adapter name in the configuration options, which overrides the default standalone transfer adapter implemented in the Git LFS client. Co-authored-by: Aniket Gupta <[email protected]>
Since commit 9f4a8e2, the Git LFS client has read Git configuration data by splitting the output of "git config -l" commands on each line feed (LF) character and then splitting each line on the first equals sign ("=") character to determine an option's name and value. However, Git configuration option values may contain line feed characters, and so the output of a "git config -l" command is ambiguous unless the -z option is also specified. Without that option, a line feed character in an option's value is indistinguishable from the line feed character which separates one option from the next. As a consequence, the Git LFS client may interpret portions of an option's value as separate configuration options. This behaviour is of particular concern because some Git LFS configuration options specify local programs which the client should execute. In particular, custom transfer adapter programs may be configured with the "lfs.customTransfer.<name>.path" option, while pointer extension programs may be configured with the "lfs.extension.<name>.clean" and "lfs.extension.<name>.smudge" options. (We added support for custom transfer adapter configurations in commit 357703e of PR #1367. Prior to that, we added support for pointer extension filter configurations in several commits of PR #486, including commits ffd52e6 and 6a3a2c3.) For security reasons, we do not accept options which specify custom transfer adapter or pointer extension programs from the ".lfsconfig" files that may be supplied in a repository. However, a repository may supply a ".gitmodules" file containing Git submodule configuration options, and when a user runs the "git submodule init" command, Git copies some of these options into the repository's local Git configuration file. In particular, the "submodule.<name>.url" option will be copied into the local configuration file. If the URL provided in a "submodule.<name>.url" option contains a line feed character, then Git LFS will interpret any trailing portion of the URL as a separate option, and may thus be induced to run a potentially malicious executable. This vulnerability has been assigned the identifier CVE-2026-105802. To resolve this problem, we add the -z option to the "git config -l" commands run by the methods of the Configuration structure in our "git" package which read Git configuration settings. With the -z option, Git terminates each configuration entry with a null (NUL) character and separates its name from its value with a line feed character. We therefore revise the ParseConfigEntries() function in our "git" package so that it splits its input on NUL characters and then splits each entry on its first line feed character. To confirm that our changes are effective, we update the Go tests we expanded in a prior commit to use NUL-delimited configuration input and check the parsed entry structures. In our "git" package, the TestParseConfigEntries() function now checks that values containing line feed characters, including those followed by equals sign characters, are retained without being split into additional entries. In our "config" package, we add a test case to the TestReadGitConfigKeys() function to verify that a line feed character is preserved in an option's value in both unrestricted and restricted configuration sources. We then add a shell integration test which attempts to inject an "lfs.url" option in the value of an "lfs.fetchinclude" option in an ".lfsconfig" file. The test checks that the "git lfs env" command reports the endpoint from the actual "lfs.url" option rather than the injected one and does not report conflicting values for that option. We perform these checks with the ".lfsconfig" file in the working tree, then with the file present only in the index, and finally with the file available only from the HEAD commit, so that configuration options are parsed from both file and Git blob sources. Finally, we add two shell tests to our "t/t-submodule-lfsconfig.sh" script. These tests both attempt to inject a Git LFS configuration option into the submodule URL defined in a repository's ".gitmodules" file. The value of the injected option is a relative path to a local file (with an appropriate ".exe" file extension on Windows systems), and the repository includes a copy of our "lfstest-exploitcheck" test utility with that name. As we described in the prior commit in which we renamed the "lfstest-exploitcheck" utility from its previous name of "lfstest-badpathcheck", this program creates an empty sentinel file named "exploit" in the current working directory. Before exiting, it then writes the string "exploit" to its standard output and error file descriptors. The first test attempts to override the default standalone custom transfer adapter implemented in the Git LFS client, which is used when fetching objects from a local repository. After cloning the repository and running the "git submodule init" command, the test re-fetches a Git LFS object and confirms that no sentinel file has been created, as it would be without the change in this commit to the ParseConfigEntries() function. Absent that change, the Git LFS client would execute the utility to fetch objects from the source repository, and the utility would create a sentinel file that the test would detect. The test then verifies that the re-fetch operation succeeded and that the Git LFS client fetched an object without an error. The test also checks that the client did not output any trace log messages which would indicate that it executed a custom transfer adapter with the name "adapter", as that is the name we assign to the copy of the "lfstest-exploitcheck" utility in the repository. The second new test in the "t/t-submodule-lfsconfig.sh" script attempts to configure a pointer extension program which would be executed by the Git LFS "clean" filter. After running the "git submodule init" command, the test adds a Git LFS file to the index and confirms that no sentinel file has been created, as it would be without the change in this commit to the ParseConfigEntries() function. The test then verifies that the "git add" operation succeeded and that a Git LFS object was created without any error. The test also confirms that no pointer extension line entry appears in the Git LFS pointer file created for the object. As well, the test checks that the client did not output any trace log messages which would indicate that it executed a pointer extension program with the name "ext-clean", as that is the name we assign to the copy of the "lfstest-exploitcheck" utility in the repository. Finally, note that preserving line feed characters in configuration values may make the output of the "git lfs env" command more difficult to interpret, as that command does not quote the file paths and URLs it reports. However, any improvements to that command's output fall outside of the scope of the present work and so we defer such changes to a future PR. Users should not expect the output of the "git lfs env" command to have an unambiguous format suitable for consumption by other tools. Co-authored-by: Aniket Gupta <[email protected]>
This PR allow external processes to act as relays for uploads and downloads, allowing anyone to implement custom transfer techniques of their choosing without cluttering the core with N supported methods.
LFS talks to the process over stdin/stdout with a simple linewise JSON protocol, with content being exchanged by file path (process and lfs must be on the same machine). The process can then talk to the server however it likes.
The name of the custom adapter must be agreed between the client config and server, since the adapter to use will be negotiated at the API stage, and may change the information the API returns in the
actionssection. The content of theuploadanddownloadactions from the API are passed to the custom adapter so it can use that information as it sees fit.This PR also contains a reference implementation of a custom adapter process,
test/cmd/lfstest-customadapter.gowhich is used in the integration tests. It just uses the same HTTP protocol under the hood to keep things simple and to re-use thelfstest-gitserver, but demonstrates how the process comms work and how progress is reported etc. Other implementations can transfer data however they like.