From bdc29ed46a8ac5f54403c7006b68d0a14029ceb3 Mon Sep 17 00:00:00 2001 From: Joshua Davis Date: Sat, 31 Oct 2026 16:54:08 -0400 Subject: [PATCH] Warn when .env can be read by other users (roadmap 4.10) The file holds every API key and the web password. Loading a loose one prints one warning to stderr with the fix, and never the file's contents. Story: PDX-37 Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + ROADMAP.md | 6 +++--- SECURITY.md | 6 +++--- paradox/config.py | 14 ++++++++++++++ tests/test_config.py | 26 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 961604e..355c717 100644 --- a/README.md +++ b/README.md @@ -412,6 +412,7 @@ API quotas (and Anthropic credits, if narratives are on), so it has guards: ```bash cp paradox/env.example .env # add keys and PARADOX_PASSWORD +chmod 600 .env docker compose up -d --build docker compose run --rm web paradox scan 45.155.205.233 --json # any headless command ``` diff --git a/ROADMAP.md b/ROADMAP.md index 0108552..a89873d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -426,9 +426,9 @@ The URL works like a password for its channel, and on the command line it shows - [x] **PDX-36: Refuse `http://` webhooks.** Done when: the watcher refuses to start with a non-HTTPS webhook unless it points at loopback or an explicit override is set, with tests for each case. -### 4.10 Warn about a loose `.env` · S +### 4.10 Warn about a loose `.env` · S · ✅ shipped -- [ ] **PDX-37: Warn about `.env` permissions.** Done when: loading a `.env` readable by group or others prints one warning to stderr without the file's contents, a mode 600 file prints none, a test covers both, and platforms without POSIX modes are handled. +- [x] **PDX-37: Warn about `.env` permissions.** Done when: loading a `.env` readable by group or others prints one warning to stderr without the file's contents, a mode 600 file prints none, a test covers both, and platforms without POSIX modes are handled. ### 4.5 Limit which files `scan_log` can read · S · ✅ shipped @@ -551,7 +551,7 @@ and fix the top issues. Repeat after 5.6. It also doubles as class material. | When | Items | Why first | |---|---|---| | Now | ~~1.1, 1.2, 1.5, 3.2, 5.1~~ (done) | Small, keyless, and fix the biggest measured weakness (the scanner rule's dependence on GreyNoise); 5.1 makes that dependence visible | -| Next | 1.3, 3.1, 3.5, 3.6, 4.1, 4.10, ~~4.6, 4.7, 4.8, 4.9, 4.5, 1.7, 1.8, 3.7, 1.4, 1.6, 5.2, 5.4~~ (done) | Close the known false positives, build adversarial and hiding-place test sets, test the AI summarizer under attack, and show watch state and claim checks in the UI | +| Next | 1.3, 3.1, 3.5, 3.6, 4.1, ~~4.6, 4.7, 4.8, 4.9, 4.10, 4.5, 1.7, 1.8, 3.7, 1.4, 1.6, 5.2, 5.4~~ (done) | Close the known false positives, build adversarial and hiding-place test sets, test the AI summarizer under attack, and show watch state and claim checks in the UI | | Then | 2.1, 2.3, 3.3, 3.4, 4.2, ~~4.3, 4.4~~ (done), 5.3, 5.5–5.7 | New unmasking signals plus self-measurement once history has accumulated, with the UI restructured to hold them | | Later | 2.2, 2.4, 2.5 | Bigger extensions: domains, campaigns, more beacons | diff --git a/SECURITY.md b/SECURITY.md index 631610c..adf72a2 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -122,15 +122,15 @@ the web app. **What's in place:** -- `paradox init` creates it with mode 600. +- `paradox init` creates it with mode 600, and loading a file that others can read prints a warning with the fix. - It's in `.gitignore` and `.dockerignore`, so it stays out of commits and images. Compose passes it at runtime. - Status output reports whether a key is set, never its value. - `scan_log` can't read it by default, because the working folder isn't allowed. **Open:** -- Copying `env.example` by hand, as the Docker steps do, keeps the default permissions. Run `chmod 600 .env`. -- The file isn't checked for loose permissions when it's loaded. +- A loose `.env` only gets a warning on stderr. It still loads, so a keys file others can read keeps working. +- Windows has no such check, and a container's `env_file` is read by Docker, not this check. - Keys are held in plain text and in the process environment. - A key pasted into a chat or an AI session should be treated as exposed and rotated. diff --git a/paradox/config.py b/paradox/config.py index 24a368e..c1f1495 100644 --- a/paradox/config.py +++ b/paradox/config.py @@ -3,10 +3,12 @@ from __future__ import annotations import os +import sys from pathlib import Path PACKAGE = Path(__file__).resolve().parent ROOT = PACKAGE.parent +POSIX_MODES = os.name == "posix" # Windows reports made-up modes def _home() -> Path: @@ -28,10 +30,22 @@ def _home() -> Path: ENV_FILE = HOME / ".env" +def warn_if_exposed(path: Path) -> None: + """A keys file others can read gives away every API key and the web password.""" + if not POSIX_MODES: + return + try: + if path.stat().st_mode & 0o077: + print(f"warning: {path} can be read by other users; fix it with: chmod 600 {path}", file=sys.stderr) + except OSError: + pass + + def load_dotenv(path: Path = ENV_FILE) -> None: """Load KEY=VALUE lines into os.environ without overriding existing vars.""" if not path.exists(): return + warn_if_exposed(path) for line in path.read_text().splitlines(): line = line.strip() if not line or line.startswith("#") or "=" not in line: diff --git a/tests/test_config.py b/tests/test_config.py index 6eb6c12..afceb53 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,3 +1,4 @@ +import io import os import tempfile import unittest @@ -7,6 +8,31 @@ from paradox import config +class ExposedKeysTests(unittest.TestCase): + def warnings(self, mode, posix=True): + with tempfile.TemporaryDirectory() as d, mock.patch.dict(os.environ, {}, clear=True), \ + mock.patch("sys.stderr", io.StringIO()) as err, mock.patch.object(config, "POSIX_MODES", posix): + path = Path(d) / ".env" + path.write_text("SOME_KEY=s3cret\n") + path.chmod(mode) + config.load_dotenv(path) + self.assertEqual(os.environ["SOME_KEY"], "s3cret") # still loads + return err.getvalue() + + def test_private_file_is_quiet(self): + self.assertEqual(self.warnings(0o600), "") + + def test_group_or_world_access_warns_once_without_contents(self): + for mode in (0o644, 0o640, 0o604): + text = self.warnings(mode) + self.assertEqual(text.count("warning"), 1, oct(mode)) + self.assertIn("chmod 600", text) + self.assertNotIn("s3cret", text) + + def test_platforms_without_posix_modes_are_quiet(self): + self.assertEqual(self.warnings(0o644, posix=False), "") + + class DotenvTests(unittest.TestCase): def load(self, text): with tempfile.TemporaryDirectory() as d, mock.patch.dict(os.environ, {}, clear=True):