@@ -265,12 +265,12 @@ const handleWebsocketAuth = async (
265
265
req . headers . authorization = `Bearer ${ jwt } ` ;
266
266
const user = await getUser ( req ) ;
267
267
268
- const isIpInAllowlist = await checkIpInAllowlist ( req ) ;
269
- if ( ! isIpInAllowlist ) {
268
+ const { isAllowed , ip } = await checkIpInAllowlist ( req ) ;
269
+ if ( ! isAllowed ) {
270
270
logger ( {
271
271
service : "server" ,
272
272
level : "error" ,
273
- message : `Unauthorized IP address: ${ req . ip } ` ,
273
+ message : `Unauthorized IP address: ${ ip } ` ,
274
274
} ) ;
275
275
return {
276
276
isAuthed : false ,
@@ -339,12 +339,12 @@ const handleKeypairAuth = async (args: {
339
339
throw error ;
340
340
}
341
341
342
- const isIpInAllowlist = await checkIpInAllowlist ( req ) ;
343
- if ( ! isIpInAllowlist ) {
342
+ const { isAllowed , ip } = await checkIpInAllowlist ( req ) ;
343
+ if ( ! isAllowed ) {
344
344
logger ( {
345
345
service : "server" ,
346
346
level : "error" ,
347
- message : `Unauthorized IP address: ${ req . ip } ` ,
347
+ message : `Unauthorized IP address: ${ ip } ` ,
348
348
} ) ;
349
349
throw new Error (
350
350
"Unauthorized IP address. See: https://portal.thirdweb.com/engine/features/security" ,
@@ -400,12 +400,12 @@ const handleAccessToken = async (
400
400
return { isAuthed : false } ;
401
401
}
402
402
403
- const isIpInAllowlist = await checkIpInAllowlist ( req ) ;
404
- if ( ! isIpInAllowlist ) {
403
+ const { isAllowed , ip } = await checkIpInAllowlist ( req ) ;
404
+ if ( ! isAllowed ) {
405
405
logger ( {
406
406
service : "server" ,
407
407
level : "error" ,
408
- message : `Unauthorized IP address: ${ req . ip } ` ,
408
+ message : `Unauthorized IP address: ${ ip } ` ,
409
409
} ) ;
410
410
return {
411
411
isAuthed : false ,
@@ -523,12 +523,22 @@ const hashRequestBody = (req: FastifyRequest): string => {
523
523
* @returns boolean
524
524
* @async
525
525
*/
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
+ }
528
534
535
+ const config = await getConfig ( ) ;
529
536
if ( config . ipAllowlist . length === 0 ) {
530
- return true ;
537
+ return { isAllowed : true , ip } ;
531
538
}
532
539
533
- return config . ipAllowlist . includes ( req . ip ) ;
540
+ return {
541
+ isAllowed : config . ipAllowlist . includes ( ip ) ,
542
+ ip,
543
+ } ;
534
544
} ;
0 commit comments