Skip to content

Conversation

@abhigyanghosh30
Copy link
Contributor

@abhigyanghosh30 abhigyanghosh30 commented Mar 10, 2025

Done

  • Add rate limit to magic attach endpoint

QA

  • Check out this feature branch
  • Run the site using the command ./run serve or dotrun
  • View the site locally in your web browser at: http://0.0.0.0:8001/
    • Be sure to test on mobile, tablet and desktop screen sizes
  • Go to /pro/attach
  • Set the limit to something like 1 in 1500
    • Try to submit more times than the limit
    • Should get a limited response as below
  • Let the time limit pass
    • Should be able to submit again

Issue / Card

Fixes #

Screenshots

image

Help

QA steps - Commit guidelines

@webteam-app
Copy link

@abhigyanghosh30 abhigyanghosh30 marked this pull request as draft March 10, 2025 21:42
import flask
from webapp.login import user_info

rate_limits = defaultdict(deque)
Copy link
Contributor

Choose a reason for hiding this comment

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

Having a global dictionary for rate limiting is simple but it has its pitfalls which you should be aware of:

  • This is per process memory and each instance of app is going to have independent memory
  • It isn't thread safe
  • There is no way to reclaim the memory being used by this dictionary, it will keep growing indefinitely unless the process is stopped
  • Also, these limits will reset as soon as the process stops. Users will be able to make requests again.

I would suggest that you go with a different approach that caters to the above problems that will arise

Redis comes to my mind as a solution.

Copy link
Contributor Author

@abhigyanghosh30 abhigyanghosh30 Mar 11, 2025

Choose a reason for hiding this comment

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

  1. That is not going to be a problem because the limits are not a hard limit
  2. The order of the array does not matter in this case. Thread safety should not affect the functioning of the limiter.
  3. It removes older timestamps when there is a repeated request.
  4. Yes that is what is expected as this is going to be in memory

Redis deployment will take time as it will need to requested from IS and this is something temporary that does the job as we don't have a database or anything to add stateful features to our application.
That being said this is not a final implementation as I am implementing a leaky bucket. Hence this PR is in draft and not yet ready for review. That would add a process to remove older requests from the queue at a steady rate.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please let me know how many instances of the app are running at a given time?

Is this system running behind K8s?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes the website is on k8s, there are multiple pods in prod and staging and only one in demos running that specific app instance.
Also it is a moot point now because I am storing it in session which means it is stored in the user's end as a signed cookie. So this means the only way to tamper with it is to invalidate the session which means they need to login again. Since the goal was to stop misuse, this should work

abhigyanghosh30 and others added 21 commits March 11, 2025 20:41
Co-authored-by: Muhammad Ali <its.muhammad.ali.mughal@gmail.com>
…html

Co-authored-by: Muhammad Ali <its.muhammad.ali.mughal@gmail.com>
…html

Co-authored-by: Muhammad Ali <its.muhammad.ali.mughal@gmail.com>
…html

Co-authored-by: Muhammad Ali <its.muhammad.ali.mughal@gmail.com>
…html

Co-authored-by: Muhammad Ali <its.muhammad.ali.mughal@gmail.com>
…html

Co-authored-by: Muhammad Ali <its.muhammad.ali.mughal@gmail.com>
samhotep and others added 22 commits March 18, 2025 19:18
* fix(performance): Add API token for the perf-checks CI

* Replace api_key with key
* add form on cancel subscription
* Exclude static case studies from ubuntu.com/engage

* Bumped discourse module version

* format python
 suggesting the addition of esm-infra-legacy pockets to cves and usns
 webpages so we can have the hyperlink to Ubuntu Pro and the note
 about this pocket. It is not a new pocket, it is already known in
 the ubuntu-com-security-api and it is being pushed to our websites
 already, we are just missing the information as we have for esm-infra

Signed-off-by: Rodrigo Figueiredo Zaiden <rodrigo.zaiden@canonical.com>
 reformating with 'dotrun format-python' to fix
 lint python report.

Signed-off-by: Rodrigo Figueiredo Zaiden <rodrigo.zaiden@canonical.com>
@abhigyanghosh30 abhigyanghosh30 force-pushed the WD-20031-add-auth-based-rate-limiting-to-magic-attach-endpoint branch from ab4c2fc to 86308bd Compare March 18, 2025 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.