fs: don't panic when using a too-short object ID to push

Previously, we'd panic when trying to slice into the object ID if the
user provided a too-short object ID on the command line.  Instead, let's
fix this so we reject too-short object IDs with an error.
This commit is contained in:
brian m. carlson 2020-11-16 15:30:18 +00:00
parent f529c87d5e
commit 3f4662b489
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
2 changed files with 16 additions and 0 deletions

@ -65,6 +65,9 @@ func (f *Filesystem) ObjectExists(oid string, size int64) bool {
} }
func (f *Filesystem) ObjectPath(oid string) (string, error) { func (f *Filesystem) ObjectPath(oid string) (string, error) {
if len(oid) < 4 {
return "", fmt.Errorf("too short object ID: %q", oid)
}
dir := f.localObjectDir(oid) dir := f.localObjectDir(oid)
if err := tools.MkdirAll(dir, f); err != nil { if err := tools.MkdirAll(dir, f); err != nil {
return "", fmt.Errorf("error trying to create local storage directory in %q: %s", dir, err) return "", fmt.Errorf("error trying to create local storage directory in %q: %s", dir, err)

@ -820,3 +820,16 @@ begin_test "push custom reference"
assert_server_object "$reponame" "$oid" assert_server_object "$reponame" "$oid"
) )
end_test end_test
begin_test "push --object-id (invalid value)"
(
set -e
push_all_setup "everything"
git lfs push --object-id origin '' 2>&1 | tee push.log
git lfs push --object-id origin "${oid1:1:4}" 2>&1 | tee -a push.log
[ "$(grep -c 'too short object ID' push.log)" -eq 2 ]
)
end_test