Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
59c7d67c
Commit
59c7d67c
authored
Nov 01, 2021
by
Ash McKenzie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raise InvalidKeyError if key is unknown/invalid
parent
2064506c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
1 deletion
+33
-1
lib/gitlab/application_rate_limiter.rb
lib/gitlab/application_rate_limiter.rb
+3
-1
spec/lib/gitlab/application_rate_limiter_spec.rb
spec/lib/gitlab/application_rate_limiter_spec.rb
+30
-0
No files found.
lib/gitlab/application_rate_limiter.rb
View file @
59c7d67c
...
...
@@ -11,6 +11,8 @@ module Gitlab
# redirect_to(edit_project_path(@project), status: :too_many_requests)
# end
class
ApplicationRateLimiter
InvalidKeyError
=
Class
.
new
(
StandardError
)
def
initialize
(
key
,
**
options
)
@key
=
key
@options
=
options
...
...
@@ -69,7 +71,7 @@ module Gitlab
#
# @return [Boolean] Whether or not a request should be throttled
def
throttled?
(
key
,
**
options
)
r
eturn
unless
rate_limits
[
key
]
r
aise
InvalidKeyError
unless
rate_limits
[
key
]
return
if
scoped_user_in_allowlist?
(
options
)
...
...
spec/lib/gitlab/application_rate_limiter_spec.rb
View file @
59c7d67c
...
...
@@ -50,6 +50,36 @@ RSpec.describe Gitlab::ApplicationRateLimiter do
end
describe
'.throttled?'
do
context
'when the key is invalid'
do
context
'is provided as a Symbol'
do
context
'but is not defined in the rate_limits Hash'
do
it
'raises an InvalidKeyError exception'
do
key
=
:key_not_in_rate_limits_hash
expect
{
subject
.
throttled?
(
key
)
}.
to
raise_error
(
Gitlab
::
ApplicationRateLimiter
::
InvalidKeyError
)
end
end
end
context
'is provided as a String'
do
context
'and is a String representation of an existing key in rate_limits Hash'
do
it
'raises an InvalidKeyError exception'
do
key
=
rate_limits
.
keys
[
0
].
to_s
expect
{
subject
.
throttled?
(
key
)
}.
to
raise_error
(
Gitlab
::
ApplicationRateLimiter
::
InvalidKeyError
)
end
end
context
'but is not defined in any form the rate_limits Hash'
do
it
'raises an InvalidKeyError exception'
do
key
=
'key_not_in_rate_limits_hash'
expect
{
subject
.
throttled?
(
key
)
}.
to
raise_error
(
Gitlab
::
ApplicationRateLimiter
::
InvalidKeyError
)
end
end
end
end
context
'when the key is an array of only ActiveRecord models'
do
let
(
:scope
)
{
[
user
,
project
]
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment