fix: strip non-cookie metadata from cookie file to prevent UnicodeEncodeError - #68
Open
cdxiaodong wants to merge 1 commit into
Open
fix: strip non-cookie metadata from cookie file to prevent UnicodeEncodeError#68cdxiaodong wants to merge 1 commit into
cdxiaodong wants to merge 1 commit into
Conversation
…odeError
load_saved_cookies() returned the entire JSON dict from the cookie file,
including any metadata fields (nickname, user_id, tags, etc.) that
external tools or multi-account managers may have stored alongside real
cookies. When such values contain non-ASCII characters (e.g. a Chinese
nickname), httpx crashes with UnicodeEncodeError when building the
Cookie header.
Fix: add _sanitize_cookie_dict() which filters entries on load:
- Strip known metadata keys (_META_COOKIE_KEYS)
- Drop entries whose key or value is not ASCII-safe
- Preserve saved_at for TTL-based refresh in get_cookies()
Repro before fix:
# cookies.json contains: {"a1":"abc", "nickname":"N8N中文"}
xhs hot
# UnicodeEncodeError: 'ascii' codec can't encode characters
After fix: non-cookie entries are silently dropped with a debug log.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
load_saved_cookies()returns the entire JSON dict from the cookie file, including any metadata fields (e.g.nickname,user_id,tags) that external tools or multi-account managers may have stored alongside real cookies.When such values contain non-ASCII characters (e.g. a Chinese
nicknamelike"N8N中国"),httpxcrashes withUnicodeEncodeErrorwhen building theCookieheader:Repro
This is easy to hit when managing multiple accounts — tools naturally annotate cookie files with metadata (which account is this, nickname, user_id, etc.), and the cookie file format is not documented as cookie-keys-only.
Fix
Add
_sanitize_cookie_dict()which filters entries on load:_META_COOKIE_KEYS:nickname,user_id,red_id,description,tags,added_at,slug,browser_source)saved_atfor TTL-based refresh inget_cookies()Non-cookie entries are dropped silently with a
logger.debug()message.Design notes
save_cookies()and the cookie file format are unchanged. Only loading is filtered.Tests
Added 3 test cases to
TestLoadSavedCookies:test_strips_non_cookie_metadatatest_strips_non_ascii_valuestest_cookies_to_string_ascii_safecookies_to_string()on loaded cookies is ASCII-safe (no crash)All 20 tests pass (17 existing + 3 new). Ruff check passes.