Skip to content

Commit 55d922e

Browse files
committed
Send 404 immediately for known public requests
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 12960b9 commit 55d922e

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ type Options struct {
3030
Prefix string
3131
}
3232

33+
// List of entries inside the `public` directory
34+
var resourceEntries = []string{
35+
"js",
36+
"css",
37+
"fomantic",
38+
}
39+
3340
// Custom implements the macaron static handler for serving custom assets.
3441
func Custom(opts *Options) macaron.Handler {
3542
return opts.staticHandler(path.Join(setting.CustomPath, "public"))
@@ -99,6 +106,20 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
99106

100107
f, err := opt.FileSystem.Open(file)
101108
if err != nil {
109+
// 404 requests to any known entries in `public`
110+
if path.Base(opts.Directory) == "public" {
111+
parts := strings.Split(file, "/")
112+
if len(parts) < 2 {
113+
return false
114+
}
115+
for _, entry := range resourceEntries {
116+
if entry == parts[1] {
117+
ctx.Resp.WriteHeader(404)
118+
ctx.Resp.Write([]byte(""))
119+
return true
120+
}
121+
}
122+
}
102123
return false
103124
}
104125
defer f.Close()

0 commit comments

Comments
 (0)