Skip to content
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

Implement Accounts creation rate limit #1800

Merged
merged 3 commits into from
Aug 7, 2024

Conversation

hectorgomezv
Copy link
Member

Summary

This PR introduces a rate limitation to creating User Accounts using the related AccountsController endpoint.

Changes

  • Adds a rate limitation controlled by a cache key before proceeding with account creation.

@hectorgomezv hectorgomezv self-assigned this Aug 5, 2024
@hectorgomezv hectorgomezv requested a review from a team as a code owner August 5, 2024 11:00
Comment on lines +10 to +16
creationRateLimitPeriodSeconds: parseInt(
process.env.ACCOUNT_CREATION_RATE_LIMIT_PERIOD_SECONDS ?? `${60}`,
),
creationRateLimitCalls: parseInt(
process.env.ACCOUNT_CREATION_RATE_LIMIT_CALLS_BY_PERIOD ?? `${1}`,
),
},
Copy link
Member Author

Choose a reason for hiding this comment

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

These defaults allow each IP to create an account per minute. As the account is cross-chain, I thought that one account should be sufficient for most users.

Comment on lines +70 to +71
address: `0x${string}`;
clientIp: string;
Copy link
Member Author

Choose a reason for hiding this comment

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

As this function's arity increased to 2, I've wrapped the arguments on a type, which caused some additional changes in the PR.

Comment on lines +224 to +226
this.loggingService.warn(
`Invalid client IP while creating account: ${clientIp}`,
);
Copy link
Member Author

Choose a reason for hiding this comment

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

If the IP is not defined or it's not well formatted, the account creation is allowed. This is mostly done to avoid blockers, but I think we need to keep an eye on logs if we start seeing too many warnings here.

Copy link

Choose a reason for hiding this comment

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

In my understanding, it cannot happen.

you get this IP address from web server, who gets it from http(s) which is over tpc, where server always has to know the address on the other side.

even with http3, the udp packets would still contain originators ip. To become empty or malformed, it would need to be specially crafted.

So for this case I would propose to throw an error as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense to me, I've added a ticket to change it: #1838

Thanks!

Comment on lines +235 to +237
this.loggingService.warn(
`Limit of ${this.accountCreationRateLimitCalls} reached for IP ${clientIp}`,
);
Copy link
Member Author

Choose a reason for hiding this comment

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

As before, I added a log line here also, as getting too many rate limit conditions shouldn't be normal.

@coveralls
Copy link

coveralls commented Aug 5, 2024

Pull Request Test Coverage Report for Build 10270314343

Details

  • 5 of 17 (29.41%) changed or added relevant lines in 3 files are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.03%) to 48.03%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/domain/accounts/accounts.repository.ts 0 1 0.0%
src/datasources/accounts/accounts.datasource.ts 3 14 21.43%
Files with Coverage Reduction New Missed Lines %
src/domain/accounts/accounts.repository.ts 1 10.53%
src/datasources/accounts/accounts.datasource.ts 1 17.07%
Totals Coverage Status
Change from base Build 10247478509: -0.03%
Covered Lines: 4427
Relevant Lines: 7440

💛 - Coveralls

Copy link
Member

@iamacook iamacook left a comment

Choose a reason for hiding this comment

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

I'm not sure if this should be happening in the datasource. Rate limiting is more guard behaviour to me. What do you think?

@@ -88,20 +91,126 @@ describe('AccountsDatasource tests', () => {
);
});

it('throws when an account with the same address already exists', async () => {
it('creates an account successfully if the clientIp is not a valid IP', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
it('creates an account successfully if the clientIp is not a valid IP', async () => {
it('creates an account successfully if the clientIp is a valid IP', async () => {

Copy link
Member Author

@hectorgomezv hectorgomezv Aug 6, 2024

Choose a reason for hiding this comment

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

Hmm, this is intended, the current behavior is to allow nonvalid IPs to create an account, and log this fact as a warning. The rationale is to cover the case where for some reason our infra sees a modified IP somehow. The idea is to not block users whose IP is obfuscated in some way. I'm open to restricting the account creation to valid IPs only btw. I guess we need to evaluate this while running the service. Wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

It seems trivial to rate limit based on IP but allow users to bypass it by obfuscating their IP. Do we know if there's a high chance of modified IPs? I am impartial regarding this nonetheless so we can leave it as if we want.

Copy link
Member Author

@hectorgomezv hectorgomezv Aug 6, 2024

Choose a reason for hiding this comment

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

I guess we will see how this goes through time, I don't think users can obfuscate their IPs easily. I think we can start allowing account creation but keep an eye on logging to see if this (a malformed IP reaching the service) happens often. So I'd leave it as it is for now, and maybe reconsider it based on the logs.

src/datasources/accounts/accounts.datasource.spec.ts Outdated Show resolved Hide resolved
src/datasources/accounts/accounts.datasource.ts Outdated Show resolved Hide resolved
@hectorgomezv
Copy link
Member Author

hectorgomezv commented Aug 6, 2024

I'm not sure if this should be happening in the datasource. Rate limiting is more guard behaviour to me. What do you think?

I don't have a super firm opinion, but I think it makes sense to have the limiter on the datasource for this use case as we have to limit the actual account creations. I guess if we add the rate limiter to the controller layer it wouldn't consider failed attempts for example. For a GET endpoint, I'd be more in favor of limiting the endpoint directly.

Also, even if we put this on a Guard, it would need to have access to the Redis instance, as the rate limiter count needs to be shared among the CGW pods (maybe this could help if we want to do it https://github.com/kkoomen/nestjs-throttler-storage-redis).

@iamacook
Copy link
Member

iamacook commented Aug 6, 2024

I don't have a super firm opinion, but I think it makes sense to have the limiter on the datasource for this use case as we have to limit the actual account creations. I guess if we add the rate limiter to the controller layer it wouldn't consider failed attempts for example. For a GET endpoint, I'd be more in favor of limiting the endpoint directly.

Also, even if we put this on a Guard, it would need to have access to the Redis instance, as the rate limiter count needs to be shared among the CGW pods (maybe this could help if we want to do it https://github.com/kkoomen/nestjs-throttler-storage-redis).

Considering we want to limit creations and this can happen via various methods - I am happy with keeping it as it is. Although a nit, I think we should add a comment explaining the above.

@hectorgomezv
Copy link
Member Author

Considering we want to limit creations and this can happen via various methods - I am happy with keeping it as it is. Although a nit, I think we should add a comment explaining the above.

Great, thanks! I've included a note in 031aaca, please let me know if you think it's not clearer enough, or if you would like to rephrase it.

@hectorgomezv hectorgomezv merged commit 234a559 into main Aug 7, 2024
16 checks passed
@hectorgomezv hectorgomezv deleted the add-accounts-creation-ip-rate-limit branch August 7, 2024 08:26
this.loggingService.warn(
`Limit of ${this.accountCreationRateLimitCalls} reached for IP ${clientIp}`,
);
throw new LimitReachedError();
Copy link

Choose a reason for hiding this comment

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

Which http code would a client get?
this Error is not extending HttpException. Is it necessary to implement an ExceptionFilter for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

With this implementation, the client would get a generic 500 status code error.

I've created a PR to extend HttpException and set the correct status codes for both accounts and Counterfactual Safes rate limit hit errors on creation: #1865

Thank you!

DenSmolonski pushed a commit to Zilliqa/safe-client-gateway that referenced this pull request Oct 24, 2024
Adds a rate limitation controlled by a cache key before proceeding with account creation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants