Spaces:
Sleeping
Sleeping
Commit Β·
a3bf2e3
1
Parent(s): 7b6e647
refactor: move SENSITIVE_KEYS to config module
Browse files- Centralize sensitive environment keys in config and export them
- README.md +1 -1
- src/config/index.js +3 -0
- src/server/index.js +2 -4
- tests/README.md +1 -1
- tests/helpers/coverage.js +2 -1
README.md
CHANGED
|
@@ -392,7 +392,7 @@ See [tests/README.md](tests/README.md) for full documentation.
|
|
| 392 |
|
| 393 |
- **Minimal runtime dependencies** β only `argon2` for admin password hashing; everything else uses Node.js built-in modules (`http`, `url`, `fs`, `path`, `net`, `node:sqlite`). `dotenv` is a dev dependency.
|
| 394 |
- **Pure RESP protocol** β the Redis client in `src/redis/index.js` implements the Redis serialization protocol over raw TCP sockets without any third-party library. Supports `AUTH` password authentication and `REDIS_URL` connection strings.
|
| 395 |
-
- **Centralized config** β all environment variables are read in `src/config/index.js` and exported as camelCase (`port`, `dbPath`, `redisOpts`, `rateLimitMax`, `dbDebugSql`, etc.) for use across the codebase.
|
| 396 |
- **Rate limiter** β \`src/middleware/rate-limiter.js\` features a circuit breaker for Redis failures, CIDR-based trusted proxy IP extraction, escalating block durations (5m β 20m β 1h), atomic Redis Lua scripts, and an in-memory fallback with LRU eviction. \`createRateLimiter()\` accepts options directly instead of reading config lazily.
|
| 397 |
- **Testable seed script** β `src/db/seed.js` accepts `database` and `fetch` parameters via dependency injection, enabling full unit testing without mocking `require()`.
|
| 398 |
- **SQL query logging** β `src/db/sql-logger.js` exports `wrapDb`/`wrapStmt` Proxy wrappers that log `exec`, `prepare`, `run`, `get`, and `all` calls to stderr. `src/db/index.js` uses them via `getWrappedDb()` when `DEBUG_SQL=true`.
|
|
|
|
| 392 |
|
| 393 |
- **Minimal runtime dependencies** β only `argon2` for admin password hashing; everything else uses Node.js built-in modules (`http`, `url`, `fs`, `path`, `net`, `node:sqlite`). `dotenv` is a dev dependency.
|
| 394 |
- **Pure RESP protocol** β the Redis client in `src/redis/index.js` implements the Redis serialization protocol over raw TCP sockets without any third-party library. Supports `AUTH` password authentication and `REDIS_URL` connection strings.
|
| 395 |
+
- **Centralized config** β all environment variables are read in `src/config/index.js` and exported as camelCase (`port`, `dbPath`, `redisOpts`, `rateLimitMax`, `dbDebugSql`, `sensitiveKeys`, etc.) for use across the codebase.
|
| 396 |
- **Rate limiter** β \`src/middleware/rate-limiter.js\` features a circuit breaker for Redis failures, CIDR-based trusted proxy IP extraction, escalating block durations (5m β 20m β 1h), atomic Redis Lua scripts, and an in-memory fallback with LRU eviction. \`createRateLimiter()\` accepts options directly instead of reading config lazily.
|
| 397 |
- **Testable seed script** β `src/db/seed.js` accepts `database` and `fetch` parameters via dependency injection, enabling full unit testing without mocking `require()`.
|
| 398 |
- **SQL query logging** β `src/db/sql-logger.js` exports `wrapDb`/`wrapStmt` Proxy wrappers that log `exec`, `prepare`, `run`, `get`, and `all` calls to stderr. `src/db/index.js` uses them via `getWrappedDb()` when `DEBUG_SQL=true`.
|
src/config/index.js
CHANGED
|
@@ -35,6 +35,8 @@ const MAX_BODY_SIZE = isNaN(parsedMaxBodySize) || parsedMaxBodySize < 1 ? DEFAUL
|
|
| 35 |
const DEFAULT_PAGE_SIZE = parseInt(process.env.DEFAULT_PAGE_SIZE || '10', 10);
|
| 36 |
/* v8 ignore stop */
|
| 37 |
|
|
|
|
|
|
|
| 38 |
module.exports = {
|
| 39 |
port: PORT,
|
| 40 |
dbPath: DB_PATH,
|
|
@@ -47,4 +49,5 @@ module.exports = {
|
|
| 47 |
maxBodySize: MAX_BODY_SIZE,
|
| 48 |
defaultPageSize: DEFAULT_PAGE_SIZE,
|
| 49 |
adminKey: ADMIN_KEY,
|
|
|
|
| 50 |
};
|
|
|
|
| 35 |
const DEFAULT_PAGE_SIZE = parseInt(process.env.DEFAULT_PAGE_SIZE || '10', 10);
|
| 36 |
/* v8 ignore stop */
|
| 37 |
|
| 38 |
+
const SENSITIVE_KEYS = ['REDIS_PASSWORD', 'ADMIN_KEY'];
|
| 39 |
+
|
| 40 |
module.exports = {
|
| 41 |
port: PORT,
|
| 42 |
dbPath: DB_PATH,
|
|
|
|
| 49 |
maxBodySize: MAX_BODY_SIZE,
|
| 50 |
defaultPageSize: DEFAULT_PAGE_SIZE,
|
| 51 |
adminKey: ADMIN_KEY,
|
| 52 |
+
sensitiveKeys: SENSITIVE_KEYS,
|
| 53 |
};
|
src/server/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
const http = require('http');
|
| 4 |
|
| 5 |
const { port, redisOpts, rateLimitMax, rateLimitEnabled,
|
| 6 |
-
rateLimitWindowMs, rateLimitWindowSec, maxBodySize, adminKey } = require('../config');
|
| 7 |
const argon2 = require('argon2');
|
| 8 |
const RedisClient = require('../redis');
|
| 9 |
const { createRateLimiter } = require('../middleware/rate-limiter');
|
|
@@ -261,10 +261,8 @@ async function handleAdmin(req, res, pathname, method) {
|
|
| 261 |
return json(res, 401, { error: 'Unauthorized' });
|
| 262 |
}
|
| 263 |
|
| 264 |
-
const SENSITIVE_KEYS = ['REDIS_PASSWORD', 'ADMIN_KEY'];
|
| 265 |
-
|
| 266 |
if (pathname === '/api/admin/settings' && method === 'GET') {
|
| 267 |
-
const rows = db.listAll('settings').map((r) =>
|
| 268 |
return json(res, 200, rows);
|
| 269 |
}
|
| 270 |
|
|
|
|
| 3 |
const http = require('http');
|
| 4 |
|
| 5 |
const { port, redisOpts, rateLimitMax, rateLimitEnabled,
|
| 6 |
+
rateLimitWindowMs, rateLimitWindowSec, maxBodySize, adminKey, sensitiveKeys } = require('../config');
|
| 7 |
const argon2 = require('argon2');
|
| 8 |
const RedisClient = require('../redis');
|
| 9 |
const { createRateLimiter } = require('../middleware/rate-limiter');
|
|
|
|
| 261 |
return json(res, 401, { error: 'Unauthorized' });
|
| 262 |
}
|
| 263 |
|
|
|
|
|
|
|
| 264 |
if (pathname === '/api/admin/settings' && method === 'GET') {
|
| 265 |
+
const rows = db.listAll('settings').map((r) => sensitiveKeys.includes(r.key) ? { ...r, value: '***' } : r);
|
| 266 |
return json(res, 200, rows);
|
| 267 |
}
|
| 268 |
|
tests/README.md
CHANGED
|
@@ -35,7 +35,7 @@ tests/
|
|
| 35 |
integration.test.js # API integration tests β real HTTP + SQLite (77 tests)
|
| 36 |
server.test.js # Server request handler, admin auth, graceful shutdown (13 tests)
|
| 37 |
helpers/
|
| 38 |
-
coverage.js # Shared test utilities (save/restore/setEnv/clearCjs)
|
| 39 |
index.js # startServer / stopServer / request utilities
|
| 40 |
seed.js # Standalone script to create & seed temp DB
|
| 41 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (4 tests)
|
|
|
|
| 35 |
integration.test.js # API integration tests β real HTTP + SQLite (77 tests)
|
| 36 |
server.test.js # Server request handler, admin auth, graceful shutdown (13 tests)
|
| 37 |
helpers/
|
| 38 |
+
coverage.js # Shared test utilities (save/restore/setEnv/clearCjs/configMockFactory)
|
| 39 |
index.js # startServer / stopServer / request utilities
|
| 40 |
seed.js # Standalone script to create & seed temp DB
|
| 41 |
seed-settings-coverage.test.js # Seed-settings.js V8 coverage (4 tests)
|
tests/helpers/coverage.js
CHANGED
|
@@ -29,7 +29,7 @@ export function setEnv(overrides) {
|
|
| 29 |
}
|
| 30 |
|
| 31 |
export function configMockFactory() {
|
| 32 |
-
const METHODS = ['port', 'dbPath', 'dbDebugSql', 'redisOpts', 'rateLimitEnabled', 'rateLimitMax', 'rateLimitWindowMs', 'rateLimitWindowSec', 'adminKey', 'maxBodySize', 'defaultPageSize']
|
| 33 |
const cfg = {
|
| 34 |
getPort: () => parseInt(process.env.PORT || '3000', 10),
|
| 35 |
getDbPath: () => process.env.DB_PATH || process.cwd() + '/storage/data.db',
|
|
@@ -53,6 +53,7 @@ export function configMockFactory() {
|
|
| 53 |
return isNaN(raw) || raw < 1 ? 1048576 : raw
|
| 54 |
},
|
| 55 |
getDefaultPageSize: () => parseInt(process.env.DEFAULT_PAGE_SIZE || '10', 10),
|
|
|
|
| 56 |
}
|
| 57 |
const getMethod = (prop) => cfg['get' + prop[0].toUpperCase() + prop.slice(1)]
|
| 58 |
return new Proxy(cfg, {
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
export function configMockFactory() {
|
| 32 |
+
const METHODS = ['port', 'dbPath', 'dbDebugSql', 'redisOpts', 'rateLimitEnabled', 'rateLimitMax', 'rateLimitWindowMs', 'rateLimitWindowSec', 'adminKey', 'maxBodySize', 'defaultPageSize', 'sensitiveKeys']
|
| 33 |
const cfg = {
|
| 34 |
getPort: () => parseInt(process.env.PORT || '3000', 10),
|
| 35 |
getDbPath: () => process.env.DB_PATH || process.cwd() + '/storage/data.db',
|
|
|
|
| 53 |
return isNaN(raw) || raw < 1 ? 1048576 : raw
|
| 54 |
},
|
| 55 |
getDefaultPageSize: () => parseInt(process.env.DEFAULT_PAGE_SIZE || '10', 10),
|
| 56 |
+
getSensitiveKeys: () => ['REDIS_PASSWORD', 'ADMIN_KEY'],
|
| 57 |
}
|
| 58 |
const getMethod = (prop) => cfg['get' + prop[0].toUpperCase() + prop.slice(1)]
|
| 59 |
return new Proxy(cfg, {
|