-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/time/rate: Limiter depends on implementation-defined large float to int conversion #71154
Comments
Is this something that really needs to be fixed? rate = 1e-10 corresponds to a limit of 1 event every 317 years. I think it's more likely that you've misconfigured your program than that being a reasonable limit. |
We are developing an automated rate limiting system, that changes the limits dynamically, based on various factors. While we could (and probably will) add logic to prevent these small values escaping, it's a small enough fix to the library to guard against these scenarios, and would prevent any accidents occurring, both for us, and other folks using this rate limiter. As it stands, the library does nothing to prevent this configuration, and it results in the opposite of what you'd expect. |
Would you like to send a patch? |
I'm just getting it ready, will post it tomorrow. I accept it's pretty far out there, but solving it at this level would shut to door on a class of future bugs. |
https://go-review.googlesource.com/c/time/+/641335 - patch submitted |
Change https://go.dev/cl/641336 mentions this issue: |
Go version
go version go1.22.1 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
Playground
There is a conversion from float64 to int64 in
durationFromTokens
, when the final time.Duration is created:When limiters are configured with small values (1e-10) this overflows and allows request through that should be blocked.
amd64
does not saturate int64's when converting, where asarm64
does. Therefore this code is safe onarm64
, but notamd64
.Results of running:
GOARCH = "amd64", see "allow", "allow"
GOARCH = 'arm64', see "allow", "not allow"
This leads to the second "allow" on
amd64
. I'm preparing a PR with some overflow protection.What did you see happen?
When GOARCH = "amd64", see "allow", "allow"
Whe GOARCH = 'arm64', see "allow", "not allow"
What did you expect to see?
Regardless of GOARCH: "allow", "not allow"
The first attempt should succeed, due to the burst of 1, but the second should be denied.
The text was updated successfully, but these errors were encountered: