|
12 | 12 |
|
13 | 13 | import { createServer, IncomingMessage, ServerResponse } from 'http'; |
14 | 14 | import { parse as parseUrl } from 'url'; |
15 | | -import { existsSync, writeFileSync, mkdirSync, readFileSync } from 'fs'; |
| 15 | +import { existsSync, readFileSync } from 'fs'; |
16 | 16 | import { join } from 'path'; |
17 | 17 | import { homedir } from 'os'; |
18 | 18 | import { createHmac } from 'crypto'; |
19 | 19 | import { execFileSync } from 'child_process'; |
20 | 20 | import { processIncomingResponse, loadSMSConfig } from './sms-notify.js'; |
21 | 21 | import { queueAction, executeActionSafe } from './sms-action-runner.js'; |
| 22 | +import { writeFileSecure, ensureSecureDir } from './secure-fs.js'; |
22 | 23 |
|
23 | 24 | // Security constants |
24 | 25 | const MAX_BODY_SIZE = 50 * 1024; // 50KB max body |
@@ -96,12 +97,13 @@ function storeLatestResponse( |
96 | 97 | response: string, |
97 | 98 | action?: string |
98 | 99 | ): void { |
99 | | - const dir = join(homedir(), '.stackmemory'); |
100 | | - if (!existsSync(dir)) { |
101 | | - mkdirSync(dir, { recursive: true }); |
102 | | - } |
103 | | - const responsePath = join(dir, 'sms-latest-response.json'); |
104 | | - writeFileSync( |
| 100 | + ensureSecureDir(join(homedir(), '.stackmemory')); |
| 101 | + const responsePath = join( |
| 102 | + homedir(), |
| 103 | + '.stackmemory', |
| 104 | + 'sms-latest-response.json' |
| 105 | + ); |
| 106 | + writeFileSecure( |
105 | 107 | responsePath, |
106 | 108 | JSON.stringify({ |
107 | 109 | promptId, |
@@ -215,7 +217,7 @@ function triggerResponseNotification(response: string): void { |
215 | 217 | // Write signal file for other processes |
216 | 218 | try { |
217 | 219 | const signalPath = join(homedir(), '.stackmemory', 'sms-signal.txt'); |
218 | | - writeFileSync( |
| 220 | + writeFileSecure( |
219 | 221 | signalPath, |
220 | 222 | JSON.stringify({ |
221 | 223 | type: 'sms_response', |
@@ -366,7 +368,7 @@ export function startWebhookServer(port: number = 3456): void { |
366 | 368 | ? JSON.parse(readFileSync(statusPath, 'utf8')) |
367 | 369 | : {}; |
368 | 370 | statuses[payload['MessageSid']] = payload['MessageStatus']; |
369 | | - writeFileSync(statusPath, JSON.stringify(statuses, null, 2)); |
| 371 | + writeFileSecure(statusPath, JSON.stringify(statuses, null, 2)); |
370 | 372 |
|
371 | 373 | res.writeHead(200, { 'Content-Type': 'text/plain' }); |
372 | 374 | res.end('OK'); |
|
0 commit comments