From 3c5728b6d9c75d2d7324c9b2a779262ca83e6f01 Mon Sep 17 00:00:00 2001 From: mathijs-b2m <48010711+mathijs-b2m@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:51:20 +0200 Subject: [PATCH] Update rest-layout-service.ts --- packages/sitecore-jss/src/layout/rest-layout-service.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/sitecore-jss/src/layout/rest-layout-service.ts b/packages/sitecore-jss/src/layout/rest-layout-service.ts index a68479f66f..d0f3376194 100644 --- a/packages/sitecore-jss/src/layout/rest-layout-service.ts +++ b/packages/sitecore-jss/src/layout/rest-layout-service.ts @@ -215,12 +215,17 @@ export class RestLayoutService extends LayoutServiceBase { protected setupReqHeaders(req: IncomingMessage) { return (reqConfig: AxiosRequestConfig) => { debug.layout('performing request header passing'); + + const xForwardedFor = req.headers['x-forwarded-for']; + const remoteAddress = req.socket?.remoteAddress; + reqConfig.headers.common = { ...reqConfig.headers.common, ...(req.headers.cookie && { cookie: req.headers.cookie }), ...(req.headers.referer && { referer: req.headers.referer }), ...(req.headers['user-agent'] && { 'user-agent': req.headers['user-agent'] }), - ...(req.connection.remoteAddress && { 'X-Forwarded-For': req.connection.remoteAddress }), + ...(xForwardedFor && { 'X-Forwarded-For': xForwardedFor }), + ...(remoteAddress && !xForwardedFor && { 'X-Forwarded-For': remoteAddress }), }; return reqConfig; };