-
Notifications
You must be signed in to change notification settings - Fork 14
fix: add a more aggressive rate limit for spammy users #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wa0x6e
wants to merge
17
commits into
master
Choose a base branch
from
more-strict-rate-limit-for-spammer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+202
−125
Open
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1c9f811
refactor: extract redis config to its own file
wa0x6e 5e63a14
chore: enable redis in test suite
wa0x6e b7fb7a2
chore: use different prefix to tests
wa0x6e 49c0d3e
Merge branch 'master' into extract-redis-to-file
wa0x6e 8ac9c13
fix: add a more aggressive rate limit for high errored users
wa0x6e 1cba651
chore: upgrade rate-express-limit
wa0x6e e81463f
fix: use separate key for different rate-limiter
wa0x6e 610cd85
Merge branch 'extract-redis-to-file' into more-strict-rate-limit-for-…
wa0x6e 42e246a
Merge branch 'master' into more-strict-rate-limit-for-spammer
wa0x6e ff30217
chore: add tests
wa0x6e cfcbad8
refactor: rename rateLimit to a more appropriate name
wa0x6e 04190bd
chore: fix tests
wa0x6e e6ca0bb
chore: sync package.json
wa0x6e 504817e
Merge branch 'master' into more-strict-rate-limit-for-spammer
wa0x6e 25697ae
chore: fix tests
wa0x6e e670324
fix: fix leaking redisStore key expiration
wa0x6e c37f0d2
Merge branch 'master' into more-strict-rate-limit-for-spammer
wa0x6e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -9,3 +9,4 @@ jobs: | |
with: | ||
mysql_database_name: snapshot_sequencer_test | ||
mysql_schema_path: 'test/schema.sql' | ||
redis: true |
This file contains hidden or 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 hidden or 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,19 @@ | ||
import { createClient } from 'redis'; | ||
import log from './log'; | ||
|
||
let client; | ||
|
||
(async () => { | ||
if (!process.env.RATE_LIMIT_DATABASE_URL) return; | ||
|
||
log.info('[redis] Connecting to Redis'); | ||
client = createClient({ url: process.env.RATE_LIMIT_DATABASE_URL }); | ||
client.on('connect', () => log.info('[redis] Redis connect')); | ||
client.on('ready', () => log.info('[redis] Redis ready')); | ||
client.on('reconnecting', err => log.info('[redis] Redis reconnecting', err)); | ||
client.on('error', err => log.info('[redis] Redis error', err)); | ||
client.on('end', err => log.info('[redis] Redis end', err)); | ||
await client.connect(); | ||
})(); | ||
|
||
export default client; |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -3,3 +3,5 @@ SEQ_DATABASE_URL=mysql://root:[email protected]:3306/snapshot_sequencer_test | |
NETWORK=main | ||
RELAYER_PK=01686849e86499c1860ea0afc97f29c11018cbac049abf843df875c60054076e | ||
NODE_ENV=test | ||
RATE_LIMIT_DATABASE_URL=redis://localhost:6379 | ||
RATE_LIMIT_KEYS_PREFIX=snapshot-sequencer-test: |
This file contains hidden or 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 |
---|---|---|
@@ -1,21 +1,71 @@ | ||
import fetch from 'node-fetch'; | ||
import proposalInput from '../fixtures/ingestor-payload/proposal.json'; | ||
import redis from '../../src/helpers/redis'; | ||
|
||
const HOST = `http://localhost:${process.env.PORT || 3003}`; | ||
|
||
function successRequest() { | ||
return fetch(`${HOST}`); | ||
} | ||
|
||
function failRequest() { | ||
return fetch(HOST, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(proposalInput) | ||
}); | ||
} | ||
|
||
describe('POST /', () => { | ||
beforeEach(async () => { | ||
const keyPrefix = process.env.RATE_LIMIT_KEYS_PREFIX || 'snapshot-sequencer:'; | ||
await redis.del(`${keyPrefix}rl:3e48ef9`); | ||
await redis.del(`${keyPrefix}rl-s:3e48ef9`); | ||
}); | ||
|
||
afterAll(async () => { | ||
await redis.quit(); | ||
}); | ||
|
||
describe('on invalid client input', () => { | ||
it('returns a 400 error', async () => { | ||
const response = await fetch(HOST, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(proposalInput) | ||
}); | ||
const response = await failRequest(); | ||
const body = await response.json(); | ||
|
||
expect(response.status).toBe(400); | ||
expect(body.error).toBe('client_error'); | ||
expect(body.error_description).toBe('wrong timestamp'); | ||
}); | ||
}); | ||
|
||
describe('rate limit', () => { | ||
describe('on a mix of success and failed requests', () => { | ||
it('should return a 429 errors only after 100 requests / min', async () => { | ||
for (let i = 1; i <= 100; i++) { | ||
// 2% of failing requests | ||
const response = await (Math.random() < 0.02 ? failRequest() : successRequest()); | ||
expect(response.status).not.toEqual(429); | ||
} | ||
|
||
const response = await fetch(HOST, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(proposalInput) | ||
}); | ||
expect(response.status).toBe(429); | ||
}); | ||
}); | ||
|
||
describe('on multiple failed requests', () => { | ||
it('should return a 429 errors after 15 requests / 15s', async () => { | ||
for (let i = 1; i <= 15; i++) { | ||
const response = await failRequest(); | ||
expect(response.status).toBe(400); | ||
} | ||
|
||
const response = await fetch(`${HOST}/scores/proposal-id`); | ||
expect(response.status).toBe(429); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.