supress secret from logs if it appears multiple times (#415)

This commit is contained in:
Tom Jenkinson 2020-09-12 22:05:57 +01:00 committed by GitHub
parent 07f53375de
commit 41aadfb60f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View File

@ -148,9 +148,9 @@ describe('util', () => {
silent: false silent: false
} }
const string = `This is an error message! It contains ${action.accessToken} and ${action.gitHubToken} and ${action.repositoryPath}` const string = `This is an error message! It contains ${action.accessToken} and ${action.gitHubToken} and ${action.repositoryPath} and ${action.accessToken} again!`
expect(suppressSensitiveInformation(string, action)).toBe( expect(suppressSensitiveInformation(string, action)).toBe(
'This is an error message! It contains *** and *** and ***' 'This is an error message! It contains *** and *** and *** and *** again!'
) )
}) })

View File

@ -1,6 +1,9 @@
import {isDebug} from '@actions/core' import {isDebug} from '@actions/core'
import {ActionInterface} from './constants' import {ActionInterface} from './constants'
const replaceAll = (input: string, find: string, replace: string): string =>
input.split(find).join(replace)
/* Utility function that checks to see if a value is undefined or not. */ /* Utility function that checks to see if a value is undefined or not. */
export const isNullOrUndefined = (value: any): boolean => export const isNullOrUndefined = (value: any): boolean =>
typeof value === 'undefined' || value === null || value === '' typeof value === 'undefined' || value === null || value === ''
@ -64,16 +67,14 @@ export const suppressSensitiveInformation = (
return value return value
} }
if (action.accessToken) { const orderedByLength = ([
value = value.replace(action.accessToken, '***') action.accessToken,
} action.gitHubToken,
action.repositoryPath
].filter(Boolean) as string[]).sort((a, b) => b.length - a.length)
if (action.gitHubToken) { for (const find of orderedByLength) {
value = value.replace(action.gitHubToken, '***') value = replaceAll(value, find, '***')
}
if (action.repositoryPath) {
value = value.replace(action.repositoryPath, '***')
} }
return value return value