I'm sending a CSP-report via report-to
directive:
app.use(express.json({
type: [
"application/json",
"application/csp-report",
"application/reports+json"
]
}));
const _reportCSP = {
endpoints: [
{
url: "https://myapp.com/reportCSP"
}
],
group: "csp-endpoint",
include_subdomains: true,
max_age: 31536000
};
res.setHeader("content-security-policy", `default-src 'none'; script-src https://*.someHost.com 'self' 'unsafe-eval' 'unsafe-inline'; style-src https://*.someHost.com 'self'; font-src https://*.someHost.com 'self'; img-src 'self'; connect-src https://*.someHost.com https://*.dropboxapi.co 'self'; frame-ancestors 'none'; report-to ${reportCSP.group};`);
res.setHeader("report-to", JSON.stringify(reportCSP));
An URL https://*.dropboxapi.co
is used for the CSP-reports testing purposes only.
Based on this sample, I handle a CSP-report request in the following manner:
function echoReports(request, response) {
for (const report of request.body) {
console.log(`Report content: ${report}`);
console.log(`Report content: ${JSON.stringify(report)}`);
}
}
app.post("/reportCSP",
async (req, res) => {
console.log(`${req.body.length} CSP violation reports:`);
echoReports(req, res);
}
);
As a result, I get on a server-side:
1 CSP violation reports:
Report content: [object Object]
Report content: "[object Object]"
In DevTools I see the correct error report:
Refused to connect to 'https://content.dropboxapi.com/…' because it violates the following Content Security Policy directive: "connect-src https://*.someHost.com https://*.dropboxapi.co 'self'"
Refused to connect to 'https://content.dropboxapi.com/…' because it violates the document's Content Security Policy.
JSON.stringify(request)
inside of echoReports(req, res)
leads to:
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Socket'
| property 'parser' -> object with constructor 'HTTPParser'
--- property 'socket' closes the circle
at JSON.stringify (<anonymous>)
at echoReports (file:///app/src/server/app.mjs:2949:33)
Since I'm on Express.js 4.17.1, I don't use body-parser
but use express.json(…)
instead.
I've tried several approaches to print a content of the CSP-report on the server-side but no success.
Of course, any anti-banner, security and privacy protection software had been deactivated.
P.S. I've elaborated the question to make it more clear what's the problem I'm trying to solve.