- Notifications
You must be signed in to change notification settings - Fork673
fix: raise error if there is a 301/302 redirection#1486
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
nejch merged 1 commit intopython-gitlab:masterfromJohnVillalovos:jlvillal/prohibit_redirectionSep 8, 2021
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
11 changes: 10 additions & 1 deletiondocs/api-usage.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletiondocs/cli-usage.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
44 changes: 25 additions & 19 deletionsgitlab/client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -29,8 +29,9 @@ | ||
from gitlab import utils | ||
REDIRECT_MSG = ( | ||
"python-gitlab detected a {status_code} ({reason!r}) redirection. You must update " | ||
"your GitLab URL to the correct URL to avoid issues. The redirection was from: " | ||
"{source!r} to {target!r}" | ||
JohnVillalovos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
) | ||
@@ -456,24 +457,29 @@ def _build_url(self, path: str) -> str: | ||
return "%s%s" % (self._url, path) | ||
def _check_redirects(self, result: requests.Response) -> None: | ||
# Check the requests history to detect 301/302 redirections. | ||
# If the initial verb is POST or PUT, the redirected request will use a | ||
# GET request, leading to unwanted behaviour. | ||
# If we detect a redirection with a POST or a PUT request, we | ||
# raise an exception with a useful error message. | ||
if not result.history: | ||
return | ||
for item in result.history: | ||
if item.status_code not in (301, 302): | ||
continue | ||
# GET methods can be redirected without issue | ||
if item.request.method == "GET": | ||
continue | ||
target = item.headers.get("location") | ||
raise gitlab.exceptions.RedirectError( | ||
REDIRECT_MSG.format( | ||
status_code=item.status_code, | ||
reason=item.reason, | ||
source=item.url, | ||
target=target, | ||
) | ||
) | ||
def _prepare_send_data( | ||
self, | ||
91 changes: 89 additions & 2 deletionstests/unit/test_gitlab_http_methods.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,7 +2,7 @@ | ||
import requests | ||
from httmock import HTTMock, response, urlmatch | ||
from gitlab import GitlabHttpError, GitlabList, GitlabParsingError, RedirectError | ||
def test_build_url(gl): | ||
@@ -123,9 +123,96 @@ def resp_cont(url, request): | ||
assert call_count == 1 | ||
def create_redirect_response( | ||
*, request: requests.models.PreparedRequest, http_method: str, api_path: str | ||
) -> requests.models.Response: | ||
"""Create a Requests response object that has a redirect in it""" | ||
assert api_path.startswith("/") | ||
http_method = http_method.upper() | ||
# Create a history which contains our original request which is redirected | ||
history = [ | ||
response( | ||
status_code=302, | ||
content="", | ||
headers={"Location": f"http://example.com/api/v4{api_path}"}, | ||
reason="Moved Temporarily", | ||
request=request, | ||
) | ||
] | ||
# Create a "prepped" Request object to be the final redirect. The redirect | ||
# will be a "GET" method as Requests changes the method to "GET" when there | ||
# is a 301/302 redirect code. | ||
req = requests.Request( | ||
method="GET", | ||
url=f"http://example.com/api/v4{api_path}", | ||
) | ||
prepped = req.prepare() | ||
resp_obj = response( | ||
status_code=200, | ||
content="", | ||
headers={}, | ||
reason="OK", | ||
elapsed=5, | ||
request=prepped, | ||
) | ||
resp_obj.history = history | ||
return resp_obj | ||
def test_http_request_302_get_does_not_raise(gl): | ||
"""Test to show that a redirect of a GET will not cause an error""" | ||
method = "get" | ||
api_path = "/user/status" | ||
@urlmatch( | ||
scheme="http", netloc="localhost", path=f"/api/v4{api_path}", method=method | ||
) | ||
def resp_cont( | ||
url: str, request: requests.models.PreparedRequest | ||
) -> requests.models.Response: | ||
resp_obj = create_redirect_response( | ||
request=request, http_method=method, api_path=api_path | ||
) | ||
return resp_obj | ||
with HTTMock(resp_cont): | ||
gl.http_request(verb=method, path=api_path) | ||
def test_http_request_302_put_raises_redirect_error(gl): | ||
"""Test to show that a redirect of a PUT will cause an error""" | ||
method = "put" | ||
api_path = "/user/status" | ||
@urlmatch( | ||
scheme="http", netloc="localhost", path=f"/api/v4{api_path}", method=method | ||
) | ||
def resp_cont( | ||
url: str, request: requests.models.PreparedRequest | ||
) -> requests.models.Response: | ||
resp_obj = create_redirect_response( | ||
request=request, http_method=method, api_path=api_path | ||
) | ||
JohnVillalovos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return resp_obj | ||
with HTTMock(resp_cont): | ||
with pytest.raises(RedirectError) as exc: | ||
gl.http_request(verb=method, path=api_path) | ||
error_message = exc.value.error_message | ||
assert "Moved Temporarily" in error_message | ||
assert "http://localhost/api/v4/user/status" in error_message | ||
assert "http://example.com/api/v4/user/status" in error_message | ||
def test_get_request(gl): | ||
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", method="get") | ||
def resp_cont(url: str, request: requests.models.PreparedRequest): | ||
headers = {"content-type": "application/json"} | ||
content = '{"name": "project1"}' | ||
return response(200, content, headers, None, 5, request) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.