From 5c29d02fa731fafce6b76e35f78b2f165b887246 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Wed, 1 Nov 2023 10:20:13 +1030 Subject: [PATCH 1/2] DO-1563: Add custom header to forward user-agent header value to the Prerender origin --- packages/prerender-proxy/lib/handlers/prerender-check.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/prerender-proxy/lib/handlers/prerender-check.ts b/packages/prerender-proxy/lib/handlers/prerender-check.ts index 879c1d25..6ab03ad3 100644 --- a/packages/prerender-proxy/lib/handlers/prerender-check.ts +++ b/packages/prerender-proxy/lib/handlers/prerender-check.ts @@ -26,6 +26,14 @@ export const handler = async ( request.headers["x-prerender-host"] = [ { key: "X-Prerender-Host", value: request.headers.host[0].value }, ]; + + // Custom header to be forwarded to Prerender service for better logging + request.headers["x-prerender-user-agent"] = [ + { + key: "x-prerender-user-agent", + value: request.headers["user-agent"][0].value, + }, + ]; } } From c1db14864c01aef5cb04210d4ffc9305b829322d Mon Sep 17 00:00:00 2001 From: Chris Park Date: Wed, 1 Nov 2023 10:31:03 +1030 Subject: [PATCH 2/2] DO-1563: Log custom header having the Prerender requester's User-Agent information --- packages/prerender-fargate/lib/prerender/server.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index d7a41ef1..587a7667 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -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); } @@ -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); }