forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
make sure limit can not be bypassed #13
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
6543
merged 5 commits into
a1012112796:zzc/dev/attach_size_limit
from
6543-forks:fix-for-attach_size_limit
Oct 7, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c90df6d
enhance test TestUploadAttachment
6543 a70dc88
introduce attachmentLimitedReader to make sure io stream dont bypass …
6543 7316593
Update services/attachment/reader.go
6543 9e16482
Update services/attachment/reader.go
6543 9c1e0b5
Update services/attachment/reader.go
6543 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright 2021 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package attachment | ||
|
|
||
| import "io" | ||
|
|
||
| // attachmentLimitedReader returns a Reader that reads from r | ||
| // but errors with ErrAttachmentSizeExceed after n bytes. | ||
| // The underlying implementation is a *attachmentReader. | ||
| func attachmentLimitedReader(r io.Reader, n int64) io.Reader { return &attachmentReader{r, n} } | ||
|
|
||
| // A attachmentReader reads from R but limits the amount of | ||
| // data returned to just N bytes. Each call to Read | ||
| // updates N to reflect the new amount remaining. | ||
| // Read returns EOF when N <= 0 or when the underlying R returns EOF. | ||
6543 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type attachmentReader struct { | ||
| R io.Reader // underlying reader | ||
| N int64 // max bytes remaining | ||
| } | ||
|
|
||
| func (l *attachmentReader) Read(p []byte) (n int, err error) { | ||
| if l.N <= 0 { | ||
| return 0, &ErrAttachmentSizeExceed{MaxSize: l.N} | ||
| } | ||
| if int64(len(p)) > l.N { | ||
| p = p[0:l.N] | ||
| } | ||
| n, err = l.R.Read(p) | ||
| l.N -= int64(n) | ||
| return | ||
| } | ||
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.