mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
feat(security): add request rate limiter on authentication endpoint (#1866)
This commit is contained in:
parent
6360e6a20b
commit
55a96767bb
4 changed files with 122 additions and 3 deletions
69
api/http/security/rate_limiter_test.go
Normal file
69
api/http/security/rate_limiter_test.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package security
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLimitAccess(t *testing.T) {
|
||||
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
t.Run("Request below the limit", func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
rateLimiter := NewRateLimiter(10, 1*time.Second, 1*time.Hour)
|
||||
handler := rateLimiter.LimitAccess(testHandler)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Request above the limit", func(t *testing.T) {
|
||||
rateLimiter := NewRateLimiter(1, 1*time.Second, 1*time.Hour)
|
||||
handler := rateLimiter.LimitAccess(testHandler)
|
||||
|
||||
ts := httptest.NewServer(handler)
|
||||
defer ts.Close()
|
||||
http.Get(ts.URL)
|
||||
resp, err := http.Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if status := resp.StatusCode; status != http.StatusForbidden {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusForbidden)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestStripAddrPort(t *testing.T) {
|
||||
t.Run("IP with port", func(t *testing.T) {
|
||||
result := StripAddrPort("127.0.0.1:1000")
|
||||
if result != "127.0.0.1" {
|
||||
t.Errorf("Expected IP with address to be '127.0.0.1', but it was %s instead", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IP without port", func(t *testing.T) {
|
||||
result := StripAddrPort("127.0.0.1")
|
||||
if result != "127.0.0.1" {
|
||||
t.Errorf("Expected IP with address to be '127.0.0.1', but it was %s instead", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Local IP", func(t *testing.T) {
|
||||
result := StripAddrPort("[::1]:1000")
|
||||
if result != "[::1]" {
|
||||
t.Errorf("Expected IP with address to be '[::1]', but it was %s instead", result)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue