Skip to content

Commit 2fcd8da

Browse files
committed
Do not send credentials by default, make it explicit when patching
1 parent 4ad0640 commit 2fcd8da

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

pyodide_http/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__version__ = "0.2.2"
99

1010

11-
def patch_requests(continue_on_import_error: bool = False):
11+
def patch_requests(continue_on_import_error: bool = False, with_credentials: bool = False):
1212
if not _SHOULD_PATCH:
1313
return
1414
try:
@@ -18,10 +18,10 @@ def patch_requests(continue_on_import_error: bool = False):
1818
return
1919
raise
2020
else:
21-
patch()
21+
patch(with_credentials)
2222

2323

24-
def patch_urllib(continue_on_import_error: bool = False):
24+
def patch_urllib(continue_on_import_error: bool = False, with_credentials: bool = False):
2525
if not _SHOULD_PATCH:
2626
return
2727

@@ -32,13 +32,13 @@ def patch_urllib(continue_on_import_error: bool = False):
3232
return
3333
raise
3434
else:
35-
patch()
35+
patch(with_credentials)
3636

3737

3838
def should_patch():
3939
return _SHOULD_PATCH
4040

4141

42-
def patch_all():
43-
patch_requests(continue_on_import_error=True)
44-
patch_urllib(continue_on_import_error=True)
42+
def patch_all(with_credentials: bool = False):
43+
patch_requests(continue_on_import_error=True, with_credentials=with_credentials)
44+
patch_urllib(continue_on_import_error=True, with_credentials=with_credentials)

pyodide_http/_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def show_streaming_warning():
7272
)
7373

7474

75-
def send(request: Request, stream: bool = False) -> Response:
75+
def send(request: Request, stream: bool = False, with_credentials: bool = False) -> Response:
7676
if request.params:
7777
from js import URLSearchParams
7878

@@ -118,7 +118,7 @@ def send(request: Request, stream: bool = False) -> Response:
118118
if name.lower() not in HEADERS_TO_IGNORE:
119119
xhr.setRequestHeader(name, value)
120120

121-
xhr.withCredentials = True
121+
xhr.withCredentials = with_credentials
122122
xhr.send(to_js(request.body))
123123

124124
headers = dict(Parser().parsestr(xhr.getAllResponseHeaders()))

pyodide_http/_requests.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
class PyodideHTTPAdapter(BaseAdapter):
1313
"""The Base Transport Adapter"""
1414

15-
def __init__(self):
15+
def __init__(self, with_credentials=False):
1616
super().__init__()
17+
self._with_credentials = with_credentials
1718

1819
def send(self, request, **kwargs):
1920
"""Sends PreparedRequest object. Returns Response object.
@@ -46,7 +47,7 @@ def send(self, request, **kwargs):
4647
if request.body:
4748
pyodide_request.set_body(request.body)
4849
try:
49-
resp = send(pyodide_request, stream)
50+
resp = send(pyodide_request, stream, self._with_credentials)
5051
except _StreamingTimeout:
5152
from requests import ConnectTimeout
5253

@@ -88,7 +89,7 @@ def close(self):
8889
pass
8990

9091

91-
def patch():
92+
def patch(with_credentials=False):
9293
global _IS_PATCHED
9394
"""
9495
Patch the requests Session. Will add a new adapter for the http and https protocols.
@@ -105,8 +106,8 @@ def patch():
105106

106107
def new_init(self):
107108
self._old_init()
108-
self.mount("https://", PyodideHTTPAdapter())
109-
self.mount("http://", PyodideHTTPAdapter())
109+
self.mount("https://", PyodideHTTPAdapter(with_credentials))
110+
self.mount("http://", PyodideHTTPAdapter(with_credentials))
110111

111112
requests.sessions.Session._old_init = requests.sessions.Session.__init__
112113
requests.sessions.Session.__init__ = new_init

pyodide_http/_urllib.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from io import BytesIO
2+
from functools import partial
23

34
import urllib.request
45
from http.client import HTTPResponse
@@ -28,7 +29,7 @@ def urlopen(url, *args, **kwargs):
2829
url = url.full_url
2930

3031
request = Request(method, url, headers=headers, body=data)
31-
resp = send(request)
32+
resp = send(request, with_credentials=kwargs.get("with_credentials", False))
3233

3334
# Build a fake http response
3435
# Strip out the content-length header. When Content-Encoding is 'gzip' (or other
@@ -59,13 +60,16 @@ def urlopen_self_removed(self, url, *args, **kwargs):
5960
return urlopen(url, *args, **kwargs)
6061

6162

62-
def patch():
63+
def patch(with_credentials=False):
6364
global _IS_PATCHED
6465

6566
if _IS_PATCHED:
6667
return
6768

68-
urllib.request.urlopen = urlopen
69-
urllib.request.OpenerDirector.open = urlopen_self_removed
69+
_urlopen = partial(urlopen, with_credentials=with_credentials)
70+
_open = partial(urlopen_self_removed, with_credentials=with_credentials)
71+
72+
urllib.request.urlopen = _urlopen
73+
urllib.request.OpenerDirector.open = _open
7074

7175
_IS_PATCHED = True

0 commit comments

Comments
 (0)