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

Conversation

zeripath
Copy link
Contributor

Since we moved the assets to the /assets there are multiple requests to serviceworker.js that are getting 404'd.

This PR simply redirects requests to /assets.

Signed-off-by: Andrew Thornton art27@cantab.net

Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath added this to the 1.15.0 milestone May 10, 2021
@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label May 10, 2021
@techknowlogick techknowlogick added the skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. label May 10, 2021
@silverwind
Copy link
Member

I think this may be old serviceworker instances that are trying to self-update. The new ones should be on the correct path. It may be possible to cleanly unregister the old instances from JS, but I'm not exactly sure how to do it.

@@ -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.

@a1012112796
Copy link
Member

Don't forgot add serviceworker.js to reservedUsernames list also.

@a1012112796
Copy link
Member

I think it's okay to add routes /serviceworker.js only, but should limit scope

example:

navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`, {scope: '/assets'}),

@zeripath
Copy link
Contributor Author

Don't forgot add serviceworker.js to reservedUsernames list also.

As far as I understood it already was.

@zeripath
Copy link
Contributor Author

I think it's okay to add routes /serviceworker.js only, but should limit scope

example:

navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`, {scope: '/assets'}),

I don't understand what you mean.

@zeripath
Copy link
Contributor Author

I think this may be old serviceworker instances that are trying to self-update. The new ones should be on the correct path. It may be possible to cleanly unregister the old instances from JS, but I'm not exactly sure how to do it.

Yes I'm not certain why these requests are being made either. It could be due to chrome's setting UpdateOnReload in the developer settings but I don't understand why it would actually request this. AFAIU the service worker should now be embedded.

However given this repeated querying I think we do have to redirect it.

@silverwind
Copy link
Member

silverwind commented May 11, 2021

I think we just should instead cleanly unregister all service workers in JS that are not on the current scope. Quick test on try.gitea.io shows two active service workers since the /assets change:

(await navigator.serviceWorker.getRegistrations()).map(r => r.scope)
// => [ "https://try.gitea.io/", "https://try.gitea.io/assets/" ]

I will try to come up with something.

@silverwind
Copy link
Member

silverwind commented May 11, 2021

I think it's okay to add routes /serviceworker.js only, but should limit scope

Adding a scope parameter is unnecessary, it will default to the directory of the loaded script, e.g. /assets:

By default, the scope value for a service worker registration is set to the directory where the service worker script is located

@a1012112796

This comment has been minimized.

@zeripath
Copy link
Contributor Author

agh it must have been moved out when we did the assets change - thanks @a1012112796 I'll add it back in. I don't think we really want to allow a user to be named serviceworker.js anyway.

Hopefully @silverwind will figure out how to make this PR completely unnecessary.

Signed-off-by: Andrew Thornton <art27@cantab.net>
@silverwind
Copy link
Member

silverwind commented May 11, 2021

Alternative PR: #15834

silverwind added a commit to silverwind/gitea that referenced this pull request May 11, 2021
With the addition of the /assets url, users who visited a previous
version of the site now may have two active service workers, one with
the old scope `/` and one with scope `/assets`. This check for
serviceworkers that do not match the current script path and unregisters
them.

Also included is a small refactor to publicpath.js which was simplified
because AssetUrlPrefix is always present now. Also it makes use of the
new joinPaths helper too.

Fixes: go-gitea#15823
@codecov-commenter
Copy link

Codecov Report

Merging #15823 (dfd5ad5) into main (d37a89e) will increase coverage by 0.01%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #15823      +/-   ##
==========================================
+ Coverage   43.98%   44.00%   +0.01%     
==========================================
  Files         680      681       +1     
  Lines       82212    82231      +19     
==========================================
+ Hits        36162    36182      +20     
- Misses      40134    40139       +5     
+ Partials     5916     5910       -6     
Impacted Files Coverage Δ
models/user.go 53.15% <ø> (ø)
routers/routes/web.go 90.50% <0.00%> (-0.34%) ⬇️
routers/repo/download.go 44.26% <0.00%> (-1.51%) ⬇️
models/repo_list.go 77.04% <0.00%> (-0.78%) ⬇️
services/pull/pull.go 43.37% <0.00%> (ø)
modules/structs/user.go 100.00% <0.00%> (ø)
modules/notification/mail/mail.go 38.77% <0.00%> (ø)
modules/setting/mime_type_map.go 55.55% <0.00%> (ø)
modules/setting/setting.go 49.90% <0.00%> (+0.09%) ⬆️
modules/convert/user.go 94.11% <0.00%> (+0.36%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d37a89e...dfd5ad5. Read the comment docs.

@6543 6543 closed this in #15834 May 12, 2021
6543 pushed a commit that referenced this pull request May 12, 2021
* Unregister non-matching serviceworkers

With the addition of the /assets url, users who visited a previous
version of the site now may have two active service workers, one with
the old scope `/` and one with scope `/assets`. This check for
serviceworkers that do not match the current script path and unregisters
them.

Also included is a small refactor to publicpath.js which was simplified
because AssetUrlPrefix is always present now. Also it makes use of the
new joinPaths helper too.

Fixes: #15823
@zeripath zeripath deleted the redirect-serviceworker.js-to-assets branch May 12, 2021 19:26
AbdulrhmnGhanem pushed a commit to kitspace/gitea that referenced this pull request Aug 10, 2021
* Unregister non-matching serviceworkers

With the addition of the /assets url, users who visited a previous
version of the site now may have two active service workers, one with
the old scope `/` and one with scope `/assets`. This check for
serviceworkers that do not match the current script path and unregisters
them.

Also included is a small refactor to publicpath.js which was simplified
because AssetUrlPrefix is always present now. Also it makes use of the
new joinPaths helper too.

Fixes: go-gitea#15823
@go-gitea go-gitea locked and limited conversation to collaborators Oct 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants