File tree Expand file tree Collapse file tree 4 files changed +15
-7
lines changed
Expand file tree Collapse file tree 4 files changed +15
-7
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ const logger = require("@logger/logger").default
99const { attachReadRouteHandlerWrapper } = require ( "@middleware/routeHandler" )
1010
1111const FRONTEND_URL = config . get ( "app.frontendUrl" )
12- const { isSecure } = require ( "@utils/auth-utils" )
12+ const { isSecure, getUserIPAddress } = require ( "@utils/auth-utils" )
1313
1414const {
1515 EmailSchema,
@@ -106,7 +106,7 @@ class AuthRouter {
106106 message : `Invalid request format: ${ error . message } ` ,
107107 } )
108108 const email = rawEmail . toLowerCase ( )
109- const userIp = isSecure ? req . get ( "cf-connecting-ip" ) : req . ip
109+ const userIp = getUserIPAddress ( req )
110110 const userInfo = (
111111 await this . authService . verifyOtp ( { email, otp, clientIp : userIp } )
112112 ) . value
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import UserSessionData from "@classes/UserSessionData"
1414import DatabaseError from "@root/errors/DatabaseError"
1515import { isError , RequestHandler } from "@root/types"
1616import { nameAnonymousMethods } from "@root/utils/apm-utils"
17- import { isSecure } from "@root/utils/auth-utils"
17+ import { getUserIPAddress } from "@root/utils/auth-utils"
1818import {
1919 VerifyEmailOtpSchema ,
2020 VerifyMobileNumberOtpSchema ,
@@ -84,7 +84,7 @@ export class UsersRouter {
8484 const userId = userSessionData . isomerUserId
8585 const parsedEmail = email . toLowerCase ( )
8686
87- const userIp = isSecure ? req . get ( "cf-connecting-ip" ) : req . ip
87+ const userIp = getUserIPAddress ( req )
8888 return this . usersService
8989 . verifyEmailOtp ( parsedEmail , otp , userIp )
9090 . andThen ( ( ) =>
@@ -144,7 +144,7 @@ export class UsersRouter {
144144 const { userSessionData } = res . locals
145145 const userId = userSessionData . isomerUserId
146146
147- const userIp = isSecure ? req . get ( "cf-connecting-ip" ) : req . ip
147+ const userIp = getUserIPAddress ( req )
148148 return this . usersService
149149 . verifyMobileOtp ( mobile , otp , userIp )
150150 . andThen ( ( ) =>
Original file line number Diff line number Diff line change 11import rateLimit from "express-rate-limit"
22
33import { BaseIsomerError } from "@root/errors/BaseError"
4- import { isSecure } from "@root/utils/auth-utils"
4+ import { getUserIPAddress } from "@root/utils/auth-utils"
55
66const DEFAULT_AUTH_TOKEN_EXPIRY_MILLISECONDS = 900000
77
@@ -21,7 +21,7 @@ export const rateLimiter = rateLimit({
2121 // We know that this key exists in a secure env (Cloudflare)
2222 // See https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-connecting-ip
2323 keyGenerator : ( req ) => {
24- const userIp = isSecure ? req . get ( "cf-connecting-ip" ) : req . ip
24+ const userIp = getUserIPAddress ( req )
2525 if ( ! userIp ) {
2626 // This should never happen, but if it does, we should know about it
2727 throw new BaseIsomerError ( {
Original file line number Diff line number Diff line change @@ -4,6 +4,14 @@ const NODE_ENV = config.get("env")
44
55const isSecure = NODE_ENV !== "dev" && NODE_ENV !== "test"
66
7+ // FIXME: This makes a strong assumption that the app is always behind
8+ // Cloudflare, but may not necessarily be the case when Cloudflare is disabled.
9+ // Fix this to fallback to other headers or req.ip if Cloudflare headers are not
10+ // present.
11+ const getUserIPAddress = ( req ) =>
12+ isSecure ? req . get ( "cf-connecting-ip" ) : req . ip
13+
714module . exports = {
815 isSecure,
16+ getUserIPAddress,
917}
You can’t perform that action at this time.
0 commit comments