aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorAntoine GIRARD <sapk@users.noreply.github.com>2020-01-05 00:20:08 +0100
committerLauris BH <lauris@nix.lv>2020-01-05 01:20:08 +0200
commit8b2407371365fc123fc368bfd46b15f55ba8ae6a (patch)
tree8f112acce97c863846a88a6b37e3b570062860d2 /routers
parent6a5a2f493a2b8d19a9f6196bd208a3b8a14e9c1c (diff)
downloadgitea-8b2407371365fc123fc368bfd46b15f55ba8ae6a.tar.gz
gitea-8b2407371365fc123fc368bfd46b15f55ba8ae6a.zip
Only serve attachments when linked to issue/release and if accessible by user (#9340)
* test: add current attachement responses * refactor: check if attachement is linked and accessible by user * chore: clean TODO * fix: typo attachement -> attachment * revert un-needed go.sum change * refactor: move models logic to models * fix TestCreateIssueAttachment which was wrongly successful * fix unit tests with unittype added * fix unit tests with changes * use a valid uuid format for pgsql int. test * test: add unit test TestLinkedRepository * refactor: allow uploader to access unlinked attachement * add missing blank line * refactor: move to a separate function repo.GetAttachment * typo * test: remove err test return * refactor: use repo perm for access checking generally + 404 for all reject
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/attachment.go56
-rw-r--r--routers/routes/routes.go30
-rw-r--r--routers/user/home_test.go4
3 files changed, 59 insertions, 31 deletions
diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go
index 0d496230e1..96dc28a23a 100644
--- a/routers/repo/attachment.go
+++ b/routers/repo/attachment.go
@@ -6,6 +6,8 @@ package repo
import (
"fmt"
+ "net/http"
+ "os"
"strings"
"code.gitea.io/gitea/models"
@@ -85,3 +87,57 @@ func DeleteAttachment(ctx *context.Context) {
"uuid": attach.UUID,
})
}
+
+// GetAttachment serve attachements
+func GetAttachment(ctx *context.Context) {
+ attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
+ if err != nil {
+ if models.IsErrAttachmentNotExist(err) {
+ ctx.Error(404)
+ } else {
+ ctx.ServerError("GetAttachmentByUUID", err)
+ }
+ return
+ }
+
+ repository, unitType, err := attach.LinkedRepository()
+ if err != nil {
+ ctx.ServerError("LinkedRepository", err)
+ return
+ }
+
+ if repository == nil { //If not linked
+ if !(ctx.IsSigned && attach.UploaderID == ctx.User.ID) { //We block if not the uploader
+ ctx.Error(http.StatusNotFound)
+ return
+ }
+ } else { //If we have the repository we check access
+ perm, err := models.GetUserRepoPermission(repository, ctx.User)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err.Error())
+ return
+ }
+ if !perm.CanRead(unitType) {
+ ctx.Error(http.StatusNotFound)
+ return
+ }
+ }
+
+ //If we have matched and access to release or issue
+ fr, err := os.Open(attach.LocalPath())
+ if err != nil {
+ ctx.ServerError("Open", err)
+ return
+ }
+ defer fr.Close()
+
+ if err := attach.IncreaseDownloadCount(); err != nil {
+ ctx.ServerError("Update", err)
+ return
+ }
+
+ if err = ServeData(ctx, attach.Name, fr); err != nil {
+ ctx.ServerError("ServeData", err)
+ return
+ }
+}
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index c8351f312b..888c92ac4a 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -8,7 +8,6 @@ import (
"bytes"
"encoding/gob"
"net/http"
- "os"
"path"
"text/template"
"time"
@@ -474,34 +473,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/following", user.Following)
})
- m.Get("/attachments/:uuid", func(ctx *context.Context) {
- attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
- if err != nil {
- if models.IsErrAttachmentNotExist(err) {
- ctx.Error(404)
- } else {
- ctx.ServerError("GetAttachmentByUUID", err)
- }
- return
- }
-
- fr, err := os.Open(attach.LocalPath())
- if err != nil {
- ctx.ServerError("Open", err)
- return
- }
- defer fr.Close()
-
- if err := attach.IncreaseDownloadCount(); err != nil {
- ctx.ServerError("Update", err)
- return
- }
-
- if err = repo.ServeData(ctx, attach.Name, fr); err != nil {
- ctx.ServerError("ServeData", err)
- return
- }
- })
+ m.Get("/attachments/:uuid", repo.GetAttachment)
}, ignSignIn)
m.Group("/attachments", func() {
diff --git a/routers/user/home_test.go b/routers/user/home_test.go
index e5bbd0e98e..39186d93ee 100644
--- a/routers/user/home_test.go
+++ b/routers/user/home_test.go
@@ -26,10 +26,10 @@ func TestIssues(t *testing.T) {
Issues(ctx)
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
- assert.EqualValues(t, map[int64]int64{1: 1}, ctx.Data["Counts"])
+ assert.EqualValues(t, map[int64]int64{1: 1, 2: 1}, ctx.Data["Counts"])
assert.EqualValues(t, true, ctx.Data["IsShowClosed"])
assert.Len(t, ctx.Data["Issues"], 1)
- assert.Len(t, ctx.Data["Repos"], 1)
+ assert.Len(t, ctx.Data["Repos"], 2)
}
func TestMilestones(t *testing.T) {