Skip to content

Commit 41aadfb

Browse files
authored
supress secret from logs if it appears multiple times (JamesIves#415)
1 parent 07f5337 commit 41aadfb

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

__tests__/util.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ describe('util', () => {
148148
silent: false
149149
}
150150

151-
const string = `This is an error message! It contains ${action.accessToken} and ${action.gitHubToken} and ${action.repositoryPath}`
151+
const string = `This is an error message! It contains ${action.accessToken} and ${action.gitHubToken} and ${action.repositoryPath} and ${action.accessToken} again!`
152152
expect(suppressSensitiveInformation(string, action)).toBe(
153-
'This is an error message! It contains *** and *** and ***'
153+
'This is an error message! It contains *** and *** and *** and *** again!'
154154
)
155155
})
156156

src/util.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {isDebug} from '@actions/core'
22
import {ActionInterface} from './constants'
33

4+
const replaceAll = (input: string, find: string, replace: string): string =>
5+
input.split(find).join(replace)
6+
47
/* Utility function that checks to see if a value is undefined or not. */
58
export const isNullOrUndefined = (value: any): boolean =>
69
typeof value === 'undefined' || value === null || value === ''
@@ -64,16 +67,14 @@ export const suppressSensitiveInformation = (
6467
return value
6568
}
6669

67-
if (action.accessToken) {
68-
value = value.replace(action.accessToken, '***')
69-
}
70-
71-
if (action.gitHubToken) {
72-
value = value.replace(action.gitHubToken, '***')
73-
}
70+
const orderedByLength = ([
71+
action.accessToken,
72+
action.gitHubToken,
73+
action.repositoryPath
74+
].filter(Boolean) as string[]).sort((a, b) => b.length - a.length)
7475

75-
if (action.repositoryPath) {
76-
value = value.replace(action.repositoryPath, '***')
76+
for (const find of orderedByLength) {
77+
value = replaceAll(value, find, '***')
7778
}
7879

7980
return value

0 commit comments

Comments
 (0)