Skip to content

Commit

Permalink
DO-1563: Log custom header having the Prerender requester's User-Agen…
Browse files Browse the repository at this point in the history
…t information
  • Loading branch information
Chris Park committed Nov 1, 2023
1 parent 5c29d02 commit c1db148
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/prerender-fargate/lib/prerender/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ const server = prerender({

server.use({
requestReceived: (req, res, next) => {
console.log(`${new Date().toISOString()} User-Agent: "${req.get('user-agent')}" ${req.prerender.reqId} ${req.prerender.url}`);
// Log "x-prerender-user-agent" value forwarded from CloudFront/Lambda@edge that contains the original User-Agent value. If not present, e.g. requests from ELB, default to "user-agent" value.
const userAgent = req.get('x-prerender-user-agent') || req.get('user-agent');

console.log(`${new Date().toISOString()} User-Agent: "${userAgent}" ${req.prerender.reqId} ${req.prerender.url}`);
let auth = req.headers['x-prerender-token'];
if (!auth) {
console.log(`${new Date().toISOString()} "${req.get('user-agent')}" ${req.prerender.reqId} Authentication header not found.`);
console.log(`${new Date().toISOString()} "${userAgent}" ${req.prerender.reqId} Authentication header not found.`);
return res.send(401);
}

Expand All @@ -40,7 +43,7 @@ server.use({
if (authenticated) break;
}
if (!authenticated) {
console.log(`${new Date().toISOString()} "${req.get('user-agent')}" ${req.prerender.reqId} Authentication Failed.`);
console.log(`${new Date().toISOString()} "${userAgent}" ${req.prerender.reqId} Authentication Failed.`);
return res.send(401);
}

Expand Down

0 comments on commit c1db148

Please sign in to comment.