Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LFS object verification step after upload #2868

Merged
merged 6 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion modules/lfs/content_store.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package lfs

import (
"code.gitea.io/gitea/models"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"os"
"path/filepath"

"code.gitea.io/gitea/models"
)

var (
Expand Down Expand Up @@ -82,6 +83,15 @@ func (s *ContentStore) Exists(meta *models.LFSMetaObject) bool {
return true
}

// Verify returns true if the object exists in the content store and size is correct.
func (s *ContentStore) Verify(meta *models.LFSMetaObject) bool {
path := filepath.Join(s.BasePath, transformKey(meta.Oid))
if fi, err := os.Stat(path); os.IsNotExist(err) || err == nil && fi.Size() != meta.Size {
return false
}
return true
}

func transformKey(key string) string {
if len(key) < 5 {
return key
Expand Down
43 changes: 42 additions & 1 deletion modules/lfs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"path"
"regexp"
"strconv"
"strings"
Expand All @@ -15,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"github.com/dgrijalva/jwt-go"
"gopkg.in/macaron.v1"
)
Expand Down Expand Up @@ -66,7 +68,12 @@ type ObjectError struct {

// ObjectLink builds a URL linking to the object.
func (v *RequestVars) ObjectLink() string {
return fmt.Sprintf("%s%s/%s/info/lfs/objects/%s", setting.AppURL, v.User, v.Repo, v.Oid)
return setting.AppURL + path.Join(v.User, v.Repo, "info/lfs/objects", v.Oid)
}

// VerifyLink builds a URL for verifying the object.
func (v *RequestVars) VerifyLink() string {
return setting.AppURL + path.Join(v.User, v.Repo, "info/lfs/verify")
}

// link provides a structure used to build a hypermedia representation of an HTTP link.
Expand Down Expand Up @@ -320,6 +327,35 @@ func PutHandler(ctx *context.Context) {
logRequest(ctx.Req, 200)
}

// VerifyHandler verify oid and its size from the content store
func VerifyHandler(ctx *context.Context) {

Copy link
Member

Choose a reason for hiding this comment

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

please remove blank line.

if !setting.LFS.StartServer {
writeStatus(ctx, 404)
return
}

if !ContentMatcher(ctx.Req) {
writeStatus(ctx, 400)
return
}

rv := unpack(ctx)

meta, _ := getAuthenticatedRepoAndMeta(ctx, rv, true)
if meta == nil {
return
}

contentStore := &ContentStore{BasePath: setting.LFS.ContentPath}
if !contentStore.Verify(meta) {
writeStatus(ctx, 404)
return
}

logRequest(ctx.Req, 200)
}

// Represent takes a RequestVars and Meta and turns it into a Representation suitable
// for json encoding
func Represent(rv *RequestVars, meta *models.LFSMetaObject, download, upload bool) *Representation {
Expand Down Expand Up @@ -347,6 +383,11 @@ func Represent(rv *RequestVars, meta *models.LFSMetaObject, download, upload boo
rep.Actions["upload"] = &link{Href: rv.ObjectLink(), Header: header}
}

if upload && !download {
// Force client side verify action while gitea lacks proper server side verification
rep.Actions["verify"] = &link{Href: rv.VerifyLink(), Header: header}
}

return rep
}

Expand Down
1 change: 1 addition & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/objects/:oid/:filename", lfs.ObjectOidHandler)
m.Any("/objects/:oid", lfs.ObjectOidHandler)
m.Post("/objects", lfs.PostHandler)
m.Post("/verify", lfs.VerifyHandler)
m.Any("/*", func(ctx *context.Context) {
ctx.Handle(404, "", nil)
})
Expand Down