Skip to content

Commit 5180deb

Browse files
authored
Send 404 immediately for known public requests (#11117)
Instead of further handling requests to public which causes issues like #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: #11088
1 parent 6034f8b commit 5180deb

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
@@ -844,23 +844,28 @@ func (u *User) IsGhost() bool {
844844

845845
var (
846846
reservedUsernames = []string{
847-
"attachments",
847+
".",
848+
"..",
849+
".well-known",
848850
"admin",
849851
"api",
850852
"assets",
853+
"attachments",
851854
"avatars",
852855
"commits",
853856
"css",
854857
"debug",
855858
"error",
856859
"explore",
860+
"fomantic",
857861
"ghost",
858862
"help",
859863
"img",
860864
"install",
861865
"issues",
862866
"js",
863867
"less",
868+
"login",
864869
"manifest.json",
865870
"metrics",
866871
"milestones",
@@ -871,16 +876,12 @@ var (
871876
"pulls",
872877
"raw",
873878
"repo",
879+
"robots.txt",
880+
"search",
874881
"stars",
875882
"template",
876883
"user",
877884
"vendor",
878-
"login",
879-
"robots.txt",
880-
".",
881-
"..",
882-
".well-known",
883-
"search",
884885
}
885886
reservedUserPatterns = []string{"*.keys", "*.gpg"}
886887
)

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)