Skip to content

Allow mime types to match based off of prefix #3617

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

Closed
wants to merge 1 commit into from

Conversation

mqudsi
Copy link
Contributor

@mqudsi mqudsi commented Mar 3, 2018

The old behavior prevented simple file types like "text/plain" from
being uploaded since browsers upload them with the charset as well (e.g.
text/plain charset=utf-8) without specifying all possible charsets.

Additionally, this allows for blanket includes like "text/" or "image/"
by class type.

There should be minimal risk introduced here as mime types are generally
hierarchical, but an alternative approach would be the equivalent of

if allowed.endsWith("*") && strings.HasPrefix(fileType,
    allowed.substr(0, allowed.length - 1) { ....

@mqudsi
Copy link
Contributor Author

mqudsi commented Mar 3, 2018

The preference here would be to evaluate the rules with pattern matching. Is there a globbing/pattern matching API gitea already uses elsewhere we could simply call to evaluate the match?

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 3, 2018
@thehowl
Copy link
Contributor

thehowl commented Mar 23, 2018

@thehowI is a copycat trying to impersonate me; don't mind them

@lunny
Copy link
Member

lunny commented Mar 23, 2018

How about

if t == "*/*" || t == fileType || strings.HasPrefix(t, fileType+" charset=")

@mqudsi
Copy link
Contributor Author

mqudsi commented Mar 23, 2018

@lunny that would work, if it's only to address the text charset issue. It wouldn't support adding a class like image/*, though.

@mqudsi
Copy link
Contributor Author

mqudsi commented Apr 16, 2018

The issue with the @thehowl impersonation nonsense derailed the question I was asking:

Is there a globbing/pattern matching API gitea already uses elsewhere we could simply call to evaluate the match?

I imagine you do use wildcards somewhere else in the code and that can just be plugged in here?

@codecov-io
Copy link

Codecov Report

Merging #3617 into master will increase coverage by 0.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3617      +/-   ##
==========================================
+ Coverage      23%   23.02%   +0.01%     
==========================================
  Files         126      126              
  Lines       24894    24894              
==========================================
+ Hits         5728     5731       +3     
+ Misses      18289    18287       -2     
+ Partials      877      876       -1
Impacted Files Coverage Δ
routers/repo/attachment.go 0% <0%> (ø) ⬆️
modules/process/manager.go 73.91% <0%> (+4.34%) ⬆️

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 9add96a...e95ba54. Read the comment docs.

@lunny lunny added the type/enhancement An improvement of existing functionality label Apr 17, 2018
@lunny lunny added this to the 1.x.x milestone Apr 17, 2018
@stale
Copy link

stale bot commented Jan 5, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 months. Thank you for your contributions.

@stale stale bot added the issue/stale label Jan 5, 2019
@mqudsi
Copy link
Contributor Author

mqudsi commented Jan 5, 2019

It is still pending feedback from maintainers.

@stale stale bot removed the issue/stale label Jan 5, 2019
@lafriks
Copy link
Member

lafriks commented Jan 5, 2019

I would propose use wildcard matching (golang has function for this)

@stale
Copy link

stale bot commented Mar 6, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 months. Thank you for your contributions.

@stale stale bot added the issue/stale label Mar 6, 2019
@lunny lunny added the issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented label Mar 11, 2019
@stale stale bot removed the issue/stale label Mar 11, 2019
@6543
Copy link
Member

6543 commented Dec 25, 2019

@mqudsi can you resolve confilct?

The old behavior prevented simple file types like `text/plain` from
being uploaded since browsers upload them with the charset as well (e.g.
`text/plain charset=utf-8`) without specifying all possible charsets.

Additionally, this allows for blanket includes like `text/*` or
`image/*` by class type.
@mqudsi mqudsi force-pushed the attachment_allow_prefix branch from e95ba54 to 3b9396c Compare February 15, 2020 00:10
@mqudsi
Copy link
Contributor Author

mqudsi commented Feb 15, 2020

I've redone the patch and changed the syntax from any prefix to a class whitelist (e.g. instead of allowing image it now allows image/*) to prevent any mime abuse.

@guillep2k
Copy link
Member

Related #10172

@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 Feb 15, 2020
@guillep2k
Copy link
Member

Be careful, as image/* will match image/svg+xml which can pose a security problem.

@mqudsi
Copy link
Contributor Author

mqudsi commented Mar 11, 2020

@lunny thoughts?

@lafriks
Copy link
Member

lafriks commented Mar 12, 2020

@guillep2k if svg is not displayed in browser it should not be threat

@lunny
Copy link
Member

lunny commented Mar 13, 2020

@mqudsi The codes looks good for me. But if you can extract them as a function and add some tests for it with some update notes on app.ini.sample and cheetsheet, it's better.

@lafriks
Copy link
Member

lafriks commented Mar 13, 2020

It would be better to use wildcard globs for this

@mqudsi
Copy link
Contributor Author

mqudsi commented Mar 14, 2020

I’m not a go developer but I’m not aware of a generic wildcard module in go. The only one I found the last time I looked is a file system glob.

That aside, I disagree entirely. You have to take context into account. What does general globbing get you that a simple string comparison (as performed) doesn’t? On the flip side, you get behavior that makes no sense for your context. What does it mean to have a glob pattern im*s or even */jpeg mean?

@lafriks
Copy link
Member

lafriks commented Mar 14, 2020

@mqudsi we are already using it in multiple places, for example see PR #7791

If blob case you could use for example such filter: image/{png|jpeg}

@mqudsi
Copy link
Contributor Author

mqudsi commented Mar 14, 2020

Ah, in the circles I run in, glob pretty much refers to * or ** only. I didn’t realize go’s implementation was more patterns than that. Still, that’s not much less verbose but considerably less readable than typing those two cases out twice, though if you’re already using it then it’s likely a paradigm advanced users will be familiar with.

@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 Mar 4, 2022
@singuliere
Copy link
Contributor

@mqudsi this looks good but needs rebasing.

@mqudsi
Copy link
Contributor Author

mqudsi commented Mar 4, 2022

@singuliere I think it's no longer required. The current code deletes ;.* from the detected mime type before matching against the allowed entry:

var mimeTypeSuffixRe = regexp.MustCompile(`;.*$`)
...
	fullMimeType := http.DetectContentType(buf)
	mimeType := strings.TrimSpace(mimeTypeSuffixRe.ReplaceAllString(fullMimeType, ""))
...
		} else if mimeType == allowEntry {
			return nil // mime type is allowed
		} ...

Class type matches (e.g. image/ or text/) are already explicitly supported with foo/* syntax.

@mqudsi mqudsi closed this Mar 4, 2022
@lunny lunny removed this from the 1.x.x milestone Mar 9, 2022
@go-gitea go-gitea locked and limited conversation to collaborators Apr 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants