Files
harheimertc/server/utils/audit-log.js
Torsten Schulz (local) 3586704cce
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 10m29s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped
Update file permissions for multiple server and test files to executable mode
2026-07-15 08:45:12 +02:00

36 lines
970 B
JavaScript
Executable File

import fs from 'fs/promises'
import path from 'path'
// nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
const getDataPath = (filename) => {
const cwd = process.cwd()
if (cwd.endsWith('.output')) {
// nosemgrep
return path.join(cwd, '../server/data', filename)
}
// nosemgrep
return path.join(cwd, 'server/data', filename)
}
const AUDIT_LOG_FILE = getDataPath('audit.log.jsonl')
function safeStr(v, max = 500) {
return String(v == null ? '' : v).slice(0, max)
}
export async function writeAuditLog(eventType, data = {}) {
const enabled = (process.env.AUDIT_LOG_ENABLED || 'true').toLowerCase() !== 'false'
if (!enabled) return
const entry = {
ts: new Date().toISOString(),
type: safeStr(eventType, 100),
data
}
await fs.mkdir(path.dirname(AUDIT_LOG_FILE), { recursive: true })
await fs.appendFile(AUDIT_LOG_FILE, JSON.stringify(entry) + '\n', 'utf-8')
}