I ran into some difficulties getting my Express application coming out with Nginx to report csp violations , and two things I learned from the above answer:
- There must be a
POST method, not a GET method req.body contains a report
But the above was not enough, and all the time I became empty req.body , and I could not find another message to describe how to fix it. After some research, I came across this post , as well as a completely isolated github issue , where dougwilson give tips on where to put the route that the csp report handles.
The reason req.body was empty for me was because I put the csp report route handler after the following configurations:
app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser());
I moved the csp report route handler over them, but I still kept getting req.body empty, then I added the csp report route handler above to get the report in req.body
app.use(bodyParser.json({ type: 'application/csp-report' }));
After adding the above line to the csp report request handler, Express realized that it should parse requests that have a Content-type like application/csp-report .
Maybe, by default, Express does not parse application/csp-report and add a resolved problem for me. I also googled if Express parses application/csp-report by default, and I came across this gist , claiming Chrome sends application/csp-report , while Firefox sends application/json (and I use Chrome - you can enable application/json if you run into problems with FF ).
So this is how it looks in my app.js
// without following csp-report don't get parsed. app.use(bodyParser.json({ type: 'application/csp-report' })); app.get('/vehicle/cspreport', function(req, res) { res.status(403); }); app.post('/vehicle/cspreport', function(req, res) { console.log('csp report > ' + JSON.stringify(req.body)); }); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); ...
The accepted answer sent by the OP is from 2011 , and I was thinking of adding an answer to show how I solved this problem in 2016 with the following versions of Node.js, Express and Nginx
Node: v4.2.4 Express: 4.13.1 Nginx: 1.8.1