Skip to content

Commit a37aea6

Browse files
committed
improve
1 parent 3e00536 commit a37aea6

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

services/auth/auth.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ type globalVarsStruct struct {
2626
gitRawOrAttachPathRe *regexp.Regexp
2727
lfsPathRe *regexp.Regexp
2828
archivePathRe *regexp.Regexp
29+
feedPathRe *regexp.Regexp
30+
feedRefPathRe *regexp.Regexp
2931
}
3032

3133
var globalVars = sync.OnceValue(func() *globalVarsStruct {
3234
return &globalVarsStruct{
33-
gitRawOrAttachPathRe: regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/)|(?:attachments/))`),
34-
lfsPathRe: regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`),
35-
archivePathRe: regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/archive/`),
35+
gitRawOrAttachPathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/)|(?:attachments/))`),
36+
lfsPathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/info/lfs/`),
37+
archivePathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/archive/`),
38+
feedPathRe: regexp.MustCompile(`^/[-.\w]+(/[-.\w]+)?\.(rss|atom)$`), // "/owner.rss" or "/owner/repo.atom"
39+
feedRefPathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/(rss|atom)/`), // "/owner/repo/rss/branch/..."
3640
}
3741
})
3842

@@ -61,9 +65,14 @@ func (a *authPathDetector) isAttachmentDownload() bool {
6165
return strings.HasPrefix(a.req.URL.Path, "/attachments/") && a.req.Method == "GET"
6266
}
6367

64-
// isFeed checks if the request targets a rss/atom feed
65-
func isFeed(req *http.Request) bool {
66-
return setting.Other.EnableFeed && req.Method == "GET" && (strings.HasSuffix(req.URL.Path, ".rss") || strings.HasSuffix(req.URL.Path, ".atom"))
68+
func (a *authPathDetector) isFeedRequest(req *http.Request) bool {
69+
if !setting.Other.EnableFeed {
70+
return false
71+
}
72+
if req.Method != "GET" {
73+
return false
74+
}
75+
return a.vars.feedPathRe.MatchString(req.URL.Path) || a.vars.feedRefPathRe.MatchString(req.URL.Path)
6776
}
6877

6978
// isContainerPath checks if the request targets the container endpoint

services/auth/auth_test.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/test"
1213

1314
"github.com/stretchr/testify/assert"
1415
)
@@ -92,20 +93,8 @@ func Test_isGitRawOrLFSPath(t *testing.T) {
9293
true,
9394
},
9495
}
95-
lfsTests := []string{
96-
"/owner/repo/info/lfs/",
97-
"/owner/repo/info/lfs/objects/batch",
98-
"/owner/repo/info/lfs/objects/oid/filename",
99-
"/owner/repo/info/lfs/objects/oid",
100-
"/owner/repo/info/lfs/objects",
101-
"/owner/repo/info/lfs/verify",
102-
"/owner/repo/info/lfs/locks",
103-
"/owner/repo/info/lfs/locks/verify",
104-
"/owner/repo/info/lfs/locks/123/unlock",
105-
}
106-
107-
origLFSStartServer := setting.LFS.StartServer
10896

97+
defer test.MockVariableValue(&setting.LFS.StartServer)()
10998
for _, tt := range tests {
11099
t.Run(tt.path, func(t *testing.T) {
111100
req, _ := http.NewRequest("POST", "http://localhost"+tt.path, nil)
@@ -116,6 +105,18 @@ func Test_isGitRawOrLFSPath(t *testing.T) {
116105
assert.Equal(t, tt.want, newAuthPathDetector(req).isGitRawOrAttachOrLFSPath())
117106
})
118107
}
108+
109+
lfsTests := []string{
110+
"/owner/repo/info/lfs/",
111+
"/owner/repo/info/lfs/objects/batch",
112+
"/owner/repo/info/lfs/objects/oid/filename",
113+
"/owner/repo/info/lfs/objects/oid",
114+
"/owner/repo/info/lfs/objects",
115+
"/owner/repo/info/lfs/verify",
116+
"/owner/repo/info/lfs/locks",
117+
"/owner/repo/info/lfs/locks/verify",
118+
"/owner/repo/info/lfs/locks/123/unlock",
119+
}
119120
for _, tt := range lfsTests {
120121
t.Run(tt, func(t *testing.T) {
121122
req, _ := http.NewRequest("POST", tt, nil)
@@ -128,5 +129,27 @@ func Test_isGitRawOrLFSPath(t *testing.T) {
128129
assert.Equalf(t, setting.LFS.StartServer, got, "isGitOrLFSPath(%q) = %v, want %v", tt, got, setting.LFS.StartServer)
129130
})
130131
}
131-
setting.LFS.StartServer = origLFSStartServer
132+
}
133+
134+
func Test_isFeedRequest(t *testing.T) {
135+
tests := []struct {
136+
want bool
137+
path string
138+
}{
139+
{true, "/user.rss"},
140+
{true, "/user/repo.atom"},
141+
{false, "/user/repo"},
142+
{false, "/use/repo/file.rss"},
143+
144+
{true, "/org/repo/rss/branch/xxx"},
145+
{true, "/org/repo/atom/tag/xxx"},
146+
{false, "/org/repo/branch/main/rss/any"},
147+
{false, "/org/atom/any"},
148+
}
149+
for _, tt := range tests {
150+
t.Run(tt.path, func(t *testing.T) {
151+
req, _ := http.NewRequest("GET", "http://localhost"+tt.path, nil)
152+
assert.Equal(t, tt.want, newAuthPathDetector(req).isFeedRequest(req))
153+
})
154+
}
132155
}

services/auth/basic.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ func (b *Basic) Name() string {
4747
// name/token on successful validation.
4848
// Returns nil if header is empty or validation fails.
4949
func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
50-
// Basic authentication should only fire on API, Download or on Git or LFSPaths
50+
// Basic authentication should only fire on API, Feed, Download or on Git or LFSPaths
51+
// Not all feed (rss/atom) clients feature the ability to add cookies or headers, so we need to allow basic auth for feeds
5152
detector := newAuthPathDetector(req)
52-
if !detector.isAPIPath() && !isFeed(req) && !detector.isContainerPath() && !detector.isAttachmentDownload() && !detector.isGitRawOrAttachOrLFSPath() {
53+
if !detector.isAPIPath() && !detector.isFeedRequest(req) && !detector.isContainerPath() && !detector.isAttachmentDownload() && !detector.isGitRawOrAttachOrLFSPath() {
5354
return nil, nil
5455
}
5556

0 commit comments

Comments
 (0)