Skip to content

Refactor: Extract encryption/key management utilities from google_drive.py and settings.py #208

@techmore

Description

@techmore

Type

refactor

Severity

medium

Area

nmapui/google_drive.py, nmapui/settings.py

Description

Both google_drive.py and settings.py contain nearly identical implementations of:

  • _set_owner_only_permissions() — sets file to 0o600
  • _load_or_create_encryption_key() — creates/loads Fernet keys with atomic file operations

The implementations have the same structure but have drifted slightly. Bug fixes applied to one copy may not be applied to the other.

Additionally, both have a TOCTOU race condition in key creation: between the exists() check and read_bytes(), another process could delete or modify the key file. Two processes starting simultaneously could both generate different keys, making tokens encrypted with one key undecryptable.

Proposed Fix

  1. Create nmapui/crypto_utils.py with the shared implementations
  2. Use fcntl.flock or O_CREAT | O_EXCL for atomic key creation
  3. Have both google_drive.py and settings.py import from the shared module

Related Issues

#182 (Duplicate encryption helpers — closed, but apparently not fully resolved)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions