Skip to content

Commit 242b743

Browse files
merllsvcAPLBot
andauthored
fix: add type checking to message sanitation (#755)
* fix: add type checking to message sanitation * fix: replace all occurences of credentials --------- Co-authored-by: svcAPLBot <[email protected]>
1 parent bd258ed commit 242b743

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/git.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { BASEURL } from './constants'
2525
import { GitPullError, HttpError, ValidationError } from './error'
2626
import { Core } from './otomi-models'
2727
import { FileMap, getFilePath, getResourceName, renderManifest, renderManifestForSecrets } from './repo'
28-
import { getSanitizedErrorMessage, removeBlankAttributes } from './utils'
28+
import { getSanitizedErrorMessage, removeBlankAttributes, sanitizeGitPassword } from './utils'
2929

3030
const debug = Debug('otomi:repo')
3131

@@ -473,7 +473,8 @@ export class Git {
473473
}
474474
} catch (e) {
475475
const sanitizedMessage = getSanitizedErrorMessage(e)
476-
debug(`${sanitizedMessage} for command ${JSON.stringify(e.task?.commands).replace(env.GIT_PASSWORD, '****')}`)
476+
const sanitizedCommands = sanitizeGitPassword(JSON.stringify(e.task?.commands))
477+
debug(`${sanitizedMessage} for command ${sanitizedCommands}`)
477478
debug('Git save error')
478479
throw new GitPullError()
479480
}

src/utils.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Cluster } from 'src/otomi-models'
2-
import { getServiceUrl } from 'src/utils'
2+
import { getSanitizedErrorMessage, getServiceUrl, sanitizeGitPassword } from 'src/utils'
3+
import { cleanEnv, GIT_PASSWORD } from './validators'
34

45
describe('Utils', () => {
56
const cluster: Cluster = {
@@ -57,4 +58,28 @@ describe('Utils', () => {
5758
expect(service.subdomain).toEqual('aa')
5859
expect(service.domain).toEqual('bb.cc.dd.ee')
5960
})
61+
62+
describe('sanitizeGitPassword should remove git credentials', () => {
63+
const env = cleanEnv({
64+
GIT_PASSWORD,
65+
})
66+
test('from strings', () => {
67+
expect(sanitizeGitPassword('test string')).toBe('test string')
68+
expect(sanitizeGitPassword(`${env.GIT_PASSWORD} test string ${env.GIT_PASSWORD}`)).toBe('**** test string ****')
69+
})
70+
test('from objects', () => {
71+
expect(sanitizeGitPassword(JSON.stringify({ test: 'some string' }))).toEqual('{"test":"some string"}')
72+
expect(sanitizeGitPassword(JSON.stringify({ test: `some string ${env.GIT_PASSWORD}` }))).toEqual(
73+
'{"test":"some string ****"}',
74+
)
75+
})
76+
test('return empty string on empty or undefined input', () => {
77+
expect(sanitizeGitPassword('')).toEqual('')
78+
expect(sanitizeGitPassword(undefined)).toEqual('')
79+
})
80+
test('extract message from exception', () => {
81+
expect(getSanitizedErrorMessage(new Error('test error'))).toEqual('test error')
82+
expect(getSanitizedErrorMessage(new Error(`test error ${env.GIT_PASSWORD}`))).toEqual('test error ****')
83+
})
84+
})
6085
})

src/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,14 @@ export const objectToYaml = (obj: Record<string, any>, indent = 4, lineWidth = 2
201201
return isEmpty(obj) ? '' : stringify(obj, { indent, lineWidth })
202202
}
203203

204+
export function sanitizeGitPassword(str?: string) {
205+
return str ? str.replaceAll(env.GIT_PASSWORD, '****') : ''
206+
}
207+
204208
export function getSanitizedErrorMessage(error) {
205-
const errorMessage = typeof error?.message === 'string' ? error.message.replace(env.GIT_PASSWORD, '****') : ''
206-
return errorMessage
209+
const message = error?.message
210+
if (!message) {
211+
return ''
212+
}
213+
return typeof message === 'string' ? sanitizeGitPassword(message) : `[unprocessable message type ${typeof message}]`
207214
}

0 commit comments

Comments
 (0)