Skip to content

Commit 9557588

Browse files
silverwindYohann Delafollye
authored andcommitted
Send 404 immediately for known public requests (go-gitea#11117)
Instead of further handling requests to public which causes issues like go-gitea#11088, immediately terminate requests to directories js, css, fomantic if no file is found which is checked against a hardcoded list. Maybe there is a way to retrieve the top-level entries below public in a dynamic fashion. I also added fomantic to the reserved usernames and sorted the list. Fixes: go-gitea#11088
1 parent 0c1e412 commit 9557588

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

models/user.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -850,23 +850,28 @@ func (u *User) IsGhost() bool {
850850

851851
var (
852852
reservedUsernames = []string{
853-
"attachments",
853+
".",
854+
"..",
855+
".well-known",
854856
"admin",
855857
"api",
856858
"assets",
859+
"attachments",
857860
"avatars",
858861
"commits",
859862
"css",
860863
"debug",
861864
"error",
862865
"explore",
866+
"fomantic",
863867
"ghost",
864868
"help",
865869
"img",
866870
"install",
867871
"issues",
868872
"js",
869873
"less",
874+
"login",
870875
"manifest.json",
871876
"metrics",
872877
"milestones",
@@ -877,16 +882,12 @@ var (
877882
"pulls",
878883
"raw",
879884
"repo",
885+
"robots.txt",
886+
"search",
880887
"stars",
881888
"template",
882889
"user",
883890
"vendor",
884-
"login",
885-
"robots.txt",
886-
".",
887-
"..",
888-
".well-known",
889-
"search",
890891
}
891892
reservedUserPatterns = []string{"*.keys", "*.gpg"}
892893
)

modules/public/public.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ type Options struct {
3030
Prefix string
3131
}
3232

33+
// List of known entries inside the `public` directory
34+
var knownEntries = []string{
35+
"css",
36+
"fomantic",
37+
"img",
38+
"js",
39+
"vendor",
40+
}
41+
3342
// Custom implements the macaron static handler for serving custom assets.
3443
func Custom(opts *Options) macaron.Handler {
3544
return opts.staticHandler(path.Join(setting.CustomPath, "public"))
@@ -99,6 +108,19 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
99108

100109
f, err := opt.FileSystem.Open(file)
101110
if err != nil {
111+
// 404 requests to any known entries in `public`
112+
if path.Base(opts.Directory) == "public" {
113+
parts := strings.Split(file, "/")
114+
if len(parts) < 2 {
115+
return false
116+
}
117+
for _, entry := range knownEntries {
118+
if entry == parts[1] {
119+
ctx.Resp.WriteHeader(404)
120+
return true
121+
}
122+
}
123+
}
102124
return false
103125
}
104126
defer f.Close()

0 commit comments

Comments
 (0)