total-recall-brain 3.30.2 → 3.31.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,7 @@ import path from 'node:path';
18
18
  import { fileURLToPath } from 'node:url';
19
19
  import zlib from 'node:zlib';
20
20
  import fs from 'node:fs';
21
+ import { writeFileSecure } from '../core/secure-file.mjs';
21
22
  import {
22
23
  apiRateLimiter,
23
24
  corsOptions,
@@ -1078,7 +1079,7 @@ if (HOST !== '127.0.0.1' && HOST !== '0.0.0.0') {
1078
1079
  wizardCfg['cfg-dash-url'] = `${tunnelUrl}/dashboard`;
1079
1080
  wizardCfg['cfg-health-url'] = `${tunnelUrl}/health`;
1080
1081
 
1081
- fs.writeFileSync(configFile, JSON.stringify(wizardCfg, null, 2), { encoding: 'utf8', mode: 0o600 });
1082
+ writeFileSecure(configFile, JSON.stringify(wizardCfg, null, 2), { encoding: 'utf8', mode: 0o600 });
1082
1083
  logger.info('server', `Updated wizard-config.json with active Quick Tunnel URL: ${tunnelUrl}`);
1083
1084
  } else {
1084
1085
  logger.warn('server', 'Could not allocate Quick Tunnel URL. Please check logs in brainDir/logs/cloudflared.log');
@@ -6,6 +6,7 @@ import bcrypt from 'bcrypt';
6
6
  import jwt from 'jsonwebtoken';
7
7
  import { WebSocket } from 'ws';
8
8
  import { brainDir as configBrainDir } from '../../core/config.mjs';
9
+ import { chmodSecure, writeFileSecure } from '../../core/secure-file.mjs';
9
10
 
10
11
  // Setup database paths in brainDir
11
12
  const DATA_DIR = path.join(configBrainDir, 'collab');
@@ -22,10 +23,13 @@ export function resolveJwtSecret(env = process.env, secretFile = JWT_SECRET_FILE
22
23
  if (env.JWT_SECRET) return env.JWT_SECRET;
23
24
  try {
24
25
  const existing = fs.readFileSync(secretFile, 'utf8').trim();
25
- if (existing.length >= 32) return existing;
26
+ if (existing.length >= 32) {
27
+ chmodSecure(secretFile);
28
+ return existing;
29
+ }
26
30
  } catch { /* not created yet */ }
27
31
  const generated = crypto.randomBytes(48).toString('base64url');
28
- fs.writeFileSync(secretFile, generated, { encoding: 'utf8', mode: 0o600 });
32
+ writeFileSecure(secretFile, generated, { encoding: 'utf8' });
29
33
  return generated;
30
34
  }
31
35
  const JWT_SECRET = resolveJwtSecret();
@@ -44,7 +48,7 @@ function readJsonFile(filePath, defaultData = []) {
44
48
  }
45
49
 
46
50
  function writeJsonFile(filePath, data) {
47
- fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
51
+ writeFileSecure(filePath, JSON.stringify(data, null, 2), { encoding: 'utf8' });
48
52
  }
49
53
 
50
54
  // ─── Database Helpers ────────────────────────────────────────────────────────
@@ -15,6 +15,7 @@ import yaml from 'yaml';
15
15
  import { requireAuth, requireScope, loadSecurityConfig } from '../auth.mjs';
16
16
  import { BRAIN_DIR, CONFIG_DIR, AGENT_DIR, serverError, badRequest } from './_shared.mjs';
17
17
  import { loadRuntimeConfig } from '../../core/runtime.mjs';
18
+ import { writeFileSecure } from '../../core/secure-file.mjs';
18
19
 
19
20
  const router = Router();
20
21
 
@@ -152,10 +153,10 @@ router.post('/api/config-json', requireAuth, requireScope('config:write'), async
152
153
  }
153
154
 
154
155
  if (security) {
155
- fs.writeFileSync(securityPath, yaml.stringify(security), { encoding: 'utf8', mode: 0o600 });
156
+ writeFileSecure(securityPath, yaml.stringify(security), { encoding: 'utf8', mode: 0o600 });
156
157
  }
157
158
  if (budget) {
158
- fs.writeFileSync(budgetPath, yaml.stringify(budget), { encoding: 'utf8', mode: 0o600 });
159
+ writeFileSecure(budgetPath, yaml.stringify(budget), { encoding: 'utf8', mode: 0o600 });
159
160
  }
160
161
  if (brain) {
161
162
  let existingBrain = {};
@@ -167,7 +168,7 @@ router.post('/api/config-json', requireAuth, requireScope('config:write'), async
167
168
  nextBrain.token = existingBrain.token;
168
169
  }
169
170
  delete nextBrain.has_token;
170
- fs.writeFileSync(brainPath, JSON.stringify(nextBrain, null, 2), { encoding: 'utf8', mode: 0o600 });
171
+ writeFileSecure(brainPath, JSON.stringify(nextBrain, null, 2), { encoding: 'utf8', mode: 0o600 });
171
172
  }
172
173
  if (secrets) {
173
174
  // Only accept real secret values — ignore masked placeholders from GET /api/config-json.