T09 · Insecure Skill Coding Practices
- Location
lib/token_manager.py:45- Finding
Predictable Shared Temporary Cache Path Can Expose Access Tokens
- Content
View full analysis
Vulnerability Details
File Location:
lib/token_manager.py, lines 45-51 and 97-110
Vulnerability Type: Unsafe temporary file and directory handling
Risk Level: HighVulnerable Code:
python def get_cache_dir(): """Get global cache directory path.""" base_temp = tempfile.gettempdir() cache_dir = os.path.join(base_temp, CACHE_DIR_NAME) os.makedirs(cache_dir, exist_ok=True) return cache_dirpython def save_token_cache(cache_data): """ Save all tokens to global cache file (atomic write). Args: cache_data: Dict of all cached tokens """ cache_file = get_cache_file_path() cache_dir = get_cache_dir() try: # Write to temp file first, then rename (atomic operation) temp_file = cache_file + ".tmp" with open(temp_file, 'w') as f: json.dump(cache_data, f, indent=2) os.replace(temp_file, cache_file) # Set file permissions (readable only by owner) os.chmod(cache_file, 0o600) return TrueTechnical Analysis
The token cache is placed in the predictable shared path
/tmp/ezviz_global_token_cache/. The implementation creates this directory withexist_ok=Truebut does not verify that it is a real directory, that it is owned by the current user, or that it has restrictive permissions.The temporary filename,
global_token_cache.json.tmp, is also predictable. Opening it with the ordinaryopen(..., 'w')interface follows a pre-existing symbolic link. The intended0600permissions are applied only after the temporary file has been written and renamed. Consequently:- An attacker may pre-create the cache directory with insecure permissions.
- An attacker may pre-create the predictable temporary path as a symbolic link.
- The temporary file may initially inherit a permissive process umask.
- Sen ...[truncated 1905 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer a per-user runtime directory such as
$XDG_RUNTIME_DIRinstead of a globally predictable path under/tmp. - Create the cache directory with mode
0700and verify that:- It is a directory rather than a symbolic link.
- It is owned by the effective user.
- It is not writable by group or other users.
- Create temporary files using
tempfile.mkstemp(dir=cache_dir)ortempfile.NamedTemporaryFile(dir=cache_dir, delete=False). - Set mode
0600at creation time, before writing token data. - Where supported, use
os.open()withO_CREAT,O_EXCL, andO_NOFOLLOWto prevent link-following and filename-reuse attacks. - Validate the final cache file with
os.lstat()before reading it, rejecting symbolic links, unexpected owners, and unsafe permission modes. - Flush and
fsync()the temporary file before atomic replacement when cache durability is required. - Disable token caching by default in multi-user environments or use an operating-system credential store.
- Prefer a per-user runtime directory such as
