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
ab44bab8
Commit
ab44bab8
authored
Jan 27, 2020
by
Aishwarya Subramanian
Committed by
Jan Provaznik
Jan 27, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add rate limiter to import api
parent
bbe778f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
lib/api/project_import.rb
lib/api/project_import.rb
+16
-0
spec/requests/api/project_import_spec.rb
spec/requests/api/project_import_spec.rb
+13
-0
No files found.
lib/api/project_import.rb
View file @
ab44bab8
...
@@ -18,6 +18,14 @@ module API
...
@@ -18,6 +18,14 @@ module API
def
validate_file!
def
validate_file!
render_api_error!
(
'The file is invalid'
,
400
)
unless
file_is_valid?
render_api_error!
(
'The file is invalid'
,
400
)
unless
file_is_valid?
end
end
def
throttled?
(
key
,
scope
)
rate_limiter
.
throttled?
(
key
,
scope:
scope
)
end
def
rate_limiter
::
Gitlab
::
ApplicationRateLimiter
end
end
end
before
do
before
do
...
@@ -43,6 +51,14 @@ module API
...
@@ -43,6 +51,14 @@ module API
success
Entities
::
ProjectImportStatus
success
Entities
::
ProjectImportStatus
end
end
post
'import'
do
post
'import'
do
key
=
"project_import"
.
to_sym
if
throttled?
(
key
,
[
current_user
,
key
])
rate_limiter
.
log_request
(
request
,
"
#{
key
}
_request_limit"
.
to_sym
,
current_user
)
render_api_error!
({
error:
_
(
'This endpoint has been requested too many times. Try again later.'
)
},
429
)
end
validate_file!
validate_file!
Gitlab
::
QueryLimiting
.
whitelist
(
'https://gitlab.com/gitlab-org/gitlab-foss/issues/42437'
)
Gitlab
::
QueryLimiting
.
whitelist
(
'https://gitlab.com/gitlab-org/gitlab-foss/issues/42437'
)
...
...
spec/requests/api/project_import_spec.rb
View file @
ab44bab8
...
@@ -196,6 +196,19 @@ describe API::ProjectImport do
...
@@ -196,6 +196,19 @@ describe API::ProjectImport do
end
end
end
end
context
'when request exceeds the rate limit'
do
before
do
allow
(
::
Gitlab
::
ApplicationRateLimiter
).
to
receive
(
:throttled?
).
and_return
(
true
)
end
it
'prevents users from importing projects'
do
post
api
(
'/projects/import'
,
user
),
params:
{
path:
'test-import'
,
file:
fixture_file_upload
(
file
),
namespace:
namespace
.
id
}
expect
(
response
).
to
have_gitlab_http_status
(
429
)
expect
(
json_response
[
'message'
][
'error'
]).
to
eq
(
'This endpoint has been requested too many times. Try again later.'
)
end
end
def
stub_import
(
namespace
)
def
stub_import
(
namespace
)
expect_any_instance_of
(
ProjectImportState
).
to
receive
(
:schedule
)
expect_any_instance_of
(
ProjectImportState
).
to
receive
(
:schedule
)
expect
(
::
Projects
::
CreateService
).
to
receive
(
:new
).
with
(
user
,
hash_including
(
namespace_id:
namespace
.
id
)).
and_call_original
expect
(
::
Projects
::
CreateService
).
to
receive
(
:new
).
with
(
user
,
hash_including
(
namespace_id:
namespace
.
id
)).
and_call_original
...
...
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