T09 · Insecure Skill Coding Practices
- Location
scripts/work_mail_notifier.py:140- Finding
Email Metadata Is Stored Without Restrictive File Permissions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/work_mail_notifier.py, lines 140–169
Vulnerability Type: Plaintext sensitive-data storage with process-default file permissions
Risk Level: MediumVulnerable Code
python def save_state(state): os.makedirs(os.path.dirname(STATE_PATH), exist_ok=True) with open(STATE_PATH, "w", encoding="utf-8") as f: json.dump(state, f, ensure_ascii=False, indent=2) def save_anchor_ids(anchor_time: datetime, items): """保存上次通知中时间等于 anchor_time 的所有邮件 ID。""" anchor_ids = {item["id"] for item in items if item["date"] == anchor_time} os.makedirs(os.path.dirname(ANCHOR_IDS_PATH), exist_ok=True) with open(ANCHOR_IDS_PATH, "w", encoding="utf-8") as f: json.dump(list(anchor_ids), f, ensure_ascii=False) def save_notification_refs(items): os.makedirs(os.path.dirname(LAST_NOTIFICATION_FILE), exist_ok=True) groups = {"告警/风险": [], "失败/异常": [], "普通": []} for item in items: groups[classify_item(item)].append(item) ordered = [] for group_name in ["告警/风险", "失败/异常", "普通"]: for item in groups[group_name]: ordered.append({ "ref": f"{item['folder']}#{item['id']}", "folder": item["folder"], "id": item["id"], "from": item["from"], "subject": item["subject"], "date": item["date"].isoformat(), "group": group_name, }) with open(LAST_NOTIFICATION_FILE, "w", encoding="utf-8") as f: json.dump(ordered, f, ensure_ascii=False, indent=2)Technical Analysis
The notification reference file stores email senders, subjects, folder names, timestamps, and message identifiers in plaintext. The state and anchor files also retain mailbox-processing metadata. These files are opened without an explicit private creation mode, and the parent directory is created without explicitly enforcing owner-only permissions.
Consequ ...[truncated 2112 chars]
- Remediation
View remediation
Remediation Suggestions
- Ensure the data directory is owner-only before storing mailbox state:
python data_dir = os.path.dirname(LAST_NOTIFICATION_FILE) os.makedirs(data_dir, mode=0o700, exist_ok=True) os.chmod(data_dir, 0o700)- Create each sensitive file with mode
0600, rather than relying on the ambient umask. - Use atomic writes through a temporary file created in the same protected directory, apply mode
0600, flush and synchronize it, and then replace the destination withos.replace(). - Correct permissions on existing files with
os.chmod(path, 0o600)after validating that the destination is a regular file owned by the expected user. - Retain only metadata required by
mark_read.pyandshow_body.py; avoid persisting sender names and subject lines if they are not essential. - Validate ownership and reject symlinks or unexpected file types before reading or replacing persistent state files.
- Apply the same protection consistently to
work_mail_notifier_state.json,work_mail_anchor_ids.json, andlast_notification.json.
