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

Attachments: Add extension support, allow all types for releases #12465

Merged
merged 4 commits into from
Oct 5, 2020

Conversation

silverwind
Copy link
Member

@silverwind silverwind commented Aug 10, 2020

  • Add support for file extensions, matching the accept attribute of <input type="file">
  • Add support for type wildcard mime types, e.g. image/*
  • Create repository.release.ALLOWED_TYPES setting (default unrestricted)
  • Change default for attachment.ALLOWED_TYPES to a list of extensions
  • Split out POST /attachments into two endpoints for issue/pr and releases to prevent circumvention of allowed types check

Fixes: #10172
Fixes: #7266
Fixes: #12460
Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers

@silverwind silverwind force-pushed the attachment-defaults branch 4 times, most recently from cf84720 to d251979 Compare August 10, 2020 18:46
@silverwind silverwind force-pushed the attachment-defaults branch 5 times, most recently from 88fd59e to 81803d6 Compare August 10, 2020 19:13
@silverwind
Copy link
Member Author

Will do a few tweaks soon like using path over filepath because browsers generally only send Unix-style paths. I guess this may not fully resolve #8229 but at least users will not hit those mime-type problems any more in the default configuration.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Aug 11, 2020
@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2020

Codecov Report

Merging #12465 into master will increase coverage by 0.03%.
The diff coverage is 72.97%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #12465      +/-   ##
==========================================
+ Coverage   43.80%   43.83%   +0.03%     
==========================================
  Files         630      631       +1     
  Lines       69821    69892      +71     
==========================================
+ Hits        30585    30640      +55     
- Misses      34300    34305       +5     
- Partials     4936     4947      +11     
Impacted Files Coverage Δ
modules/setting/repository.go 57.69% <ø> (ø)
modules/setting/setting.go 46.44% <ø> (ø)
routers/api/v1/repo/release_attachment.go 0.00% <0.00%> (ø)
routers/repo/editor.go 25.04% <0.00%> (+0.04%) ⬆️
routers/repo/attachment.go 49.43% <50.00%> (-0.57%) ⬇️
modules/upload/filetype.go 82.14% <95.83%> (+17.85%) ⬆️
routers/repo/release.go 26.52% <100.00%> (+0.32%) ⬆️
modules/indexer/stats/queue.go 52.94% <0.00%> (-11.77%) ⬇️
modules/util/sanitize.go 37.50% <0.00%> (-3.68%) ⬇️
modules/lfs/content_store.go 48.33% <0.00%> (-1.67%) ⬇️
... and 51 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update faa676c...503bebb. Read the comment docs.

@zeripath zeripath added pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! type/feature Completely new functionality. Can only be merged if feature freeze is not active. labels Aug 11, 2020
@zeripath
Copy link
Contributor

I think we have to mark this as breaking because server managers will have to do something to prevent uploads

Copy link
Contributor

@zeripath zeripath left a comment

Choose a reason for hiding this comment

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

I think although this is breaking this is probably still the correct thing to do.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Aug 11, 2020
@silverwind
Copy link
Member Author

The new default can't really "break" any existing uploads but it's certainly worth mentioning it in the release notes that we now allow all files to be uploaded by default.

@lunny
Copy link
Member

lunny commented Aug 12, 2020

I agree with @zeripath, For a security consideration, it's a break change.

@CirnoT
Copy link
Contributor

CirnoT commented Aug 13, 2020

While this is desired for content uploaded as releases, it is definitely not expected behavior when uploading content embedded in a comment.

We should distinguish them and keep comment one to sane text files + images while changing release one to empty by default. Improvements from this PR should of course be kept for both.

@silverwind
Copy link
Member Author

silverwind commented Aug 13, 2020

it is definitely not expected behavior when uploading content embedded in a comment

What exactly is the issue with allowing all files? Security concerns? A user can already upload malicious files by just renaming their extension to .png and execution of that could be achieved through other means if such vulnerabilities exist.

These mime/extension filters does not really help much if any to prevent such attacks and golang's magic byte-based (I assume its based on that) detection can be fooled too. A much better alternative would be to mount the directory for uploads using noexec mount flag and restricting the gitea process through capabilities as much as possible.

sane text files + images

How would you match them using the new filter options? text/* may not be supported in all browsers as the spec only specifies audio,video,image wildcard (but we do support them all on the backend).

@CirnoT
Copy link
Contributor

CirnoT commented Aug 14, 2020

It would not be perfect of course, nothing ever will.

How would you match them using the new filter options?

Same as GitHub? .gif,.jpeg,.jpg,.png,.docx,.gz,.log,.pdf,.pptx,.txt,.xlsx,.zip

What exactly is the issue with allowing all files? Security concerns?

I suppose the main point would be that no .exe files and such are directly downloadable when uploaded by any user, thus helping site not being marked as hosting malicious content or using it directly to serve payloads. Of course malicious user can still create repository but such cases are easier to track and remove than random attachment on some comment.

@silverwind
Copy link
Member Author

silverwind commented Aug 14, 2020

Seems resonable to change the default to the issue attachment filter to the extensions listed here.

@silverwind
Copy link
Member Author

silverwind commented Aug 15, 2020

Default value .docx,.gif,.gz,.jpeg,.jpg,.log,.pdf,.png,.pptx,.txt,.xlsx,.zip added for issue/pr attachments and server-sent error string added to i18n.

@silverwind
Copy link
Member Author

@zeripath: maybe you want to re-review. pr has substantially changed since your review.

@CirnoT
Copy link
Contributor

CirnoT commented Aug 30, 2020

Linter failure

@silverwind
Copy link
Member Author

Lint fixed.

@lafriks lafriks added this to the 1.13.0 milestone Aug 31, 2020
modules/upload/upload.go Outdated Show resolved Hide resolved
@silverwind
Copy link
Member Author

Encryption removed, now using separate endpoints for issue and release attachment POSTs.

Known issue: Can not remove files from a release when editing it. Issue seems already present on master.

@silverwind silverwind force-pushed the attachment-defaults branch 2 times, most recently from ea2317e to b9f96dc Compare August 31, 2020 18:56
@silverwind
Copy link
Member Author

Rebased, squashed and update OP. I'm not sure yet how hard the fix for the release edits will be, it'll likely requre quite a bit of fiddling with the quirky dropzone code.

routers/routes/routes.go Show resolved Hide resolved
@lafriks
Copy link
Member

lafriks commented Sep 27, 2020

@silverwind ping

@silverwind
Copy link
Member Author

Will have another look shortly. I opened #12963 for issue with editing so to not distract this PR further.

- Add support for file extensions, matching the `accept` attribute of `<input type="file">`
- Add support for type wildcard mime types, e.g. `image/*`
- Create repository.release.ALLOWED_TYPES setting (default unrestricted)
- Change default for attachment.ALLOWED_TYPES to a list of extensions
- Split out POST /attachments into two endpoints for issue/pr and
  releases to prevent circumvention of allowed types check

Fixes: go-gitea#10172
Fixes: go-gitea#7266
Fixes: go-gitea#12460
Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers
silverwind referenced this pull request Oct 1, 2020
* Add a storage layer for attachments

* Fix some bug

* fix test

* Fix copyright head and lint

* Fix bug

* Add setting for minio and flags for migrate-storage

* Add documents

* fix lint

* Add test for minio store type on attachments

* fix test

* fix test

* Apply suggestions from code review

Co-authored-by: guillep2k <[email protected]>

* Add warning when storage migrated successfully

* Fix drone

* fix test

* rebase

* Fix test

* display the error on console

* Move minio test to amd64 since minio docker don't support arm64

* refactor the codes

* add trace

* Fix test

* remove log on xorm

* Fi download bug

* Add a storage layer for attachments

* Add setting for minio and flags for migrate-storage

* fix lint

* Add test for minio store type on attachments

* Apply suggestions from code review

Co-authored-by: guillep2k <[email protected]>

* Fix drone

* fix test

* Fix test

* display the error on console

* Move minio test to amd64 since minio docker don't support arm64

* refactor the codes

* add trace

* Fix test

* Add URL function to serve attachments directly from S3/Minio

* Add ability to enable/disable redirection in attachment configuration

* Fix typo

* Add a storage layer for attachments

* Add setting for minio and flags for migrate-storage

* fix lint

* Add test for minio store type on attachments

* Apply suggestions from code review

Co-authored-by: guillep2k <[email protected]>

* Fix drone

* fix test

* Fix test

* display the error on console

* Move minio test to amd64 since minio docker don't support arm64

* don't change unrelated files

* Fix lint

* Fix build

* update go.mod and go.sum

* Use github.com/minio/minio-go/v6

* Remove unused function

* Upgrade minio to v7 and some other improvements

* fix lint

* Fix go mod

Co-authored-by: guillep2k <[email protected]>
Co-authored-by: Tyler <[email protected]>
@silverwind
Copy link
Member Author

Ready for review again.

@lunny
Copy link
Member

lunny commented Oct 5, 2020

@lafriks Need your review again.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 5, 2020
@techknowlogick techknowlogick merged commit cda4475 into go-gitea:master Oct 5, 2020
@silverwind silverwind deleted the attachment-defaults branch October 5, 2020 06:37
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot upload jar file
8 participants