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

fix: prevent annexed files to be edited with WebUI #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 10 additions & 6 deletions internal/route/repo/repo_gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,38 @@ func readDataciteFile(c *context.Context) {
// and io.Reader sent in through the caller so that any existing code can use
// the two variables without modifications.
// Any errors that occur during processing are stored in the provided context.
// The FileSize of the annexed content is also saved in the context (c.Data["FileSize"]).
// The FileSize of the annexed content is also saved in the context (c.Data["FileSize"]),
// along with flags indicating that the file is annexed (c.Data["IsAnnexedFile"]),
// and that the annexed content is not locally available (c.Data["IsAnnexedContentUnavailable"]).
func resolveAnnexedContent(c *context.Context, buf []byte) ([]byte, error) {
if !tool.IsAnnexedFile(buf) {
// not an annex pointer file; return as is
return buf, nil
}
c.Data["IsAnnexedFile"] = true
log.Trace("Annexed file requested: Resolving content for %q", bytes.TrimSpace(buf))

keyparts := strings.Split(strings.TrimSpace(string(buf)), "/")
key := keyparts[len(keyparts)-1]
contentPath, err := git.NewCommand("annex", "contentlocation", key).RunInDir(c.Repo.Repository.RepoPath())
if err != nil {
log.Error(2, "Failed to find content location for key %q", key)
c.Data["IsAnnexedFile"] = true
return buf, err
log.Warn("Failed to find content location for key %q", key)
//key's content is not present in the local repository, meaning that annexed content hasn't been pushed yet
c.Data["IsAnnexedContentUnavailable"] = true
return buf, nil
}
// always trim space from output for git command
contentPath = bytes.TrimSpace(contentPath)
afp, err := os.Open(filepath.Join(c.Repo.Repository.RepoPath(), string(contentPath)))
if err != nil {
log.Trace("Could not open annex file: %v", err)
c.Data["IsAnnexedFile"] = true
c.Data["IsAnnexedContentUnavailable"] = true
return buf, err
}
info, err := afp.Stat()
if err != nil {
log.Trace("Could not stat annex file: %v", err)
c.Data["IsAnnexedFile"] = true
c.Data["IsAnnexedContentUnavailable"] = true
return buf, err
}
annexDataReader := bufio.NewReader(afp)
Expand Down
6 changes: 3 additions & 3 deletions internal/route/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
if err != nil {
return
}
isannex := c.Data["IsAnnexedFile"] == true
isTextFile := tool.IsTextFile(p)
c.Data["IsTextFile"] = isTextFile

Expand Down Expand Up @@ -232,7 +233,6 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
c.Data["LineNums"] = gotemplate.HTML(output.String())
}

isannex := tool.IsAnnexedFile(p)
if canEnableEditor && !isannex {
c.Data["CanEditFile"] = true
c.Data["EditFileTooltip"] = c.Tr("repo.editor.edit_this_file")
Expand All @@ -251,9 +251,9 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
case tool.IsImageFile(p) && (c.Data["FileSize"].(int64) < conf.Repository.RawCaptchaMinFileSize*annex.MEGABYTE ||
c.IsLogged):
c.Data["IsImageFile"] = true
case tool.IsAnnexedFile(p) && (c.Data["FileSize"].(int64) < conf.Repository.RawCaptchaMinFileSize*annex.MEGABYTE ||
case isannex && (c.Data["FileSize"].(int64) < conf.Repository.RawCaptchaMinFileSize*annex.MEGABYTE ||
c.IsLogged):
c.Data["IsAnnexedFile"] = true
//
}

if canEnableEditor {
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
{{if .IsImageFile}}
<img src="{{EscapePound $.RawFileLink}}">

{{else if .IsAnnexedFile}}
{{else if .IsAnnexedContentUnavailable}}
<div class="ui yellow segment">
<strong>File content is not available</strong>
<p>
Expand Down