Skip to content

Commit 3ee39a6

Browse files
committed
Prevent duplicate entries in OIDCRegistry
addEntry now checks for existing entries with the same path and port before appending. This keeps storage bounded regardless of how many times the same URL is registered.
1 parent 1c3a4e2 commit 3ee39a6

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

internal/oidc/oidc_registry.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,28 @@ func (r *OIDCRegistry) TryAuth(req *http.Request, ctx *goproxy.ProxyCtx) bool {
154154
}
155155

156156
// addEntry parses a URL or hostname string and adds a credential entry
157-
// to the appropriate host bucket. Returns false if the URL could not be parsed.
157+
// to the appropriate host bucket. Skips duplicates with the same path and port.
158+
// Returns false if the URL could not be parsed.
158159
func (r *OIDCRegistry) addEntry(urlOrHost string, cred *OIDCCredential) bool {
159160
host, path, port := parseRegistryURL(urlOrHost)
160161
if host == "" {
161162
return false
162163
}
163164

164-
entry := oidcEntry{
165+
r.mutex.Lock()
166+
defer r.mutex.Unlock()
167+
168+
for _, e := range r.byHost[host] {
169+
if e.path == path && e.port == port {
170+
return true
171+
}
172+
}
173+
174+
r.byHost[host] = append(r.byHost[host], oidcEntry{
165175
path: path,
166176
port: port,
167177
credential: cred,
168-
}
169-
170-
r.mutex.Lock()
171-
r.byHost[host] = append(r.byHost[host], entry)
172-
r.mutex.Unlock()
178+
})
173179

174180
return true
175181
}

internal/oidc/oidc_registry_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,21 @@ func TestOIDCRegistry_RegisterURL_MultipleOnSameHost(t *testing.T) {
462462
assert.Equal(t, "Bearer __test_token__", req.Header.Get("Authorization"))
463463
}
464464
}
465+
466+
func TestOIDCRegistry_Register_NoDuplicateEntries(t *testing.T) {
467+
setupOIDCEnv(t)
468+
469+
r := NewOIDCRegistry()
470+
471+
cred1 := azureCredWithURL("tenant-1", "client-1", "https://registry.example.com/packages")
472+
cred2 := azureCredWithURL("tenant-2", "client-2", "https://registry.example.com/packages")
473+
474+
r.Register(cred1, []string{"url"}, "test registry")
475+
r.Register(cred2, []string{"url"}, "test registry")
476+
477+
r.mutex.RLock()
478+
entries := r.byHost["registry.example.com"]
479+
r.mutex.RUnlock()
480+
481+
assert.Equal(t, 1, len(entries), "duplicate path+port should not create a second entry")
482+
}

0 commit comments

Comments
 (0)