-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
region is sometimes user-controlled and allows for redirect attacks in order to steal API keys. This is no longer possible.
- Loading branch information
1 parent
368cd2c
commit 8087ff6
Showing
17 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class IllegalArgumentError(RuntimeError): | ||
def __init__( | ||
self, argument_name: str, argument_value: str, extra_message: str = None | ||
): | ||
message = ( | ||
f"Illegal value provided for argument '{argument_name}': '{argument_value}'" | ||
) | ||
if extra_message: | ||
message += f" - {extra_message}" | ||
|
||
super().__init__(message) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import re | ||
|
||
from . import IllegalArgumentError, RequestHandler | ||
|
||
|
||
class SanitationHandler(RequestHandler): | ||
""" | ||
The SanitationHandler class provides some basic sanity checks to parameters to | ||
ensure safe usage. | ||
Only check as of now is ensuring that region doesn't cause HTTP requests to unknown | ||
servers, which would allow a malicious user to steal API keys. | ||
""" | ||
|
||
def __init__(self): | ||
self._region_expr = re.compile("[a-zA-Z0-9]+") | ||
|
||
def preview_request( | ||
self, | ||
region: str, | ||
endpoint_name: str, | ||
method_name: str, | ||
url: str, | ||
query_params: dict, | ||
): | ||
""" | ||
called before a request is processed. | ||
:param string endpoint_name: the name of the endpoint being requested | ||
:param string method_name: the name of the method being requested | ||
:param url: the URL that is being requested. | ||
:param query_params: dict: the parameters to the url that is being queried, | ||
e.g. ?key1=val&key2=val2 | ||
""" | ||
region_ok = self._region_expr.fullmatch(region) | ||
if region_ok is None: | ||
raise IllegalArgumentError("region", region) |
This file contains 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from .IllegalArgumentError import IllegalArgumentError | ||
from .RequestHandler import RequestHandler | ||
|
||
from .DeprecationHandler import DeprecationHandler | ||
from .DeserializerAdapter import DeserializerAdapter | ||
from .DictionaryDeserializer import DictionaryDeserializer | ||
from .RateLimiterAdapter import RateLimiterAdapter | ||
from .SanitationHandler import SanitationHandler | ||
from .TypeCorrectorHandler import TypeCorrectorHandler | ||
from .ThrowOnErrorHandler import ApiError, ThrowOnErrorHandler |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import requests | ||
|
||
from .Handlers import ApiError as _ApiError | ||
from .Handlers import ( | ||
ApiError as _ApiError, | ||
IllegalArgumentError as _IllegalArgumentError, | ||
) | ||
|
||
ApiError = _ApiError # should silence code analysis warning | ||
IllegalArgumentError = _IllegalArgumentError | ||
TimeoutError = requests.exceptions.Timeout |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
|
||
from riotwatcher.Handlers import IllegalArgumentError, SanitationHandler | ||
|
||
|
||
@pytest.mark.common | ||
@pytest.mark.unit | ||
class TestSanitationHandler: | ||
@pytest.mark.parametrize( | ||
"region", ["na1", "Na1", "aN1", "enue", "americas", "europe", "euw1"] | ||
) | ||
def test_valid_region_passes(self, region): | ||
handler = SanitationHandler() | ||
|
||
handler.preview_request(region, None, None, None, None) | ||
|
||
@pytest.mark.parametrize("region", ["", "google.com/?stolen=", "+", "na1?"]) | ||
def test_invalid_region_fails(self, region): | ||
handler = SanitationHandler() | ||
|
||
with pytest.raises(IllegalArgumentError): | ||
handler.preview_request(region, None, None, None, None) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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