From 50ed21035307b64d0b520de3bfbeeb85a88c7d67 Mon Sep 17 00:00:00 2001 From: Paperclip Agent Date: Sat, 9 May 2026 01:11:45 +0000 Subject: [PATCH 1/2] docs(secrets): align SECRETS_MANAGEMENT precedence with secrets.js PR #15 (commit 0ab7e7e) fixed the merge-order comment inside src/config/secrets.js but the standalone docs/SECRETS_MANAGEMENT.md still claimed the inverse precedence (system file > .env > process.env), which contradicts the runtime where SecretsManager.load() applies process.env then /etc/ai-news-writer/secrets.env then the repo .env so the last assignment wins. Changes: - Update docs/SECRETS_MANAGEMENT.md to reflect the true precedence (.env > /etc/ai-news-writer/secrets.env > process.env) plus a production-safety note explaining why .env must not exist on deployed hosts and how lxc-secrets-inject.sh enforces that. - Add a load() precedence test in src/config/secrets.test.js that stubs fs.existsSync/fs.readFileSync to exercise the singleton end to end, locking the contract so future refactors fail loudly if the merge order changes without a docs update. Closes the documentation drift left over from BIT-4294 PR #15. The runtime semantics are unchanged. Paperclip-Agent: David Santos (id 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376) Paperclip-Issue: BIT-4294 Paperclip-Run: d5bfe2e3-56d0-418f-a8fd-80817aaa40f2 --- docs/SECRETS_MANAGEMENT.md | 38 +++++++++++++++------ src/config/secrets.test.js | 68 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 11 deletions(-) diff --git a/docs/SECRETS_MANAGEMENT.md b/docs/SECRETS_MANAGEMENT.md index f757148..7cc4af1 100644 --- a/docs/SECRETS_MANAGEMENT.md +++ b/docs/SECRETS_MANAGEMENT.md @@ -1,33 +1,49 @@ # Secrets Management — BIT-4603 -**Status:** ✅ Consolidated, standardized (v0.1) +**Status:** ✅ Consolidated, standardized (v0.2) **Owner:** BitReport DevOps -**Last Updated:** 2026-04-24 +**Last Updated:** 2026-05-09 ## Overview This document describes the unified secrets management strategy for **ai-news-writer**. All sensitive data (API keys, credentials, tokens) follow a single, standardized approach: 1. **Centralized loading** via `src/config/secrets.js` -2. **Priority-based resolution** (deployed secrets > development .env > environment) +2. **Layered resolution** (later sources override earlier ones — see precedence below) 3. **Single source of truth** across all environments --- ## Architecture -### Load Priority (highest to lowest) +### Load order (later sources override earlier — implementation in `src/config/secrets.js`) + +`SecretsManager.load()` performs three `Object.assign` calls in this exact order, so the **last source to define a key wins**: ``` -1. /etc/ai-news-writer/secrets.env - ↓ (injected by deployment tooling — Gitea Actions, lxc-secrets-inject.sh) -2. .env (local development — not in git) - ↓ (loaded via fs.readFileSync) -3. process.env (already in shell environment) - ↓ (e.g., PM2 env config, Docker env, CI pipeline) -4. Defaults (where safe: timeouts, log levels, ports) +1. process.env + ↓ (baseline — shell exports, PM2 env config, Docker, CI pipeline) +2. /etc/ai-news-writer/secrets.env + ↓ (production injection target — root:root 600, written by tools/deploy/lxc-secrets-inject.sh) +3. .env at the repo root + ↓ (local development override — `.env` is git-ignored) + +Effective precedence (highest to lowest): + .env > /etc/ai-news-writer/secrets.env > process.env ``` +> **Production note.** `.env` must not exist in production deployments. If it does, it +> **silently overrides** `/etc/ai-news-writer/secrets.env`. The intent of putting `.env` +> last is to let a developer override a single key on their workstation without rewriting +> the system file. Deployment tooling (`tools/deploy/lxc-secrets-inject.sh`) writes only +> the system file and never copies a `.env`; the `.gitignore` keeps `.env` out of any +> image built from the repo. + +> **Defaults.** Built-in defaults for non-secret tunables (timeouts, log levels, ports, +> retry caps) live in `src/config/loader.js`, `src/config/listen-defaults.js`, and the +> per-module `process.env.X || ` reads. They are applied only when none of the +> three sources above provide a value. See `.env.example` for the canonical list. + ### Components | Component | Purpose | Location | diff --git a/src/config/secrets.test.js b/src/config/secrets.test.js index 4a28c93..8a7f2a7 100644 --- a/src/config/secrets.test.js +++ b/src/config/secrets.test.js @@ -1,6 +1,8 @@ 'use strict'; const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); const { describe, it, beforeEach, afterEach } = require('node:test'); describe('SecretsManager', () => { @@ -134,4 +136,70 @@ describe('SecretsManager', () => { assert.strictEqual(parsed.CONNECTION_STRING, 'user=admin;pass=secret'); }); }); + + // BIT-4294 / BIT-4602: lock the documented precedence so that the runtime + // implementation in src/config/secrets.js and docs/SECRETS_MANAGEMENT.md + // cannot silently drift. Effective precedence (highest → lowest): + // 1. repo .env (developer override) + // 2. /etc/ai-news-writer/secrets.env (production injection target) + // 3. process.env (shell / PM2 / CI baseline) + describe('load() — precedence (BIT-4294 contract)', () => { + it('repo .env overrides /etc/.../secrets.env which overrides process.env', () => { + // Stub fs reads so the singleton load() executes against in-memory file + // contents rather than the real /etc/ai-news-writer/secrets.env path. + // This proves the merge order in SecretsManager.load() itself, not just + // the parser, so a future refactor of load() can't break the contract. + const SYSTEM_FILE = '/etc/ai-news-writer/secrets.env'; + const REPO_FILE = path.join(__dirname, '../../.env'); + + const fakeFiles = { + [SYSTEM_FILE]: [ + 'PREC_KEY_SHARED=from-system-file', + 'PREC_KEY_SYSTEM_ONLY=system-only-value', + 'PREC_KEY_SHELL_ONLY=system-overrides-shell' + ].join('\n') + '\n', + [REPO_FILE]: [ + 'PREC_KEY_SHARED=from-repo-env', + 'PREC_KEY_REPO_ONLY=repo-only-value', + 'PREC_KEY_SYSTEM_ONLY=repo-overrides-system' + ].join('\n') + '\n' + }; + + const realExistsSync = fs.existsSync; + const realReadFileSync = fs.readFileSync; + fs.existsSync = (p) => Object.prototype.hasOwnProperty.call(fakeFiles, p) || realExistsSync(p); + fs.readFileSync = (p, enc) => + Object.prototype.hasOwnProperty.call(fakeFiles, p) ? fakeFiles[p] : realReadFileSync(p, enc); + + try { + process.env.PREC_KEY_SHARED = 'from-process-env'; + process.env.PREC_KEY_SHELL_ONLY = 'shell-only-value'; + manager._clearCache(); + + const loaded = manager.load(); + + // Repo .env wins for keys defined in all three sources. + assert.strictEqual(loaded.PREC_KEY_SHARED, 'from-repo-env', + '.env should override /etc/ai-news-writer/secrets.env and process.env'); + // System file wins over shell when .env is silent on a key. + assert.strictEqual(loaded.PREC_KEY_SHELL_ONLY, 'system-overrides-shell', + '/etc/ai-news-writer/secrets.env should override process.env'); + // Repo .env wins over system file when shell is silent on a key. + assert.strictEqual(loaded.PREC_KEY_SYSTEM_ONLY, 'repo-overrides-system', + '.env should override /etc/ai-news-writer/secrets.env'); + // Source-only keys are still readable. + assert.strictEqual(loaded.PREC_KEY_REPO_ONLY, 'repo-only-value'); + + // get() honours the same precedence (sanity check on the public API). + assert.strictEqual(manager.get('PREC_KEY_SHARED'), 'from-repo-env'); + assert.strictEqual(manager.get('PREC_KEY_SHELL_ONLY'), 'system-overrides-shell'); + } finally { + fs.existsSync = realExistsSync; + fs.readFileSync = realReadFileSync; + delete process.env.PREC_KEY_SHARED; + delete process.env.PREC_KEY_SHELL_ONLY; + manager._clearCache(); + } + }); + }); }); -- 2.47.3 From c340d41f1951e0f7530f541af055a65457556db3 Mon Sep 17 00:00:00 2001 From: Paperclip Agent Date: Sat, 9 May 2026 11:21:11 +0000 Subject: [PATCH 2/2] chore(deps): fix npm audit high (fast-xml-builder) Resolves transitive advisory so `npm audit --audit-level=high` passes in Gitea Actions; unblocks merge of PR #17 (secrets doc + precedence test). Paperclip-Issue: BIT-4602 Paperclip-Run: 8225f565-2ac5-41fb-abb0-65b6ae6bf099 --- package-lock.json | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 72c12b1..cd1c4e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1100,9 +1100,9 @@ "license": "MIT" }, "node_modules/fast-xml-builder": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/fast-xml-builder/-/fast-xml-builder-1.1.5.tgz", - "integrity": "sha512-4TJn/8FKLeslLAH3dnohXqE3QSoxkhvaMzepOIZytwJXZO69Bfz0HBdDHzOTOon6G59Zrk6VQ2bEiv1t61rfkA==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fast-xml-builder/-/fast-xml-builder-1.2.0.tgz", + "integrity": "sha512-00aAWieqff+ZJhsXA4g1g7M8k+7AYoMUUHF+/zFb5U6Uv/P0Vl4QZo84/IcufzYalLuEj9928bXN9PbbFzMF0Q==", "funding": [ { "type": "github", @@ -1111,7 +1111,8 @@ ], "license": "MIT", "dependencies": { - "path-expression-matcher": "^1.1.3" + "path-expression-matcher": "^1.5.0", + "xml-naming": "^0.1.0" } }, "node_modules/fast-xml-parser": { @@ -2793,6 +2794,21 @@ "dev": true, "license": "ISC" }, + "node_modules/xml-naming": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/xml-naming/-/xml-naming-0.1.0.tgz", + "integrity": "sha512-k8KO9hrMyNk6tUWqUfkTEZbezRRpONVOzUTnc97VnCvyj6Tf9lyUR9EDAIeiVLv56jsMcoXEwjW8Kv5yPY52lw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/NaturalIntelligence" + } + ], + "license": "MIT", + "engines": { + "node": ">=16.0.0" + } + }, "node_modules/xml2js": { "version": "0.5.0", "license": "MIT", -- 2.47.3