Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:redis): improve password redaction in connection logs (#268)
31412eb6b59a876
diff --git a/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts b/backend/src/infrastructure/cache/adapters/redis-cache.adapter.tsindex 64bf920e..3c4cf1ec 100644--- a/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts+++ b/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts@@ -3,6 +3,7 @@ import { RedisClientOptions } from '@redis/client'import { createClient, RedisClientType } from 'redis'import { createCacheKeySlug } from '../../../common/shared'import { configuration } from '../../../configuration/config.environment'+import { redactRedisUrl } from '../../utils'import { Cache } from '../cache.service'@Injectable()@@ -11,6 +12,7 @@ export class RedisCacheAdapter implements Cache {infiniteExpiration = -1private readonly logger = new Logger(Cache.name.toUpperCase())private readonly client: RedisClientType+ private readonly redactedRedisUrl = redactRedisUrl(configuration.cache.redis)private readonly reconnectOptions = { maxAttempts: 3, minConnectDelay: 1000, maxConnectDelay: 2000 }constructor() {@@ -22,7 +24,7 @@ export class RedisCacheAdapter implements Cache {async onModuleInit() {this.client.on('error', (e: Error) => this.logger.error(e.message || e))- this.client.on('ready', () => this.logger.log(`Connected to Redis Server at ${this.client.options.url}`))+ this.client.on('ready', () => this.logger.log(`Connected to Redis Server at ${this.redactedRedisUrl}`))this.client.connect().catch((e: Error) => this.logger.error(e))}diff --git a/backend/src/infrastructure/utils.spec.ts b/backend/src/infrastructure/utils.spec.tsnew file mode 100644index 00000000..7d155d5e--- /dev/null+++ b/backend/src/infrastructure/utils.spec.ts@@ -0,0 +1,13 @@+import { describe, expect, it } from 'vitest'+import { redactRedisUrl } from './utils'++describe(redactRedisUrl.name, () => {+ it.each([+ ['redis://127.0.0.1:6379', 'redis://127.0.0.1:6379'],+ ['redis://:[email protected]:6379', 'redis://:********@127.0.0.1:6379'],+ ['redis://default:[email protected]:6379', 'redis://default:********@127.0.0.1:6379'],+ ['rediss://user:p%[email protected]', 'rediss://user:********@redis.example.com']+ ])('redacts the password from %s', (url, expected) => {+ expect(redactRedisUrl(url)).toBe(expected)+ })+})diff --git a/backend/src/infrastructure/utils.ts b/backend/src/infrastructure/utils.tsnew file mode 100644index 00000000..c53ad5ba--- /dev/null+++ b/backend/src/infrastructure/utils.ts@@ -0,0 +1,7 @@+export function redactRedisUrl(url: string): string {+ const parsedUrl = new URL(url)+ if (!parsedUrl.password) return url++ parsedUrl.password = '********'+ return parsedUrl.toString()+}diff --git a/backend/src/infrastructure/websocket/adapters/redis.adapter.ts b/backend/src/infrastructure/websocket/adapters/redis.adapter.tsindex 2bd46578..e0985737 100644--- a/backend/src/infrastructure/websocket/adapters/redis.adapter.ts+++ b/backend/src/infrastructure/websocket/adapters/redis.adapter.ts@@ -2,6 +2,8 @@ import { Logger } from '@nestjs/common'import { IoAdapter } from '@nestjs/platform-socket.io'import { ServerOptions } from 'socket.io'import { loadOptionalModule } from '../../../common/functions'+import { redactRedisUrl } from '../../utils'+import type { WebSocketConfig } from '../web-socket.config'export class RedisAdapter extends IoAdapter {private readonly logger = new Logger('WebSocketAdapter')@@ -18,15 +20,16 @@ export class RedisAdapter extends IoAdapter {}}- async connectToRedis(redisUrl: string): Promise<void> {+ async connectToRedis(redisUrl: WebSocketConfig['redis']): Promise<void> {+ const redactedRedisUrl = redactRedisUrl(redisUrl)const { createAdapter } = await loadOptionalModule('@socket.io/redis-adapter')const { createClient } = await loadOptionalModule('redis')const pubClient = createClient({ url: redisUrl, socket: { noDelay: true, reconnectStrategy: this.reconnectStrategy } })const subClient = pubClient.duplicate()pubClient.on('error', (e: Error) => this.logger.error(`PubClient: ${e.message || e}`))- pubClient.on('ready', () => this.logger.log(`PubClient: Connected to Redis Server at ${pubClient.options.url}`))+ pubClient.on('ready', () => this.logger.log(`PubClient: Connected to Redis Server at ${redactedRedisUrl}`))subClient.on('error', (e: Error) => this.logger.error(`SubClient: ${e.message || e}`))- subClient.on('ready', () => this.logger.log(`SubClient: Connected to Redis Server at ${subClient.options.url}`))+ subClient.on('ready', () => this.logger.log(`SubClient: Connected to Redis Server at ${redactedRedisUrl}`))pubClient.connect()subClient.connect()this.adapterConstructor = createAdapter(pubClient, subClient)