Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send 404 immediately for known public requests #11117

Merged
merged 5 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,23 +844,28 @@ func (u *User) IsGhost() bool {

var (
reservedUsernames = []string{
"attachments",
".",
"..",
".well-known",
"admin",
"api",
"assets",
"attachments",
"avatars",
"commits",
"css",
"debug",
"error",
"explore",
"fomantic",
"ghost",
"help",
"img",
"install",
"issues",
"js",
"less",
"login",
"manifest.json",
"metrics",
"milestones",
Expand All @@ -871,16 +876,12 @@ var (
"pulls",
"raw",
"repo",
"robots.txt",
"search",
"stars",
"template",
"user",
"vendor",
"login",
"robots.txt",
".",
"..",
".well-known",
"search",
}
reservedUserPatterns = []string{"*.keys", "*.gpg"}
)
Expand Down
20 changes: 20 additions & 0 deletions modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type Options struct {
Prefix string
}

// List of known entries inside the `public` directory
var knownEntries = []string{
"js",
"css",
"fomantic",
}

// Custom implements the macaron static handler for serving custom assets.
func Custom(opts *Options) macaron.Handler {
return opts.staticHandler(path.Join(setting.CustomPath, "public"))
Expand Down Expand Up @@ -99,6 +106,19 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)

f, err := opt.FileSystem.Open(file)
if err != nil {
// 404 requests to any known entries in `public`
if path.Base(opts.Directory) == "public" {
Copy link
Member Author

@silverwind silverwind Apr 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition seems to be needed because this handler is hit twice on every request, first with opts.Directory set to "" and then with an actual path ending in /public, not sure why.

parts := strings.Split(file, "/")
if len(parts) < 2 {
return false
}
for _, entry := range knownEntries {
if entry == parts[1] {
ctx.Resp.WriteHeader(404)
return true
}
}
}
return false
}
defer f.Close()
Expand Down