|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/elazarl/goproxy" |
| 10 | + |
| 11 | + "github.com/dependabot/proxy/internal/ctxdata" |
| 12 | + "github.com/dependabot/proxy/internal/helpers" |
| 13 | + "github.com/dependabot/proxy/internal/logging" |
| 14 | + "github.com/dependabot/proxy/internal/oidc" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + pythonIndexResponseAuthKey = "python-index-response-auth" |
| 19 | + maxPythonIndexDownloadAuthEntries = 1024 |
| 20 | +) |
| 21 | + |
| 22 | +type pythonIndexAuth struct { |
| 23 | + oidc *oidc.OIDCCredential |
| 24 | + basic pythonIndexCredentials |
| 25 | + hasBasic bool |
| 26 | +} |
| 27 | + |
| 28 | +type pythonIndexResponseAuth struct { |
| 29 | + auth pythonIndexAuth |
| 30 | + baseURL url.URL |
| 31 | +} |
| 32 | + |
| 33 | +type pythonIndexDownloadAuthStore struct { |
| 34 | + mutex sync.RWMutex |
| 35 | + entries []pythonIndexDownloadAuthEntry |
| 36 | +} |
| 37 | + |
| 38 | +type pythonIndexDownloadAuthEntry struct { |
| 39 | + prefix *url.URL |
| 40 | + auth pythonIndexAuth |
| 41 | +} |
| 42 | + |
| 43 | +func newPythonIndexDownloadAuthStore() *pythonIndexDownloadAuthStore { |
| 44 | + return &pythonIndexDownloadAuthStore{} |
| 45 | +} |
| 46 | + |
| 47 | +func (s *pythonIndexDownloadAuthStore) add(prefix *url.URL, auth pythonIndexAuth) { |
| 48 | + if prefix == nil { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + normalized := normalizedPythonIndexDownloadURL(prefix) |
| 53 | + if normalized == nil { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + s.mutex.Lock() |
| 58 | + defer s.mutex.Unlock() |
| 59 | + |
| 60 | + for i := range s.entries { |
| 61 | + if sameOrigin(s.entries[i].prefix, normalized) && s.entries[i].prefix.Path == normalized.Path { |
| 62 | + return |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + entry := pythonIndexDownloadAuthEntry{ |
| 67 | + prefix: normalized, |
| 68 | + auth: auth, |
| 69 | + } |
| 70 | + if len(s.entries) >= maxPythonIndexDownloadAuthEntries { |
| 71 | + copy(s.entries, s.entries[1:]) |
| 72 | + s.entries[len(s.entries)-1] = entry |
| 73 | + return |
| 74 | + } |
| 75 | + s.entries = append(s.entries, entry) |
| 76 | +} |
| 77 | + |
| 78 | +func (s *pythonIndexDownloadAuthStore) authFor(req *http.Request) (pythonIndexAuth, bool) { |
| 79 | + s.mutex.RLock() |
| 80 | + defer s.mutex.RUnlock() |
| 81 | + |
| 82 | + var matched pythonIndexAuth |
| 83 | + bestPathLen := -1 |
| 84 | + for _, entry := range s.entries { |
| 85 | + if !sameOrigin(entry.prefix, req.URL) || !isPathPrefix(entry.prefix.Path, req.URL.Path) { |
| 86 | + continue |
| 87 | + } |
| 88 | + if len(entry.prefix.Path) > bestPathLen { |
| 89 | + matched = entry.auth |
| 90 | + bestPathLen = len(entry.prefix.Path) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if bestPathLen < 0 { |
| 95 | + return pythonIndexAuth{}, false |
| 96 | + } |
| 97 | + return matched, true |
| 98 | +} |
| 99 | + |
| 100 | +func (h *PythonIndexHandler) applyAuth(req *http.Request, ctx *goproxy.ProxyCtx, auth pythonIndexAuth) bool { |
| 101 | + if auth.oidc != nil { |
| 102 | + return h.oidcRegistry.TryAuthCredential(req, ctx, auth.oidc) |
| 103 | + } |
| 104 | + if !auth.hasBasic { |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + logging.RequestLogf(ctx, "* authenticating python index request (host: %s)", req.URL.Hostname()) |
| 109 | + |
| 110 | + token := auth.basic.token |
| 111 | + if token == "" && auth.basic.password != "" { |
| 112 | + token = auth.basic.username + ":" + auth.basic.password |
| 113 | + } |
| 114 | + // ignore `found` because it's okay for the password to be an empty string |
| 115 | + username, password, _ := strings.Cut(token, ":") |
| 116 | + helpers.SetBasicAuthorization(req, username, password) |
| 117 | + |
| 118 | + return true |
| 119 | +} |
| 120 | + |
| 121 | +func rememberPythonIndexResponseAuth(ctx *goproxy.ProxyCtx, baseURL *url.URL, auth pythonIndexAuth) { |
| 122 | + if ctx == nil || baseURL == nil { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + ctxdata.SetValue(ctx, pythonIndexResponseAuthKey, pythonIndexResponseAuth{ |
| 127 | + auth: auth, |
| 128 | + baseURL: *baseURL, |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +func pythonIndexResponseAuthFromContext(ctx *goproxy.ProxyCtx) (pythonIndexResponseAuth, bool) { |
| 133 | + if ctx == nil { |
| 134 | + return pythonIndexResponseAuth{}, false |
| 135 | + } |
| 136 | + |
| 137 | + value, ok := ctxdata.GetValue(ctx, pythonIndexResponseAuthKey) |
| 138 | + if !ok { |
| 139 | + return pythonIndexResponseAuth{}, false |
| 140 | + } |
| 141 | + |
| 142 | + responseAuth, ok := value.(pythonIndexResponseAuth) |
| 143 | + return responseAuth, ok |
| 144 | +} |
0 commit comments