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

Prevent Override Max value by default value when passing 0 to Max #3288

Closed
wants to merge 5 commits into from

Conversation

ad3n
Copy link
Contributor

@ad3n ad3n commented Jan 23, 2025

Description

This PR makes this code https://github.com/gofiber/fiber/blob/main/middleware/limiter/limiter_fixed.go#L33 reachable, because when we supply the Max to 0, it always override by default value

@ad3n ad3n requested a review from a team as a code owner January 23, 2025 02:04
@ad3n ad3n requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team January 23, 2025 02:04
Copy link
Contributor

coderabbitai bot commented Jan 23, 2025

Walkthrough

The pull request modifies the configuration validation logic in the rate limiter middleware. Specifically, the change adjusts the condition for setting a default maximum request limit from checking if the value is less than or equal to zero to only checking if it is less than zero. This subtle modification means that zero will now be treated as a valid configuration value, potentially allowing more flexibility in rate limiting configuration.

Changes

File Change Summary
middleware/limiter/config.go Modified validation condition for Max field in configDefault function from cfg.Max <= 0 to cfg.Max < 0

Poem

🐰 A Limiter's Leap of Logic

Zero's no longer a barrier tight,
Where once limits dimmed the request's might
A rabbit's tweak, so subtle and keen
Lets configurations dance between
Rate limits now flow with newfound grace! 🚀

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.01%. Comparing base (8970f51) to head (9c350c3).

Files with missing lines Patch % Lines
middleware/limiter/config.go 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3288      +/-   ##
==========================================
- Coverage   84.04%   84.01%   -0.03%     
==========================================
  Files         116      116              
  Lines       11557    11557              
==========================================
- Hits         9713     9710       -3     
- Misses       1411     1413       +2     
- Partials      433      434       +1     
Flag Coverage Δ
unittests 84.01% <0.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
middleware/limiter/config.go (1)

96-97: Consider documenting this behavior change.

This change modifies how Max=0 is handled, which could affect existing users. Consider:

  1. Updating the field documentation to explicitly mention that Max=0 blocks all requests
  2. Adding a note in the changelog about this behavior change
  3. Providing migration guidance for users who might be relying on the old behavior
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8970f51 and 9c350c3.

📒 Files selected for processing (1)
  • middleware/limiter/config.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: repeated
🔇 Additional comments (1)
middleware/limiter/config.go (1)

96-97: LGTM! The change aligns with the PR objective.

The modification allows Max=0 to be a valid configuration value instead of being overridden by the default value. This is a logical change as it enables the rate limiter to completely block requests when desired.

Let's verify that the limiter middleware properly handles Max=0:

✅ Verification successful

Verified: Max=0 is properly handled in the limiter middleware

The code safely handles Max=0 by skipping rate limiting and allowing requests to pass through. This is:

  • Explicitly implemented in both fixed and sliding window limiters
  • Well-tested with dedicated test cases
  • Consistent with the PR objective
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check how Max=0 is handled in the limiter middleware implementation

# Search for usage of Max or MaxFunc in the limiter implementation
ast-grep --pattern 'MaxFunc($_)' || ast-grep --pattern 'Max($_)'

# Look for any existing tests that might need updating
rg -l "Max.*0" test_*

Length of output: 107


Script:

#!/bin/bash
# Find and analyze limiter-related files
echo "=== Limiter Files ==="
fd -t f limiter

echo -e "\n=== Max Usage in Implementation ==="
rg -n "Max" --type go -A 3 -B 3

echo -e "\n=== Test Cases ==="
fd -t f test -e go | xargs rg -l "Max.*[0-9]"

Length of output: 62890

@gaby
Copy link
Member

gaby commented Jan 23, 2025

@ad3n What's the point of setting this to 0?

@gaby
Copy link
Member

gaby commented Jan 23, 2025

You can use MaxFunc, to override the config.Max

https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L30

@ad3n
Copy link
Contributor Author

ad3n commented Jan 23, 2025

You can use MaxFunc, to override the config.Max

https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L30

The behavior when we set Max to 0 is confusing, i as developer imagine, when i put zero (for example, the value come from DB), i expected it would be skip the limiter, but actually the value overridden by default value (5)

@gaby
Copy link
Member

gaby commented Jan 23, 2025

You can use MaxFunc, to override the config.Max
https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L30

The behavior when we set Max to 0 is confusing, i as developer imagine, when i put zero (for example, the value come from DB), i expected it would be skip the limiter, but actually the value overridden by default value (5)

Why use the limiter then? It adds quite a bit of overhead.

@ad3n
Copy link
Contributor Author

ad3n commented Jan 23, 2025

You can use MaxFunc, to override the config.Max
https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L30

The behavior when we set Max to 0 is confusing, i as developer imagine, when i put zero (for example, the value come from DB), i expected it would be skip the limiter, but actually the value overridden by default value (5)

Why use the limiter then? It adds quite a bit of overhead.

The limiter is mandatory on my case, so i just make it simple to pass all to the limiter and then the limiter control the flow. As simple as that

@gaby
Copy link
Member

gaby commented Jan 23, 2025

You can use MaxFunc, to override the config.Max
https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L30

The behavior when we set Max to 0 is confusing, i as developer imagine, when i put zero (for example, the value come from DB), i expected it would be skip the limiter, but actually the value overridden by default value (5)

Why use the limiter then? It adds quite a bit of overhead.

The limiter is mandatory on my case, so i just make it simple to pass all to the limiter and then the limiter control the flow. As simple as that

If you set it to 0, the limiter is skipped and Next() is called. Basically disabling the limiter.

https://github.com/gofiber/fiber/blob/main/middleware/limiter/limiter_fixed.go#L33

@ad3n
Copy link
Contributor Author

ad3n commented Jan 23, 2025

You can use MaxFunc, to override the config.Max
https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L30

The behavior when we set Max to 0 is confusing, i as developer imagine, when i put zero (for example, the value come from DB), i expected it would be skip the limiter, but actually the value overridden by default value (5)

Why use the limiter then? It adds quite a bit of overhead.

The limiter is mandatory on my case, so i just make it simple to pass all to the limiter and then the limiter control the flow. As simple as that

If you set it to 0, the limiter is skipped and Next() is called. Basically disabling the limiter.

https://github.com/gofiber/fiber/blob/main/middleware/limiter/limiter_fixed.go#L33

Yup, i think the limiter will skipped, but on config, when i supply 0 on Max, it would be override by default value and change the Max to 5. That's the problem

So i create this PR to make sure, the default config not override it when i supply 0 on Max

Ref: https://github.com/gofiber/fiber/blob/main/middleware/limiter/config.go#L96

@ad3n ad3n changed the title Main Prevent Override Max value by default value when passing 0 to Max Jan 23, 2025
@ad3n ad3n changed the title Prevent Override Max value by default value when passing 0 to Max Prevent Override Max value by default value when passing 0 to Max Jan 23, 2025
@ReneWerner87
Copy link
Member

ReneWerner87 commented Jan 23, 2025

the default values of an empty structs always correspond to the default value of the datenTypes
for int this is 0, so we use this to set the default max value

in your pull request you set the condition for the max value to below 0, which makes these default value useless
also the middleware needs a max value to execute the functionality for which it was created

when it comes to skipping the middleware functionality, please use other solutions like using the Next method for the condition whether the middleware should be executed or not or the skip middleware or define the limiter middleware only on the routes where it is needed

@ad3n ad3n deleted the main branch January 23, 2025 08:08
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.

3 participants