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

Logger API: Add rate limiting #1142

Merged
merged 1 commit into from
Mar 26, 2024
Merged

Logger API: Add rate limiting #1142

merged 1 commit into from
Mar 26, 2024

Conversation

bgrgicak
Copy link
Collaborator

@bgrgicak bgrgicak commented Mar 26, 2024

Fixes #1123

What is this PR doing?

It adds rate limiting to the logger API.

What problem is it solving?

Increases protection from spam requests.

How is the problem addressed?

By tracking the number of requests in $_SESSION and returning early if the limit was reached.

Testing Instructions

  • Checkout this branch
  • Copy the logger.php file to a PHP local server or start a Docker server
cd packages/playground/website/public/
docker run -d -p 8787:80 --name playground-website -v "$PWD":/var/www/html php:8.0-apache
  • Open the API endpoint link
  • Reload 5 times and confirm that the error message changed to Too many requests

Known limitations
Because this is implemented using $_SESSION it can easily be worked around using curl or by removing session data.
To improve rate limiting we would need support for storing request counts by IP on the server and there is currently no storage support except for files.

@bgrgicak bgrgicak self-assigned this Mar 26, 2024
@bgrgicak bgrgicak requested a review from a team March 26, 2024 08:41
@adamziel adamziel merged commit 45bf169 into trunk Mar 26, 2024
5 checks passed
@adamziel adamziel deleted the add/logger-rate-limiting branch March 26, 2024 09:26
@adamziel adamziel changed the title Add rate limiting Logger API: Add rate limiting Mar 26, 2024
@brandonpayton
Copy link
Member

Because this is implemented using $_SESSION it can easily be worked around using curl or by removing session data.
To improve rate limiting we would need support for storing request counts by IP on the server and there is currently no storage support except for files.

Yeah... I guess we can revisit this with Systems if this is insufficient. It's more of a defense against unintentional overuse than it is against abuse. Still... very good to have.

$_SESSION[$rate_limit_key]++;

if ($_SESSION[$rate_limit_key] > $max_requests_per_hour) {
response(false, 'Too many requests');
Copy link
Member

Choose a reason for hiding this comment

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

would it be helpful to send a 429 here instead of what I presume is a 200?

also, these response() responses seem quite sparse. maybe we could help clue-in the browsers to what is happening?

header( 'Content-type: application/json', true, 429 );

Do we expect the clients to heed this response and wait to send new logs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This API is currently used only by one modal and it shows the same error message whenever a request fails, so it's not needed today.

If the API gets used more in the future, using the correct response code and improving errors would be good.

Do we expect the clients to heed this response and wait to send new logs?

No, users might click on the form again and if it fails, it would result in the same error.
Thinking about it now, the UX isn't great, but let's see how much it will be used before investing more time into it.

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.

Add rate limiting to the logger API
4 participants