@@ -265,12 +265,12 @@ const handleWebsocketAuth = async (
265265 req . headers . authorization = `Bearer ${ jwt } ` ;
266266 const user = await getUser ( req ) ;
267267
268- const isIpInAllowlist = await checkIpInAllowlist ( req ) ;
269- if ( ! isIpInAllowlist ) {
268+ const { isAllowed , ip } = await checkIpInAllowlist ( req ) ;
269+ if ( ! isAllowed ) {
270270 logger ( {
271271 service : "server" ,
272272 level : "error" ,
273- message : `Unauthorized IP address: ${ req . ip } ` ,
273+ message : `Unauthorized IP address: ${ ip } ` ,
274274 } ) ;
275275 return {
276276 isAuthed : false ,
@@ -339,12 +339,12 @@ const handleKeypairAuth = async (args: {
339339 throw error ;
340340 }
341341
342- const isIpInAllowlist = await checkIpInAllowlist ( req ) ;
343- if ( ! isIpInAllowlist ) {
342+ const { isAllowed , ip } = await checkIpInAllowlist ( req ) ;
343+ if ( ! isAllowed ) {
344344 logger ( {
345345 service : "server" ,
346346 level : "error" ,
347- message : `Unauthorized IP address: ${ req . ip } ` ,
347+ message : `Unauthorized IP address: ${ ip } ` ,
348348 } ) ;
349349 throw new Error (
350350 "Unauthorized IP address. See: https://portal.thirdweb.com/engine/features/security" ,
@@ -400,12 +400,12 @@ const handleAccessToken = async (
400400 return { isAuthed : false } ;
401401 }
402402
403- const isIpInAllowlist = await checkIpInAllowlist ( req ) ;
404- if ( ! isIpInAllowlist ) {
403+ const { isAllowed , ip } = await checkIpInAllowlist ( req ) ;
404+ if ( ! isAllowed ) {
405405 logger ( {
406406 service : "server" ,
407407 level : "error" ,
408- message : `Unauthorized IP address: ${ req . ip } ` ,
408+ message : `Unauthorized IP address: ${ ip } ` ,
409409 } ) ;
410410 return {
411411 isAuthed : false ,
@@ -523,12 +523,22 @@ const hashRequestBody = (req: FastifyRequest): string => {
523523 * @returns boolean
524524 * @async
525525 */
526- const checkIpInAllowlist = async ( req : FastifyRequest ) => {
527- const config = await getConfig ( ) ;
526+ const checkIpInAllowlist = async (
527+ req : FastifyRequest ,
528+ ) : Promise < { isAllowed : boolean ; ip : string } > => {
529+ let ip = req . ip ;
530+ const trustProxy = env . TRUST_PROXY || ! ! env . ENGINE_TIER ;
531+ if ( trustProxy && req . headers [ "cf-connecting-ip" ] ) {
532+ ip = req . headers [ "cf-connecting-ip" ] as string ;
533+ }
528534
535+ const config = await getConfig ( ) ;
529536 if ( config . ipAllowlist . length === 0 ) {
530- return true ;
537+ return { isAllowed : true , ip } ;
531538 }
532539
533- return config . ipAllowlist . includes ( req . ip ) ;
540+ return {
541+ isAllowed : config . ipAllowlist . includes ( ip ) ,
542+ ip,
543+ } ;
534544} ;
0 commit comments