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

Redirect /serviceworker.js to assets/ #15823

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ var (
"template",
"user",
"favicon.ico",
"serviceworker.js",
}

reservedUserPatterns = []string{"*.keys", "*.gpg"}
Expand Down
3 changes: 3 additions & 0 deletions routers/routes/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func WebRoutes() *web.Route {
// We use r.Route here over r.Use because this prevents requests that are not for avatars having to go through this additional handler
routes.Route("/avatars/*", "GET, HEAD", storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
routes.Route("/repo-avatars/*", "GET, HEAD", storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
routes.Route("/serviceworker.js", "GET, HEAD", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
http.Redirect(resp, req, setting.AppURL+"assets/serviceworker.js", http.StatusFound)
Copy link
Member

@silverwind silverwind May 10, 2021

Choose a reason for hiding this comment

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

Suggested change
http.Redirect(resp, req, setting.AppURL+"assets/serviceworker.js", http.StatusFound)
http.Redirect(resp, req, setting.AppURL+"assets/serviceworker.js", http.StatusTemporaryRedirect)

We should only use 307 or 308 redirects because those forbid method change which is better for both functionality and security.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not certain I understand how security or functionality could affected by posts etc being redirected to assets - in fact it would be better if they were.

Copy link
Member

Choose a reason for hiding this comment

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

Does the web browser support the redirect?

Copy link
Member

@silverwind silverwind May 11, 2021

Choose a reason for hiding this comment

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

It does not matter for GET requests but for other methods, browsers may decide to change method to GET based on obscure criteria related to HTML forms which I think is an outdated concept. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#temporary_redirections for more details.

Security can be impacted of POST to a endpoint with credentials where a change to GET would move potentially sensitive parameters from request body to request URL, adding them to the browser history and making them visible in server logs. For that reason alone, I never use 301, 302 or 303, only 307 and 308 which of course all browsers support.

}))

// for health check - doeesn't need to be passed through gzip handler
routes.Head("/", func(w http.ResponseWriter, req *http.Request) {
Expand Down