Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (da *DigestAuth) Purge(count int) {
}
cache := digestCache(entries)
sort.Sort(cache)
for _, client := range cache[:count] {
Copy link

Choose a reason for hiding this comment

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

You could use min of count/len if you really want to remove count entries only

Copy link
Author

Choose a reason for hiding this comment

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

@andig updated!


for _, client := range cache {
delete(da.clients, client.nonce)
}
}
Expand Down
25 changes: 25 additions & 0 deletions digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,28 @@ func TestDigestAuthParams(t *testing.T) {
t.Fatalf("failed to parse uri with embedded commas, got %q want %q", params["uri"], want)
}
}

// TestDigestPurge tests that when we purge clients from the authenticator we do not purge
// more cache entries than the number of clients we have received.
// This is to avoid regressing and hitting a "slice bounds out of range" panic.
func TestDigestPurge(t *testing.T) {
t.Parallel()
// Creating dummy clients for the digest authenticator.
nClients := 10
clients := make(map[string]*digestClient, nClients)
for i := 0; i < nClients; i++ {
clients[string(i)] = &digestClient{}
}

secrets := HtdigestFileProvider("test.htdigest")
da := &DigestAuth{
Opaque: "U7H+ier3Ae8Skd/g",
Realm: "example.com",
Secrets: secrets,
clients: clients,
}

// Purging more than the number of clients we have stored in the
// digest authenticator.
da.Purge(nClients * 2)
}