Skip to content

Commit

Permalink
Fix proxy issue reported at slackapi#820 slackapi#821
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Sep 23, 2020
1 parent c723633 commit eb7b33e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
22 changes: 14 additions & 8 deletions slack/web/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import json
import logging
import mimetypes
import re
import urllib
import uuid
import warnings
from http.client import HTTPResponse
Expand All @@ -17,7 +17,7 @@
from typing import Optional, Union
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from urllib.request import Request, urlopen, OpenerDirector, ProxyHandler, HTTPSHandler

import aiohttp
from aiohttp import FormData, BasicAuth
Expand Down Expand Up @@ -455,20 +455,26 @@ def _perform_urllib_http_request(
# (BAN-B310)
if url.lower().startswith("http"):
req = Request(method="POST", url=url, data=body, headers=headers)
opener: Optional[OpenerDirector] = None
if self.proxy is not None:
if isinstance(self.proxy, str):
host = re.sub("^https?://", "", self.proxy)
req.set_proxy(host, "http")
req.set_proxy(host, "https")
opener = urllib.request.build_opener(
ProxyHandler({"http": self.proxy, "https": self.proxy}),
HTTPSHandler(context=self.ssl),
)
else:
raise SlackRequestError(
f"Invalid proxy detected: {self.proxy} must be a str value"
)

# NOTE: BAN-B310 is already checked above
resp: HTTPResponse = urlopen( # skipcq: BAN-B310
req, context=self.ssl, timeout=self.timeout
)
resp: Optional[HTTPResponse] = None
if opener:
resp = opener.open(req, timeout=self.timeout) # skipcq: BAN-B310
else:
resp = urlopen( # skipcq: BAN-B310
req, context=self.ssl, timeout=self.timeout
)
charset = resp.headers.get_content_charset()
body: str = resp.read().decode(charset) # read the response body here
return {"status": resp.code, "headers": resp.headers, "body": body}
Expand Down
27 changes: 19 additions & 8 deletions slack/webhook/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
import logging
import re
import urllib
from http.client import HTTPResponse
from ssl import SSLContext
from typing import Dict, Union, List, Optional
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from urllib.request import Request, urlopen, OpenerDirector, ProxyHandler, HTTPSHandler

from slack.errors import SlackRequestError
from .internal_utils import _build_body, _build_request_headers, _debug_log_response
Expand Down Expand Up @@ -105,22 +105,33 @@ def _perform_http_request(
)
try:
url = self.url
opener: Optional[OpenerDirector] = None
# for security (BAN-B310)
if url.lower().startswith("http"):
req = Request(
method="POST", url=url, data=body.encode("utf-8"), headers=headers
)
if self.proxy is not None:
host = re.sub("^https?://", "", self.proxy)
req.set_proxy(host, "http")
req.set_proxy(host, "https")
if isinstance(self.proxy, str):
opener = urllib.request.build_opener(
ProxyHandler({"http": self.proxy, "https": self.proxy}),
HTTPSHandler(context=self.ssl),
)
else:
raise SlackRequestError(
f"Invalid proxy detected: {self.proxy} must be a str value"
)
else:
raise SlackRequestError(f"Invalid URL detected: {url}")

# NOTE: BAN-B310 is already checked above
resp: HTTPResponse = urlopen( # skipcq: BAN-B310
req, context=self.ssl, timeout=self.timeout,
)
resp: Optional[HTTPResponse] = None
if opener:
resp = opener.open(req, timeout=self.timeout) # skipcq: BAN-B310
else:
resp = urlopen( # skipcq: BAN-B310
req, context=self.ssl, timeout=self.timeout
)
charset: str = resp.headers.get_content_charset() or "utf-8"
response_body: str = resp.read().decode(charset)
resp = WebhookResponse(
Expand Down

0 comments on commit eb7b33e

Please sign in to comment.