-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Provide configuration to allow camo-media proxying #12802
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
742d357
Provide configuration to allow camo-media proxying
zeripath 2d4f88c
Merge branch 'master' into fix-916-go-camo-media-proxy
6543 e62e675
fix
6543 f9d6790
address review and add option
6543 19bd41e
compare setting once
6543 664b16d
be able to unit test
6543 3285a32
Merge branch 'master' into fix-916-go-camo-media-proxy
6543 7cd58ec
own settings section
6543 dc6d0bd
add content type comments
6543 27567fa
Merge branch 'main' into fix-916-go-camo-media-proxy
6543 40cd0d0
Add comments for camo-proxy to describe the purposes.
wxiaoguang ca8fa02
Merge branch 'main' into fix-916-go-camo-media-proxy
wxiaoguang 6a68631
fix unit test
wxiaoguang 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,45 @@ | ||
// Copyright 2022 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 markup | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/base64" | ||
"net/url" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
// CamoEncode encodes a lnk to fit with the go-camo and camo proxy links | ||
func CamoEncode(link string) string { | ||
if strings.HasPrefix(link, setting.Camo.ServerURL) { | ||
return link | ||
} | ||
|
||
mac := hmac.New(sha1.New, []byte(setting.Camo.HMACKey)) | ||
_, _ = mac.Write([]byte(link)) // hmac does not return errors | ||
macSum := b64encode(mac.Sum(nil)) | ||
encodedURL := b64encode([]byte(link)) | ||
|
||
return util.URLJoin(setting.Camo.ServerURL, macSum, encodedURL) | ||
} | ||
|
||
func b64encode(data []byte) string { | ||
return strings.TrimRight(base64.URLEncoding.EncodeToString(data), "=") | ||
} | ||
|
||
func camoHandleLink(link string) string { | ||
if setting.Camo.Enabled { | ||
lnkURL, err := url.Parse(link) | ||
if err == nil && lnkURL.IsAbs() && !strings.HasPrefix(link, setting.AppURL) && | ||
(setting.Camo.Allways || lnkURL.Scheme != "https") { | ||
return CamoEncode(link) | ||
} | ||
} | ||
return link | ||
} |
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,45 @@ | ||
// Copyright 2022 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 markup | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCamoHandleLink(t *testing.T) { | ||
setting.AppURL = "https://gitea.com" | ||
// Test media proxy | ||
setting.CamoEnabled = true | ||
setting.CamoServerURL = "https://image.proxy" | ||
setting.CamoHMACKey = "geheim" | ||
|
||
assert.Equal(t, | ||
"https://gitea.com/img.jpg", | ||
camoHandleLink("https://gitea.com/img.jpg")) | ||
assert.Equal(t, | ||
"https://testimages.org/img.jpg", | ||
camoHandleLink("https://testimages.org/img.jpg")) | ||
assert.Equal(t, | ||
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc", | ||
camoHandleLink("http://testimages.org/img.jpg")) | ||
|
||
setting.CamoAllways = true | ||
assert.Equal(t, | ||
"https://gitea.com/img.jpg", | ||
camoHandleLink("https://gitea.com/img.jpg")) | ||
assert.Equal(t, | ||
"https://image.proxy/tkdlvmqpbIr7SjONfHNgEU622y0/aHR0cHM6Ly90ZXN0aW1hZ2VzLm9yZy9pbWcuanBn", | ||
camoHandleLink("https://testimages.org/img.jpg")) | ||
assert.Equal(t, | ||
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc", | ||
camoHandleLink("http://testimages.org/img.jpg")) | ||
|
||
// Restore previous settings | ||
setting.CamoEnabled = false | ||
} |
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
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.