|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package common |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + user_model "code.gitea.io/gitea/models/user" |
| 11 | + "code.gitea.io/gitea/modules/reqctx" |
| 12 | + "code.gitea.io/gitea/modules/setting" |
| 13 | + "code.gitea.io/gitea/modules/web/middleware" |
| 14 | + |
| 15 | + "github.com/go-chi/chi/v5" |
| 16 | +) |
| 17 | + |
| 18 | +func BlockExpensive() func(next http.Handler) http.Handler { |
| 19 | + if !setting.Service.BlockAnonymousAccessExpensive { |
| 20 | + return nil |
| 21 | + } |
| 22 | + return func(next http.Handler) http.Handler { |
| 23 | + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 24 | + ret := determineRequestPriority(reqctx.FromContext(req.Context())) |
| 25 | + if !ret.SignedIn { |
| 26 | + if ret.Expensive || ret.LongPolling { |
| 27 | + http.Redirect(w, req, setting.AppSubURL+"/user/login", http.StatusSeeOther) |
| 28 | + return |
| 29 | + } |
| 30 | + } |
| 31 | + next.ServeHTTP(w, req) |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func isRoutePathExpensive(routePattern string) bool { |
| 37 | + if strings.HasPrefix(routePattern, "/user/") || strings.HasPrefix(routePattern, "/login/") { |
| 38 | + return false |
| 39 | + } |
| 40 | + |
| 41 | + expensivePaths := []string{ |
| 42 | + // code related |
| 43 | + "/{username}/{reponame}/archive/", |
| 44 | + "/{username}/{reponame}/blame/", |
| 45 | + "/{username}/{reponame}/commit/", |
| 46 | + "/{username}/{reponame}/commits/", |
| 47 | + "/{username}/{reponame}/graph", |
| 48 | + "/{username}/{reponame}/media/", |
| 49 | + "/{username}/{reponame}/raw/", |
| 50 | + "/{username}/{reponame}/src/", |
| 51 | + |
| 52 | + // issue & PR related (no trailing slash) |
| 53 | + "/{username}/{reponame}/issues", |
| 54 | + "/{username}/{reponame}/{type:issues}", |
| 55 | + "/{username}/{reponame}/pulls", |
| 56 | + "/{username}/{reponame}/{type:pulls}", |
| 57 | + |
| 58 | + // wiki |
| 59 | + "/{username}/{reponame}/wiki/", |
| 60 | + |
| 61 | + // activity |
| 62 | + "/{username}/{reponame}/activity/", |
| 63 | + } |
| 64 | + for _, path := range expensivePaths { |
| 65 | + if strings.HasPrefix(routePattern, path) { |
| 66 | + return true |
| 67 | + } |
| 68 | + } |
| 69 | + return false |
| 70 | +} |
| 71 | + |
| 72 | +func isRoutePathForLongPolling(routePattern string) bool { |
| 73 | + return routePattern == "/user/events" |
| 74 | +} |
| 75 | + |
| 76 | +func determineRequestPriority(reqCtx reqctx.RequestContext) (ret struct { |
| 77 | + SignedIn bool |
| 78 | + Expensive bool |
| 79 | + LongPolling bool |
| 80 | +}, |
| 81 | +) { |
| 82 | + chiRoutePath := chi.RouteContext(reqCtx).RoutePattern() |
| 83 | + if _, ok := reqCtx.GetData()[middleware.ContextDataKeySignedUser].(*user_model.User); ok { |
| 84 | + ret.SignedIn = true |
| 85 | + } else { |
| 86 | + ret.Expensive = isRoutePathExpensive(chiRoutePath) |
| 87 | + ret.LongPolling = isRoutePathForLongPolling(chiRoutePath) |
| 88 | + } |
| 89 | + return ret |
| 90 | +} |
0 commit comments