T09 · Insecure Skill Coding Practices
- Location
scripts/home_storage.py:36- Finding
Household inventory and backup files are created without restrictive permissions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/home_storage.py:36-39,scripts/home_storage.py:664-680,scripts/home_storage.py:705-709,scripts/home_storage.py:726-729
Vulnerability Type: Sensitive data stored with process-default filesystem permissions
Risk Level: MediumVulnerable Code
Main inventory storage at
scripts/home_storage.py:36-39:python def save_data(data: dict) -> None: """保存数据""" os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True) with open(DATA_FILE, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2)Backup creation at
scripts/home_storage.py:664-680:python def cmd_backup() -> str: """备份数据""" data = load_data() if not data.get("suites"): return "❌ 当前没有数据,无需备份" os.makedirs(BACKUP_DIR, exist_ok=True) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") backup_file = os.path.join(BACKUP_DIR, f"home_storage_{timestamp}.json") with open(backup_file, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) backups = [f for f in os.listdir(BACKUP_DIR) if f.startswith("home_storage_") and f.endswith(".json")] return f"✅ 备份成功!\n📁 文件:{backup_file}\n📊 当前共有 {len(backups)} 个备份"Pre-restore backup creation at
scripts/home_storage.py:705-709:python current = load_data() if current.get("suites"): pre_backup = os.path.join(BACKUP_DIR, f"pre_restore_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json") with open(pre_backup, 'w', encoding='utf-8') as f: json.dump(current, f, ensure_ascii=False, indent=2)JSON export creation at
scripts/home_storage.py:726-729:python export_file = os.path.join(BACKUP_DIR, f"export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json") with open(export_file, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent ...[truncated 2553 chars]- Remediation
View remediation
Remediation Suggestions
-
Create the data and backup directories with owner-only permissions and correct existing directory modes:
python DATA_FILE.parent.mkdir(parents=True, exist_ok=True, mode=0o700) BACKUP_DIR.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(DATA_FILE.parent, 0o700) os.chmod(BACKUP_DIR, 0o700) -
Create new files explicitly with mode
0600rather than relying on the process umask:python fd = os.open( path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600, ) with os.fdopen(fd, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) -
Apply
os.chmod(path, 0o600)to existing main, backup, pre-restore, and export files because opening an existing file does not necessarily replace its prior permissions. -
Centralize JSON writing in one secure helper and use it for
save_data(),cmd_backup(),cmd_restore(), andcmd_export()so every inventory copy receives the same protection. -
Prefer atomic writes: write to an owner-only temporary file in the same protected directory, flush and synchronize it, then replace the destination with
os.replace(). -
Add automated tests that run under a permissive umask and verify that all generated directories have mode
0700and all generated inventory files have mode0600.
-
