Description
A user-supplied newline can split a line-oriented log entry. Tabs and ANSI control sequences can also change how the entry looks in a terminal.
Potential impact
Misleading records can hide real events, disrupt SIEM alerts and undermine audit investigations. A viewer that interprets log values as HTML can expose its users to script execution.
Remediation
- Normalize fields in a shared logging helper. Remove or escape line separators, handle other control characters for the output format, and cap field length to limit log volume.
- Use a fixed format string with the cleaned values. Omit passwords and tokens.
- In a web viewer, render values as text or encode them for the HTML context.
Examples
Before
javascript
const express = require('express');
const app = express();
app.get('/audit', (req, res) => {
const agent = req.headers['user-agent']; // Untrusted input
const q = req.query.q; // Untrusted input
// BAD: insert input directly into the log template
console.info(`[AUDIT] agent=${agent} query=${q}`);
res.send('ok');
});
app.listen(3001);
After
javascript
const express = require('express');
const app = express();
function sanitizeForLog(v) {
if (typeof v !== 'string') return v;
// Remove CR/LF and Unicode line separators; limit length
const noCtl = v.replace(/[\r\n]/g, '').replace(/[\u2028\u2029]/g, '');
return noCtl.length > 256 ? noCtl.slice(0, 256) + '…' : noCtl;
}
app.get('/audit', (req, res) => {
const rawAgent = req.headers['user-agent'];
const rawQ = req.query.q;
const agent = sanitizeForLog(String(rawAgent || 'unknown'));
const q = sanitizeForLog(String(rawQ || ''));
// GOOD: use a fixed format with normalized values
console.info('[AUDIT] agent=%s query=%s', agent, q);
res.send('ok');
});
app.listen(3001);
sanitizeForLog removes CR, LF and Unicode line separators from the two input fields. It keeps up to 256 UTF-16 code units and adds an ellipsis if it cuts the value. Terminal controls and HTML still need handling at display time.