Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class QueryType < GraphQL::Schema::Object
end
```

This would block requests starting from the 15th attempt within a 60-second window by the same IP address. That is, if 15 requests are made, the first 14 will succeed and the 15th will fail.
This would allow 15 requests per minute by the same IP address, blocking the 16th and subsequent requests within that 60-second window.

## Requirements

Expand Down
8 changes: 6 additions & 2 deletions lib/graph_attack/rate_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ def key
def calls_exceeded_on_query?(rate_limited_field)
with_redis_client do |redis_client|
rate_limit = Ratelimit.new(rate_limited_field, redis: redis_client)
rate_limit.add(key)
rate_limit.exceeded?(key, threshold: threshold, interval: interval)
if rate_limit.exceeded?(key, threshold: threshold, interval: interval)
true
else
rate_limit.add(key)
false
end
end
end

Expand Down
43 changes: 36 additions & 7 deletions spec/graph_attack/rate_limit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class QueryType < GraphQL::Schema::Object
extension GraphAttack::RateLimit
end

field :field_with_threshold_one, String, null: false do
extension GraphAttack::RateLimit, threshold: 1, interval: 60
end

def inexpensive_field
'result'
end
Expand Down Expand Up @@ -69,6 +73,10 @@ def field_with_on_option
def field_with_defaults
'result'
end

def field_with_threshold_one
'result'
end
end

class Schema < GraphQL::Schema
Expand Down Expand Up @@ -120,7 +128,7 @@ class Schema < GraphQL::Schema
end

it 'returns data until rate limit is exceeded' do
4.times do
5.times do
result = schema.execute(query, context: context)

expect(result).not_to have_key('errors')
Expand All @@ -130,7 +138,7 @@ class Schema < GraphQL::Schema

context 'when rate limit is exceeded' do
before do
4.times do
5.times do
schema.execute(query, context: context)
end
end
Expand Down Expand Up @@ -196,7 +204,7 @@ class Schema < GraphQL::Schema
end

it 'returns an error when the default rate limit is exceeded' do
2.times do
3.times do
result = schema.execute(query, context: context)

expect(result).not_to have_key('errors')
Expand Down Expand Up @@ -229,7 +237,7 @@ class Schema < GraphQL::Schema
end

before do
5.times do
6.times do
schema.execute(query, context: context)
end
end
Expand Down Expand Up @@ -259,7 +267,7 @@ class Schema < GraphQL::Schema
end

before do
10.times do
11.times do
schema.execute(query, context: context)
end
end
Expand Down Expand Up @@ -310,7 +318,7 @@ class Schema < GraphQL::Schema
end

it 'returns data until rate limit is exceeded' do
9.times do
10.times do
result = schema.execute(query, context: context)

expect(result).not_to have_key('errors')
Expand All @@ -320,7 +328,7 @@ class Schema < GraphQL::Schema

context 'when rate limit is exceeded' do
before do
9.times do
10.times do
schema.execute(query, context: context)
end
end
Expand Down Expand Up @@ -351,6 +359,27 @@ class Schema < GraphQL::Schema
end
end
end

describe 'field with threshold: 1' do
let(:query) { '{ fieldWithThresholdOne }' }

it 'allows exactly 1 request before blocking the 2nd' do
# First request should succeed
result = schema.execute(query, context: context)
expect(result).not_to have_key('errors')
expect(result['data']).to eq('fieldWithThresholdOne' => 'result')

# Second request should be blocked
result = schema.execute(query, context: context)
expected_error = {
'locations' => [{ 'column' => 3, 'line' => 1 }],
'message' => 'Query rate limit exceeded',
'path' => ['fieldWithThresholdOne'],
}
expect(result['errors']).to eq([expected_error])
expect(result['data']).to be_nil
end
end
end

context 'when context has not IP key' do
Expand Down