Skip to content

fix(config): restrict censys.cfg to owner-only permissions - #720

Merged
Aidan Holland (thehappydinoa) merged 2 commits into
mainfrom
fix-config-perms
Aug 27, 2026
Merged

fix(config): restrict censys.cfg to owner-only permissions#720
Aidan Holland (thehappydinoa) merged 2 commits into
mainfrom
fix-config-perms

Conversation

@btschwartz12

@btschwartz12 Ben Schwartz (btschwartz12) commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Description

write_config() created ~/.config/censys and wrote censys.cfg without an explicit mode, so permissions were governed solely by the process umask. Under the common default umask of 022, the config file — which holds api_id, api_secret, and asm_api_key in plaintext — ended up world-readable (0644) and the directory world-traversable (0755). On shared hosts, any local unprivileged user could read the victim's live API credentials (CWE-276, Incorrect Default Permissions).

This change hardens write_config() so it no longer inherits the ambient umask:

  • The config directory is created with mode 0700 (and chmod'd to 0700 if it already exists).
  • The config file is created with mode 0600 via an opener that passes 0o600 to os.open(), and any pre-existing file is chmod'd to 0600 before credentials are written to it.

Changes

  • Updated censys/common/config.py: write_config() now creates the config directory 0700, creates the config file 0600, and tightens permissions on pre-existing files/directories on rewrite.
  • Updated tests/cli/test_config.py: adjusted existing assertions for the new open()/makedirs() calls and added test_write_config_restricts_permissions, a real-filesystem test verifying 0700/0600 for both fresh writes and rewrites over loose-permission files.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens config credential storage by ensuring the Censys config directory and file are created (and, when possible, tightened) with owner-only permissions to avoid leaking plaintext API credentials on shared hosts.

Changes:

  • Add a restricted open() opener (0600) and best-effort chmod helper to tighten existing config paths.
  • Create the default config directory with mode 0700 and attempt to chmod pre-existing directories/files to 0700/0600.
  • Update CLI config tests and add real-filesystem POSIX tests verifying permissions on fresh writes and rewrites.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
censys/common/config.py Enforces owner-only permissions for the config dir/file via mode=0o700, opener with 0o600, and best-effort chmod on rewrites.
tests/cli/test_config.py Updates mocks for the new open(..., opener=...) / makedirs(..., mode=...) behavior and adds POSIX filesystem permission tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Create ~/.config/censys with mode 0700 and write censys.cfg with mode
0600 instead of inheriting the process umask, and tighten permissions
on pre-existing files/directories on rewrite. Under the default umask
of 022 the config file was previously world-readable (0644), exposing
api_secret and asm_api_key to other local users (CWE-276).
@btschwartz12
Ben Schwartz (btschwartz12) force-pushed the fix-config-perms branch 3 times, most recently from 4b3847b to 6f09e75 Compare August 27, 2026 20:45

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thehappydinoa
Aidan Holland (thehappydinoa) merged commit d19ebfc into main Aug 27, 2026
12 checks passed
@thehappydinoa
Aidan Holland (thehappydinoa) deleted the fix-config-perms branch August 27, 2026 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants