Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logging): sanitize log message to avoid printing db connection string #3652

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions packages/server/modules/multiregion/dbSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { ensureError, MaybeNullOrUndefined } from '@speckle/shared'
import { isDevOrTestEnv, isTestEnv } from '@/modules/shared/helpers/envHelper'
import { migrateDbToLatest } from '@/db/migrations'
import { get } from 'lodash'

let getter: GetProjectDb | undefined = undefined

Expand Down Expand Up @@ -203,6 +204,12 @@ interface ReplicationArgs {
regionName: string
}

const sanitizeError = (err: unknown): unknown => {
if (!err) return err
if (get(err, 'where').includes('password='))
return { ...err, where: '[REDACTED AS IT CONTAINS CONNECTION STRING]' }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better using ^(.*)(password\=[^\s]+)(.*)$ and replacing capture group $2 with password=[REDACTED]

}

const setUpUserReplication = async ({
from,
to,
Expand All @@ -219,11 +226,18 @@ const setUpUserReplication = async ({
throw new DatabaseError(
'Could not create publication {pubName} when setting up user replication for region {regionName}',
{
cause: ensureError(err, 'Unknown database error when creating publication'),
cause: ensureError(
sanitizeError(err),
'Unknown database error when creating publication'
),
info: { pubName, regionName }
}
)
if (!err.message.includes('already exists')) throw err
if (
!err.message.includes('already exists') &&
!err.message.includes('duplicate key value violates unique constraint')
)
throw sanitizeError(err)
}

const fromUrl = new URL(
Expand Down Expand Up @@ -254,11 +268,18 @@ const setUpUserReplication = async ({
throw new DatabaseError(
'Could not create subscription {subName} to {pubName} when setting up user replication for region {regionName}',
{
cause: ensureError(err, 'Unknown database error when creating subscription'),
cause: ensureError(
sanitizeError(err),
'Unknown database error when creating subscription'
),
info: { subName, pubName, regionName }
}
)
if (!err.message.includes('already exists')) throw err
if (
!err.message.includes('already exists') &&
!err.message.includes('duplicate key value violates unique constraint')
)
throw sanitizeError(err)
}
}

Expand All @@ -278,11 +299,18 @@ const setUpProjectReplication = async ({
throw new DatabaseError(
'Could not create publication {pubName} when setting up project replication for region {regionName}',
{
cause: ensureError(err, 'Unknown database error when creating publication'),
cause: ensureError(
sanitizeError(err),
'Unknown database error when creating publication'
),
info: { pubName, regionName }
}
)
if (!err.message.includes('already exists')) throw err
if (
!err.message.includes('already exists') &&
!err.message.includes('duplicate key value violates unique constraint')
)
throw sanitizeError(err)
}

const fromUrl = new URL(
Expand Down Expand Up @@ -313,10 +341,17 @@ const setUpProjectReplication = async ({
throw new DatabaseError(
'Could not create subscription {subName} to {pubName} when setting up project replication for region {regionName}',
{
cause: ensureError(err, 'Unknown database error when creating subscription'),
cause: ensureError(
sanitizeError(err),
'Unknown database error when creating subscription'
),
info: { subName, pubName, regionName }
}
)
if (!err.message.includes('already exists')) throw err
if (
!err.message.includes('already exists') &&
!err.message.includes('duplicate key value violates unique constraint')
)
throw sanitizeError(err)
}
}