Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:users): support client WebSocket IP from x-forwarded-for when trustProxy is enabled

johaven3e66c40b6d08
diff --git a/backend/src/applications/users/users.gateway.ts b/backend/src/applications/users/users.gateway.ts
index 37be452f..7fa590d7 100644
--- a/backend/src/applications/users/users.gateway.ts
+++ b/backend/src/applications/users/users.gateway.ts
@@ -19,6 +19,7 @@ import { JwtIdentityPayload } from '../../authentication/interfaces/jwt-payload.
import { sleep } from '../../common/functions'
import { GetWsUser } from '../../infrastructure/websocket/decorators/web-socket-user.decorator'
import { AuthenticatedSocketIO } from '../../infrastructure/websocket/interfaces/auth-socket-io.interface'
+import { getClientAddress } from '../../infrastructure/websocket/utils'
import { USER_ONLINE_STATUS } from './constants/user'
import { USER_ROOM_PREFIX, USERS_WS } from './constants/websocket'
import { EventChangeOnlineStatus, EventUpdateOnlineStatus, UserOnline } from './interfaces/websocket.interface'
@@ -47,7 +48,7 @@ export class WebSocketUsers implements OnGatewayInit, OnGatewayConnection, OnGat
)
this.sendAllOnlineUsers(socket.user.id).catch((e: Error) => this.logger.error(`${this.handleConnection.name} - ${e}`))
this.logger.log(
- `Connected: *${socket.user.login}* (${socket.user.id}) [${socket.id}] ${socket.handshake.address} ${socket.handshake.headers['user-agent']}`
+ `Connected: *${socket.user.login}* (${socket.user.id}) [${socket.id}] ${getClientAddress(socket)} ${socket.handshake.headers['user-agent']}`
)
}
@@ -55,7 +56,7 @@ export class WebSocketUsers implements OnGatewayInit, OnGatewayConnection, OnGat
socket.leave(`${USER_ROOM_PREFIX}${socket.user.id}`)
this.sendOfflineUser(socket.user.id).catch((e: Error) => this.logger.error(`${this.handleDisconnect.name} - ${e}`))
this.logger.log(
- `Disconnected: *${socket.user.login}* (${socket.user.id}) [${socket.id}] ${socket.handshake.address} ${socket.handshake.headers['user-agent']}`
+ `Disconnected: *${socket.user.login}* (${socket.user.id}) [${socket.id}] ${getClientAddress(socket)} ${socket.handshake.headers['user-agent']}`
)
}
diff --git a/backend/src/infrastructure/websocket/adapters/web-socket.adapter.ts b/backend/src/infrastructure/websocket/adapters/web-socket.adapter.ts
index ba232316..9e5a0f02 100644
--- a/backend/src/infrastructure/websocket/adapters/web-socket.adapter.ts
+++ b/backend/src/infrastructure/websocket/adapters/web-socket.adapter.ts
@@ -15,6 +15,7 @@ import { Namespace, ServerOptions, Socket } from 'socket.io'
import { USERS_WS } from '../../../applications/users/constants/websocket'
import { type JwtPayload } from '../../../authentication/interfaces/jwt-payload.interface'
import { configuration } from '../../../configuration/config.environment'
+import { getClientAddress } from '../utils'
import { ClusterAdapter } from './cluster.adapter'
import { RedisAdapter } from './redis.adapter'
@@ -97,7 +98,7 @@ export class WebSocketAdapter extends IoAdapter {
}
private onAuthError(error: string, socket: Socket, next: (err?: Error) => void) {
- this.logger.warn(`${error} - ${socket.handshake.address} ${socket.id} ${socket.handshake.url} ${socket.handshake.headers['user-agent']}`)
+ this.logger.warn(`${error} - ${getClientAddress(socket)} ${socket.id} ${socket.handshake.url} ${socket.handshake.headers['user-agent']}`)
next(new WsException('Unauthorized'))
}
}
diff --git a/backend/src/infrastructure/websocket/utils.ts b/backend/src/infrastructure/websocket/utils.ts
new file mode 100644
index 00000000..ac581362
--- /dev/null
+++ b/backend/src/infrastructure/websocket/utils.ts
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2012-2025 Johan Legrand <[email protected]>
+ * This file is part of Sync-in | The open source file sync and share solution
+ * See the LICENSE file for licensing details
+ */
+
+import { Socket } from 'socket.io'
+import { configuration } from '../../configuration/config.environment'
+
+export function getClientAddress(socket: Socket) {
+ return (
+ (configuration.server.trustProxy ? socket.handshake.headers['x-forwarded-for']?.toString().split(',')[0] : undefined) || // via proxy
+ socket.handshake.address || // fallback
+ socket.conn.remoteAddress // fallback (IPv6)
+ )
+}