Skip to content

Commit 32d1ee1

Browse files
committed
Polish
1 parent ed2c2da commit 32d1ee1

3 files changed

Lines changed: 11 additions & 37 deletions

File tree

impit-python/src/cookies.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@ impl CookieStore for PythonCookieJar {
9191

9292
kwargs.set_item("rest", rest).unwrap_or_default();
9393

94-
// A hostile or buggy server can send a `Set-Cookie` that
95-
// `http.cookiejar.Cookie` rejects (e.g. a bad domain/expiry), and a custom
96-
// cookie jar's `set_cookie` may raise too. Skip the offending cookie instead
97-
// of `.unwrap()`ing: a panic here would unwind across the FFI boundary and
98-
// abort the host process rather than raise a catchable Python exception.
99-
// Cookie parsing errors are ignored silently, matching the Node binding's
100-
// `setCookie` handling in `index.wrapper.js`.
94+
// Cookie parsing errors are ignored silently.
10195
let py_cookie = match self.cookie_constructor.call(py, (), Some(&kwargs)) {
10296
Ok(py_cookie) => py_cookie,
10397
Err(_) => continue,

impit-python/test/async_client_test.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,18 @@ async def test_complex_cookies(self, browser: Browser) -> None:
193193
assert cookie.domain == '127.0.0.1'
194194

195195
@pytest.mark.asyncio
196-
async def test_malformed_cookie_is_skipped_not_crashing(self, browser: Browser) -> None:
197-
"""A cookie the jar rejects is skipped instead of aborting the process.
198-
199-
Regression test for https://github.com/apify/impit/issues/478: `set_cookies`
200-
runs inside reqwest's cookie-store callback, so a raising `set_cookie` used to
201-
`.unwrap()` into a Rust panic that unwound across the FFI boundary and aborted
202-
the host process. It must now be caught and the offending cookie skipped, while
203-
valid cookies in the same response are still stored.
204-
"""
196+
async def test_rejected_cookie_is_skipped_not_crashing(self, browser: Browser) -> None:
197+
"""A cookie jar rejects are skipped instead of aborting the process."""
205198

206199
class RejectingCookieJar(CookieJar):
207200
def set_cookie(self, cookie: Cookie) -> None:
208201
if cookie.name == 'bad':
209-
raise ValueError('cookie jar rejects this cookie')
202+
raise ValueError('simulate parsing error')
210203
super().set_cookie(cookie)
211204

212205
cookies_jar = RejectingCookieJar()
213206

214-
impit = AsyncClient(browser=browser, cookie_jar=cookies_jar, follow_redirects=True)
207+
impit = AsyncClient(browser=browser, cookie_jar=RejectingCookieJar(), follow_redirects=True)
215208

216209
url = get_httpbin_url(
217210
'/response-headers',
@@ -223,13 +216,10 @@ def set_cookie(self, cookie: Cookie) -> None:
223216
},
224217
)
225218

226-
# The request must return normally rather than aborting the interpreter.
227219
response = await impit.get(url)
228220
assert response.status_code == 200
229221

230-
names = {cookie.name for cookie in cookies_jar}
231-
assert 'bad' not in names # the rejected cookie was skipped
232-
assert 'good' in names # a valid cookie in the same response is still stored
222+
assert {'good'} == {cookie.name for cookie in cookies_jar}
233223

234224
@pytest.mark.asyncio
235225
async def test_cookie_jar_works(self, browser: Browser) -> None:

impit-python/test/basic_client_test.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,18 @@ def test_complex_cookies(self, browser: Browser) -> None:
203203
# but it's ok - https://www.rfc-editor.org/rfc/rfc6265#section-4.1.2.3
204204
assert cookie.domain == '127.0.0.1'
205205

206-
def test_malformed_cookie_is_skipped_not_crashing(self, browser: Browser) -> None:
207-
"""A cookie the jar rejects is skipped instead of aborting the process.
208-
209-
Regression test for https://github.com/apify/impit/issues/478: `set_cookies`
210-
runs inside reqwest's cookie-store callback, so a raising `set_cookie` used to
211-
`.unwrap()` into a Rust panic that unwound across the FFI boundary and aborted
212-
the host process. It must now be caught and the offending cookie skipped, while
213-
valid cookies in the same response are still stored.
214-
"""
206+
def test_rejected_cookie_is_skipped_not_crashing(self, browser: Browser) -> None:
207+
"""A cookie jar rejects are skipped instead of aborting the process."""
215208

216209
class RejectingCookieJar(CookieJar):
217210
def set_cookie(self, cookie: Cookie) -> None:
218211
if cookie.name == 'bad':
219-
raise ValueError('cookie jar rejects this cookie')
212+
raise ValueError('simulate parsing error')
220213
super().set_cookie(cookie)
221214

222215
cookies_jar = RejectingCookieJar()
223216

224-
impit = Client(browser=browser, cookie_jar=cookies_jar, follow_redirects=True)
217+
impit = Client(browser=browser, cookie_jar=RejectingCookieJar(), follow_redirects=True)
225218

226219
url = get_httpbin_url(
227220
'/response-headers',
@@ -233,13 +226,10 @@ def set_cookie(self, cookie: Cookie) -> None:
233226
},
234227
)
235228

236-
# The request must return normally rather than aborting the interpreter.
237229
response = impit.get(url)
238230
assert response.status_code == 200
239231

240-
names = {cookie.name for cookie in cookies_jar}
241-
assert 'bad' not in names # the rejected cookie was skipped
242-
assert 'good' in names # a valid cookie in the same response is still stored
232+
assert {'good'} == {cookie.name for cookie in cookies_jar}
243233

244234
def test_cookie_jar_works(self, browser: Browser) -> None:
245235
cookies = Cookies({'preset-cookie': '123'})

0 commit comments

Comments
 (0)