fix: use user-private cache directory to prevent cache poisoning on shared systems#564
Closed
yen0304 wants to merge 1 commit into
Closed
fix: use user-private cache directory to prevent cache poisoning on shared systems#564yen0304 wants to merge 1 commit into
yen0304 wants to merge 1 commit into
Conversation
…hared systems Fixes openai#500. The default cache directory was `tempfile.gettempdir() + "/data-gym-cache"` (typically `/tmp/data-gym-cache/` on Linux), which is world-writable and allows local attackers to pre-populate poisoned BPE data with predictable filenames. Changes: - Default cache to `~/.cache/tiktoken` (respects XDG_CACHE_HOME, LOCALAPPDATA on Windows) - Create cache directory with mode 0o700 (owner-only access) - Log a warning when loading cached files without hash verification Existing env vars (TIKTOKEN_CACHE_DIR, DATA_GYM_CACHE_DIR) continue to work as before. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
|
Closing as duplicate of #544 which addresses the same issue. Apologies for the duplicate — my search missed the existing PR due to different keywords. |
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.
Summary
Fixes #500. Addresses two security vulnerabilities in
tiktoken/load.py:Cache poisoning via shared temp directory (CWE-377/CWE-379): The default cache directory was
/tmp/data-gym-cache/, which is world-writable on multi-user Linux systems. An attacker could pre-populate poisoned BPE data with predictable filenames (sha1(url)), causing subsequent users to load attacker-controlled tokenizer data.Silent cache loading without hash verification (CWE-345): When
expected_hashisNone(e.g., custom encodings), cached files were loaded without any integrity check or warning.Changes
tempfile.gettempdir() + "/data-gym-cache"to a user-private location:$XDG_CACHE_HOME/tiktokenor~/.cache/tiktoken%LOCALAPPDATA%\tiktoken\cache0o700(owner-only) on creationTIKTOKEN_CACHE_DIRandDATA_GYM_CACHE_DIRenv vars continue to work as beforeWhy not migrate the old cache?
Files in
/tmp/data-gym-cache/could already be poisoned, so we intentionally do not copy them. Users will re-download encodings once into the secure location.Test plan
tests/test_cache_security.pywith 8 tests covering:/tmptest_misc.py,test_simple_public.py,test_helpers.py)tiktoken.get_encoding('cl100k_base')works correctly with new cache location