Mask different situations, malformed URL, encoded, decoded, raw signatures, nested parameters, and moved to a utility file

This commit is contained in:
Salman Chishti
2025-03-12 03:17:35 -07:00
parent 5007821c77
commit 3ac34ffcb7
8 changed files with 923 additions and 320 deletions
+3 -37
View File
@@ -1,4 +1,4 @@
import {info, debug, setSecret} from '@actions/core'
import {info, debug} from '@actions/core'
import {getUserAgentString} from './user-agent'
import {NetworkError, UsageError} from './errors'
import {getCacheServiceURL} from '../config'
@@ -6,6 +6,7 @@ import {getRuntimeToken} from '../cacheUtils'
import {BearerCredentialHandler} from '@actions/http-client/lib/auth'
import {HttpClient, HttpClientResponse, HttpCodes} from '@actions/http-client'
import {CacheServiceClientJSON} from '../../generated/results/api/v1/cache.twirp-client'
import {maskSecretUrls} from './util'
// The twirp http client must implement this interface
interface Rpc {
@@ -94,7 +95,7 @@ export class CacheServiceClient implements Rpc {
debug(`[Response] - ${response.message.statusCode}`)
debug(`Headers: ${JSON.stringify(response.message.headers, null, 2)}`)
const body = JSON.parse(rawBody)
this.maskSecretUrls(body)
maskSecretUrls(body)
debug(`Body: ${JSON.stringify(body, null, 2)}`)
if (this.isSuccessStatusCode(statusCode)) {
return {response, body}
@@ -149,41 +150,6 @@ export class CacheServiceClient implements Rpc {
throw new Error(`Request failed`)
}
/**
* Masks the `sig` parameter in a URL and sets it as a secret.
* @param url The URL containing the `sig` parameter.
* @returns A masked URL where the sig parameter value is replaced with '***' if found,
* or the original URL if no sig parameter is present.
*/
maskSigUrl(url: string): string {
const sigIndex = url.indexOf('sig=')
if (sigIndex !== -1) {
const sigValue = url.substring(sigIndex + 4)
setSecret(sigValue)
return `${url.substring(0, sigIndex + 4)}***`
}
return url
}
maskSecretUrls(body): void {
if (typeof body === 'object' && body !== null) {
if (
'signed_upload_url' in body &&
typeof body.signed_upload_url === 'string'
) {
this.maskSigUrl(body.signed_upload_url)
}
if (
'signed_download_url' in body &&
typeof body.signed_download_url === 'string'
) {
this.maskSigUrl(body.signed_download_url)
}
} else {
debug('body is not an object or is null')
}
}
isSuccessStatusCode(statusCode?: number): boolean {
if (!statusCode) return false
return statusCode >= 200 && statusCode < 300