Skip to content

Commit 664b16d

Browse files
committed
be able to unit test
1 parent 19bd41e commit 664b16d

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

modules/markup/camo.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/hmac"
99
"crypto/sha1"
1010
"encoding/base64"
11+
"net/url"
1112
"strings"
1213

1314
"code.gitea.io/gitea/modules/setting"
@@ -31,3 +32,14 @@ func CamoEncode(link string) string {
3132
func b64encode(data []byte) string {
3233
return strings.TrimRight(base64.URLEncoding.EncodeToString(data), "=")
3334
}
35+
36+
func camoHandleLink(link string) string {
37+
if setting.CamoEnabled {
38+
lnkURL, err := url.Parse(link)
39+
if err == nil && lnkURL.IsAbs() && !strings.HasPrefix(link, setting.AppURL) &&
40+
(setting.CamoAllways || lnkURL.Scheme != "https") {
41+
return CamoEncode(link)
42+
}
43+
}
44+
return link
45+
}

modules/markup/camo_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2022 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+
"testing"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCamoHandleLink(t *testing.T) {
15+
setting.AppURL = "https://gitea.com"
16+
// Test media proxy
17+
setting.CamoEnabled = true
18+
setting.CamoServerURL = "https://image.proxy"
19+
setting.CamoHMACKey = "geheim"
20+
21+
assert.Equal(t,
22+
"https://gitea.com/img.jpg",
23+
camoHandleLink("https://gitea.com/img.jpg"))
24+
assert.Equal(t,
25+
"https://testimages.org/img.jpg",
26+
camoHandleLink("https://testimages.org/img.jpg"))
27+
assert.Equal(t,
28+
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc",
29+
camoHandleLink("http://testimages.org/img.jpg"))
30+
31+
setting.CamoAllways = true
32+
assert.Equal(t,
33+
"https://gitea.com/img.jpg",
34+
camoHandleLink("https://gitea.com/img.jpg"))
35+
assert.Equal(t,
36+
"https://image.proxy/tkdlvmqpbIr7SjONfHNgEU622y0/aHR0cHM6Ly90ZXN0aW1hZ2VzLm9yZy9pbWcuanBn",
37+
camoHandleLink("https://testimages.org/img.jpg"))
38+
assert.Equal(t,
39+
"https://image.proxy/eivin43gJwGVIjR9MiYYtFIk0mw/aHR0cDovL3Rlc3RpbWFnZXMub3JnL2ltZy5qcGc",
40+
camoHandleLink("http://testimages.org/img.jpg"))
41+
42+
// Restore previous settings
43+
setting.CamoEnabled = false
44+
}

modules/markup/html.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,7 @@ func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node
386386

387387
attr.Val = util.URLJoin(prefix, attr.Val)
388388
}
389-
if setting.CamoEnabled {
390-
lnkURL, err := url.Parse(attr.Val)
391-
if err != nil && lnkURL.IsAbs() && !strings.HasPrefix(attr.Val, setting.AppURL) &&
392-
(setting.CamoAllways || lnkURL.Scheme != "https") {
393-
attr.Val = CamoEncode(attr.Val)
394-
}
395-
}
389+
attr.Val = camoHandleLink(attr.Val)
396390
node.Attr[i] = attr
397391
}
398392
} else if node.Data == "a" {

0 commit comments

Comments
 (0)