-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Only serve attachments when linked to issue/release and if accessible by user #9340
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
Merged
Merged
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
6b15314
test: add current attachement responses
sapk 595f5bb
refactor: check if attachement is linked and accessible by user
sapk cd494ee
chore: clean TODO
sapk a87f112
fix: typo attachement -> attachment
sapk 3a90576
revert un-needed go.sum change
sapk 67551c4
refactor: move models logic to models
sapk 4740e0c
fix TestCreateIssueAttachment which was wrongly successful
sapk fbddeac
fix unit tests with unittype added
sapk 7ebe2c3
fix unit tests with changes
sapk b488f1d
use a valid uuid format for pgsql int. test
sapk 0e3f0f1
Merge branch 'master' into not-found-not-linked
sapk ee21be2
Merge branch 'master' into not-found-not-linked
sapk 5db84c2
Merge branch 'master' into not-found-not-linked
sapk 3790b46
Merge branch 'master' into not-found-not-linked
sapk 93c305b
Merge branch 'master' into not-found-not-linked
sapk ce4993a
test: add unit test TestLinkedRepository
sapk 49a18a2
refactor: allow uploader to access unlinked attachement
sapk 5292d3f
Merge branch 'master' into not-found-not-linked
sapk 26df92c
Merge branch 'master' into not-found-not-linked
sapk 48006bb
Merge branch 'master' into not-found-not-linked
sapk 24476b2
Merge branch 'master' into not-found-not-linked
sapk 71c5cfd
Merge branch 'master' into not-found-not-linked
sapk 06162e3
add missing blank line
sapk 19b9eb9
refactor: move to a separate function repo.GetAttachment
sapk 219a4d0
typo
sapk 9664c9f
Merge branch 'master' into not-found-not-linked
sapk f6f30d1
Merge branch 'master' into not-found-not-linked
sapk dc68569
test: remove err test return
sapk 149d2f6
refactor: use repo perm for access checking generally + 404 for all r…
sapk 63b8479
Merge branch 'master' into not-found-not-linked
sapk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
"image/png" | ||
"io" | ||
"io/ioutil" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/test" | ||
|
||
"github.com/stretchr/testify/assert" | ||
sapk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
func generateImg() bytes.Buffer { | ||
// Generate image | ||
myImage := image.NewRGBA(image.Rect(0, 0, 32, 32)) | ||
var buff bytes.Buffer | ||
png.Encode(&buff, myImage) | ||
return buff | ||
} | ||
|
||
func createAttachment(t *testing.T, session *TestSession, repoURL, filename string, buff bytes.Buffer, expectedStatus int) string { | ||
body := &bytes.Buffer{} | ||
|
||
//Setup multi-part | ||
writer := multipart.NewWriter(body) | ||
part, err := writer.CreateFormFile("file", filename) | ||
assert.NoError(t, err) | ||
_, err = io.Copy(part, &buff) | ||
assert.NoError(t, err) | ||
err = writer.Close() | ||
assert.NoError(t, err) | ||
|
||
csrf := GetCSRF(t, session, repoURL) | ||
|
||
req := NewRequestWithBody(t, "POST", "/attachments", body) | ||
req.Header.Add("X-Csrf-Token", csrf) | ||
req.Header.Add("Content-Type", writer.FormDataContentType()) | ||
resp := session.MakeRequest(t, req, expectedStatus) | ||
|
||
if expectedStatus != http.StatusOK { | ||
return "" | ||
} | ||
var obj map[string]string | ||
DecodeJSON(t, resp, &obj) | ||
return obj["uuid"] | ||
} | ||
|
||
func TestCreateAnonymousAttachment(t *testing.T) { | ||
prepareTestEnv(t) | ||
session := emptyTestSession(t) | ||
createAttachment(t, session, "user2/repo1", "image.png", generateImg(), http.StatusFound) | ||
} | ||
|
||
func TestCreateIssueAttachment(t *testing.T) { | ||
prepareTestEnv(t) | ||
const repoURL = "user2/repo1" | ||
session := loginUser(t, "user2") | ||
uuid := createAttachment(t, session, repoURL, "image.png", generateImg(), http.StatusOK) | ||
|
||
req := NewRequest(t, "GET", repoURL+"/issues/new") | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
htmlDoc := NewHTMLParser(t, resp.Body) | ||
|
||
link, exists := htmlDoc.doc.Find("form").Attr("action") | ||
assert.True(t, exists, "The template has changed") | ||
|
||
postData := map[string]string{ | ||
"_csrf": htmlDoc.GetCSRF(), | ||
"title": "New Issue With Attachment", | ||
"content": "some content", | ||
"files": uuid, | ||
} | ||
|
||
req = NewRequestWithValues(t, "POST", link, postData) | ||
resp = session.MakeRequest(t, req, http.StatusFound) | ||
test.RedirectURL(resp) // check that redirect URL exists | ||
|
||
//Validate that attachment is available | ||
req = NewRequest(t, "GET", "/attachments/"+uuid) | ||
session.MakeRequest(t, req, http.StatusOK) | ||
} | ||
|
||
func TestGetAttachment(t *testing.T) { | ||
prepareTestEnv(t) | ||
adminSession := loginUser(t, "user1") | ||
user2Session := loginUser(t, "user2") | ||
user8Session := loginUser(t, "user8") | ||
emptySession := emptyTestSession(t) | ||
testCases := []struct { | ||
name string | ||
uuid string | ||
createFile bool | ||
session *TestSession | ||
want int | ||
}{ | ||
{"LinkedIssueUUID", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", true, user2Session, http.StatusOK}, | ||
{"LinkedCommentUUID", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", true, user2Session, http.StatusOK}, | ||
{"linked_release_uuid", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a19", true, user2Session, http.StatusOK}, | ||
{"NotExistingUUID", "b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18", false, user2Session, http.StatusNotFound}, | ||
{"FileMissing", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18", false, user2Session, http.StatusInternalServerError}, | ||
{"NotLinked", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20", true, user2Session, http.StatusNotFound}, | ||
{"NotLinkedAccessibleByUploader", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a20", true, user8Session, http.StatusOK}, | ||
{"PublicByNonLogged", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", true, emptySession, http.StatusOK}, | ||
{"PrivateByNonLogged", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, emptySession, http.StatusNotFound}, | ||
{"PrivateAccessibleByAdmin", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, adminSession, http.StatusOK}, | ||
{"PrivateAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, user2Session, http.StatusOK}, | ||
{"NotAccessibleByUser", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12", true, user8Session, http.StatusForbidden}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
//Write empty file to be available for response | ||
if tc.createFile { | ||
localPath := models.AttachmentLocalPath(tc.uuid) | ||
err := os.MkdirAll(path.Dir(localPath), os.ModePerm) | ||
assert.NoError(t, err) | ||
err = ioutil.WriteFile(localPath, []byte("hello world"), 0644) | ||
assert.NoError(t, err) | ||
} | ||
//Actual test | ||
req := NewRequest(t, "GET", "/attachments/"+tc.uuid) | ||
tc.session.MakeRequest(t, req, tc.want) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.