-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve rate limiting with better customization (#1476)
Co-authored-by: Dustin Deus <[email protected]>
- Loading branch information
Showing
12 changed files
with
540 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/wundergraph/cosmo/router/internal/expr" | ||
"github.com/wundergraph/cosmo/router/pkg/authentication" | ||
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve" | ||
) | ||
|
||
func expressionResolveContext(t *testing.T, header http.Header, claims map[string]any) *resolve.Context { | ||
req, err := http.NewRequest(http.MethodGet, "http://localhost:3002/graphql", nil) | ||
assert.NoError(t, err) | ||
if header != nil { | ||
req.Header = header | ||
} | ||
rcc := buildRequestContext(requestContextOptions{ | ||
r: req, | ||
}) | ||
ctx := withRequestContext(context.Background(), rcc) | ||
rc := &resolve.Context{ | ||
RateLimitOptions: resolve.RateLimitOptions{ | ||
RateLimitKey: "test", | ||
}, | ||
} | ||
if claims != nil { | ||
rc = ContextWithClaims(rc, claims) | ||
rcc.expressionContext.Request.Auth = expr.LoadAuth(rc.Context()) | ||
} | ||
return rc.WithContext(ctx) | ||
} | ||
|
||
func TestRateLimiterGenerateKey(t *testing.T) { | ||
t.Parallel() | ||
t.Run("default", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey(expressionResolveContext(t, nil, nil)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test", key) | ||
}) | ||
t.Run("from header", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "request.header.Get('Authorization')", | ||
}) | ||
require.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, http.Header{"Authorization": []string{"token"}}, nil), | ||
) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test:token", key) | ||
}) | ||
t.Run("from header number", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "request.header.Get('Authorization')", | ||
}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, http.Header{"Authorization": []string{"123"}}, nil), | ||
) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test:123", key) | ||
}) | ||
t.Run("from header whitespace", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "trim(request.header.Get('Authorization'))", | ||
}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, http.Header{"Authorization": []string{" token "}}, nil), | ||
) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test:token", key) | ||
}) | ||
t.Run("from claims", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "request.auth.claims.sub", | ||
}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, nil, map[string]any{"sub": "token"}), | ||
) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test:token", key) | ||
}) | ||
t.Run("from claims invalid claim", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "request.auth.claims.sub", | ||
}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, nil, map[string]any{"sub": 123}), | ||
) | ||
assert.Error(t, err) | ||
assert.Empty(t, key) | ||
}) | ||
t.Run("from claims or X-Forwarded-For header claims present", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "request.auth.claims.sub ?? request.header.Get('X-Forwarded-For')", | ||
}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, http.Header{"X-Forwarded-For": []string{"192.168.0.1"}}, map[string]any{"sub": "token"}), | ||
) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test:token", key) | ||
}) | ||
t.Run("from claims or X-Forwarded-For header claims not present", func(t *testing.T) { | ||
t.Parallel() | ||
rl, err := NewCosmoRateLimiter(&CosmoRateLimiterOptions{ | ||
KeySuffixExpression: "request.auth.claims.sub ?? request.header.Get('X-Forwarded-For')", | ||
}) | ||
assert.NoError(t, err) | ||
key, err := rl.generateKey( | ||
expressionResolveContext(t, http.Header{"X-Forwarded-For": []string{"192.168.0.1"}}, nil), | ||
) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "test:192.168.0.1", key) | ||
}) | ||
} | ||
|
||
func ContextWithClaims(ctx *resolve.Context, claims map[string]any) *resolve.Context { | ||
auth := &FakeAuthenticator{ | ||
claims: claims, | ||
} | ||
withScopes := authentication.NewContext(context.Background(), auth) | ||
return ctx.WithContext(withScopes) | ||
} | ||
|
||
type FakeAuthenticator struct { | ||
claims map[string]any | ||
scopes []string | ||
} | ||
|
||
func (f *FakeAuthenticator) Authenticator() string { | ||
return "fake" | ||
} | ||
|
||
func (f *FakeAuthenticator) Claims() authentication.Claims { | ||
return f.claims | ||
} | ||
|
||
func (f *FakeAuthenticator) SetScopes(scopes []string) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (f *FakeAuthenticator) Scopes() []string { | ||
return f.scopes | ||
} |
Oops, something went wrong.