Answer security round 1: 0 🔴, 0 🟡 (0 misread); follow no redirect, so the token reaches only the server named
CI / tests (pull_request) Successful in 2m22s
CI / tests (pull_request) Successful in 2m22s
This commit is contained in:
+13
-2
@@ -17,6 +17,16 @@ TIMEOUT = 10
|
||||
Http = typing.Callable[[str, str, str, object], object]
|
||||
|
||||
|
||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
"""Follows no redirect: urllib resends the token to the new location, even one off https, and Mattermost's API answers without one."""
|
||||
|
||||
def redirect_request(self, *args: typing.Any, **kwargs: typing.Any) -> None:
|
||||
return None
|
||||
|
||||
|
||||
OPENER = urllib.request.build_opener(NoRedirect)
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Config:
|
||||
url: str
|
||||
@@ -89,10 +99,11 @@ def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
data = None if body is None else json.dumps(body).encode()
|
||||
request = urllib.request.Request(url, data, {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=TIMEOUT) as response:
|
||||
with OPENER.open(request, timeout=TIMEOUT) as response:
|
||||
raw = response.read()
|
||||
except urllib.error.HTTPError as failure:
|
||||
return Failure(f"{request_name} answered {failure.code}: {error_message(failure.read()) or failure.reason}")
|
||||
with failure:
|
||||
return Failure(f"{request_name} answered {failure.code}: {error_message(failure.read()) or failure.reason}")
|
||||
except (OSError, ValueError) as failure:
|
||||
server = urllib.parse.urlunsplit(urllib.parse.urlsplit(url)[:2] + ("", "", ""))
|
||||
return Failure(f"cannot reach {server}: {getattr(failure, 'reason', failure)}")
|
||||
|
||||
Reference in New Issue
Block a user