Skip to content

Commit 742d357

Browse files
committed
Provide configuration to allow camo-media proxying
Fix #916 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 33ac0cc commit 742d357

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

custom/conf/app.example.ini

+4
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ PASSWORD_HASH_ALGO = argon2
514514
CSRF_COOKIE_HTTP_ONLY = true
515515
; Validate against https://haveibeenpwned.com/Passwords to see if a password has been exposed
516516
PASSWORD_CHECK_PWN = false
517+
; Use a camo image proxy - leave empty to not use
518+
CAMO_SERVER_URL =
519+
; HMAC to encode urls with
520+
CAMO_HMAC_KEY =
517521

518522
[openid]
519523
;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ set name for unique queues. Individual queues will default to
345345
- spec - use one or more special characters as ``!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~``
346346
- off - do not check password complexity
347347
- `PASSWORD_CHECK_PWN`: **false**: Check [HaveIBeenPwned](https://haveibeenpwned.com/Passwords) to see if a password has been exposed.
348+
- `CAMO_SERVER_URL`: **<empty>**: If you would like to use a camo proxy to proxy images from rendered content, set the camo server url here
349+
- `CAMO_HMAC_KEY`: **<empty>**: Provide the HMAC key for encoding urls
348350

349351
## OpenID (`openid`)
350352

modules/markup/camo.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package markup
6+
7+
import (
8+
"bytes"
9+
"crypto/hmac"
10+
"crypto/sha1"
11+
"encoding/base64"
12+
"strings"
13+
14+
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/modules/util"
16+
)
17+
18+
// CamoEncode encodes a lnk to fit with the go-camo and camo proxy links
19+
func CamoEncode(link []byte) []byte {
20+
if bytes.HasPrefix(link, []byte(setting.CamoServerURL)) || len(setting.CamoHMACKey) == 0 {
21+
return link
22+
}
23+
24+
hmacKey := []byte(setting.CamoHMACKey)
25+
mac := hmac.New(sha1.New, hmacKey)
26+
_, _ = mac.Write(link) // hmac does not return errors
27+
macSum := b64encode(mac.Sum(nil))
28+
encodedURL := b64encode(link)
29+
30+
return []byte(util.URLJoin(setting.CamoServerURL, macSum, encodedURL))
31+
}
32+
33+
func b64encode(data []byte) string {
34+
return strings.TrimRight(base64.URLEncoding.EncodeToString(data), "=")
35+
}

modules/markup/html.go

+9
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,17 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
377377

378378
lnk := string(link)
379379
lnk = util.URLJoin(prefix, lnk)
380+
380381
link = []byte(lnk)
381382
}
383+
if setting.CamoServerURL != "" {
384+
lnk := string(link)
385+
lnkURL, _ := url.Parse(lnk)
386+
if lnkURL.IsAbs() && !strings.HasPrefix(lnk, setting.AppURL) {
387+
// We should camo this url
388+
link = CamoEncode(link)
389+
}
390+
}
382391
node.Attr[idx].Val = string(link)
383392
}
384393
} else if node.Data == "a" {

modules/setting/setting.go

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ var (
147147
PasswordComplexity []string
148148
PasswordHashAlgo string
149149
PasswordCheckPwn bool
150+
CamoServerURL string
151+
CamoHMACKey string
150152

151153
// UI settings
152154
UI = struct {
@@ -746,6 +748,8 @@ func NewContext() {
746748
PasswordHashAlgo = sec.Key("PASSWORD_HASH_ALGO").MustString("argon2")
747749
CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true)
748750
PasswordCheckPwn = sec.Key("PASSWORD_CHECK_PWN").MustBool(false)
751+
CamoServerURL = sec.Key("CAMO_SERVER_URL").MustString("")
752+
CamoHMACKey = sec.Key("CAMO_HMAC_KEY").MustString("")
749753

750754
InternalToken = loadInternalToken(sec)
751755

0 commit comments

Comments
 (0)