masks the whole URL, update tests

This commit is contained in:
Salman Chishti committed 2025-03-10 06:47:52 -07:00
1 parent 5040e42c6c
commit 2a461120c5
2 files changed
+88 -56

No files matched your search

+66 -38
View File
@@ -1,9 +1,6 @@
import {ArtifactHttpClient} from '../src/internal/shared/artifact-twirp-client' import {ArtifactHttpClient} from '../src/internal/shared/artifact-twirp-client'
import {setSecret, debug} from '@actions/core' import {setSecret} from '@actions/core'
import { import {CreateArtifactResponse} from '../src/generated/results/api/v1/artifact'
CreateArtifactResponse,
GetSignedArtifactURLResponse
} from '../src/generated/results/api/v1/artifact'
jest.mock('@actions/core', () => ({ jest.mock('@actions/core', () => ({
setSecret: jest.fn(), setSecret: jest.fn(),
@@ -26,70 +23,101 @@ describe('ArtifactHttpClient', () => {
delete process.env['ACTIONS_RESULTS_URL'] delete process.env['ACTIONS_RESULTS_URL']
}) })
describe('maskSigUrl', () => {
it('should mask the sig parameter and set it as a secret', () => {
const url =
'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=secret-token'
const maskedUrl = client.maskSigUrl(url)
expect(setSecret).toHaveBeenCalledWith('secret-token')
expect(maskedUrl).toBe(
'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=***'
)
})
it('should return the original URL if no sig parameter is found', () => {
const url = 'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z'
const maskedUrl = client.maskSigUrl(url)
expect(setSecret).not.toHaveBeenCalled()
expect(maskedUrl).toBe(url)
})
it('should handle sig parameter at the end of the URL', () => {
const url = 'https://example.com/upload?param1=value&sig=secret-token'
const maskedUrl = client.maskSigUrl(url)
expect(setSecret).toHaveBeenCalledWith('secret-token')
expect(maskedUrl).toBe('https://example.com/upload?param1=value&sig=***')
})
it('should handle sig parameter in the middle of the URL', () => {
const url = 'https://example.com/upload?sig=secret-token&param2=value'
const maskedUrl = client.maskSigUrl(url)
expect(setSecret).toHaveBeenCalledWith('secret-token&param2=value')
expect(maskedUrl).toBe('https://example.com/upload?sig=***')
})
})
describe('maskSecretUrls', () => { describe('maskSecretUrls', () => {
it('should mask signed_upload_url', () => { it('should mask signed_upload_url', () => {
const response: CreateArtifactResponse = { const spy = jest.spyOn(client, 'maskSigUrl')
const response = {
ok: true, ok: true,
signedUploadUrl: signed_upload_url:
'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=secret-token' 'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=secret-token'
} }
client.maskSecretUrls(response) client.maskSecretUrls(response)
expect(setSecret).toHaveBeenCalledWith('secret-token') expect(spy).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith( 'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=secret-token'
'Masked signed_upload_url: https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=***'
) )
}) })
it('should mask signed_download_url', () => { it('should mask signed_download_url', () => {
const response: GetSignedArtifactURLResponse = { const spy = jest.spyOn(client, 'maskSigUrl')
signedUrl: const response = {
signed_url:
'https://example.com/download?se=2025-03-05T16%3A47%3A23Z&sig=secret-token' 'https://example.com/download?se=2025-03-05T16%3A47%3A23Z&sig=secret-token'
} }
client.maskSecretUrls(response) client.maskSecretUrls(response)
expect(setSecret).toHaveBeenCalledWith('secret-token') expect(spy).toHaveBeenCalledWith(
expect(debug).toHaveBeenCalledWith( 'https://example.com/download?se=2025-03-05T16%3A47%3A23Z&sig=secret-token'
'Masked signed_url: https://example.com/download?se=2025-03-05T16%3A47%3A23Z&sig=***'
) )
}) })
it('should not call setSecret if URLs are missing', () => { it('should not call maskSigUrl if URLs are missing', () => {
const spy = jest.spyOn(client, 'maskSigUrl')
const response = {} as CreateArtifactResponse const response = {} as CreateArtifactResponse
client.maskSecretUrls(response) client.maskSecretUrls(response)
expect(setSecret).not.toHaveBeenCalled() expect(spy).not.toHaveBeenCalled()
}) })
it('should mask only the sensitive token part of signed_upload_url', () => { it('should handle both URL types when present', () => {
const response: CreateArtifactResponse = { const spy = jest.spyOn(client, 'maskSigUrl')
ok: true, const response = {
signedUploadUrl: signed_upload_url: 'https://example.com/upload?sig=secret-token1',
'https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=secret-token' signed_url: 'https://example.com/download?sig=secret-token2'
} }
client.maskSecretUrls(response) client.maskSecretUrls(response)
expect(setSecret).toHaveBeenCalledWith('secret-token') expect(spy).toHaveBeenCalledTimes(2)
expect(debug).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
'Masked signed_upload_url: https://example.com/upload?se=2025-03-05T16%3A47%3A23Z&sig=***' 'https://example.com/upload?sig=secret-token1'
) )
}) expect(spy).toHaveBeenCalledWith(
'https://example.com/download?sig=secret-token2'
it('should mask only the sensitive token part of signed_download_url', () => {
const response: GetSignedArtifactURLResponse = {
signedUrl:
'https://example.com/download?se=2025-03-05T16%3A47%3A23Z&sig=secret-token'
}
client.maskSecretUrls(response)
expect(setSecret).toHaveBeenCalledWith('secret-token')
expect(debug).toHaveBeenCalledWith(
'Masked signed_url: https://example.com/download?se=2025-03-05T16%3A47%3A23Z&sig=***'
) )
}) })
}) })
+22 -18
View File
@@ -5,10 +5,6 @@ import {ArtifactServiceClientJSON} from '../../generated'
import {getResultsServiceUrl, getRuntimeToken} from './config' import {getResultsServiceUrl, getRuntimeToken} from './config'
import {getUserAgentString} from './user-agent' import {getUserAgentString} from './user-agent'
import {NetworkError, UsageError} from './errors' import {NetworkError, UsageError} from './errors'
import {
CreateArtifactResponse,
GetSignedArtifactURLResponse
} from '../../generated/results/api/v1/artifact'
// The twirp http client must implement this interface // The twirp http client must implement this interface
interface Rpc { interface Rpc {
@@ -77,24 +73,32 @@ export class ArtifactHttpClient implements Rpc {
/** /**
* Masks the `sig` parameter in a URL and sets it as a secret. * Masks the `sig` parameter in a URL and sets it as a secret.
* @param url The URL containing the `sig` parameter. * @param url The URL containing the `sig` parameter.
* @param urlType The type of the URL (e.g., 'signed_upload_url', 'signed_download_url'). * @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, urlType: string): void { maskSigUrl(url: string): string {
const sigMatch = url.match(/[?&]sig=([^&]+)/) const sigIndex = url.indexOf('sig=')
if (sigMatch) { if (sigIndex !== -1) {
setSecret(sigMatch[1]) const sigValue = url.substring(sigIndex + 4)
debug(`Masked ${urlType}: ${url.replace(sigMatch[1], '***')}`) setSecret(sigValue)
return `${url.substring(0, sigIndex + 4)}***`
} }
return url
} }
maskSecretUrls( maskSecretUrls(body): void {
body: CreateArtifactResponse | GetSignedArtifactURLResponse if (typeof body === 'object' && body !== null) {
): void { if (
if ('signedUploadUrl' in body && body.signedUploadUrl) { 'signed_upload_url' in body &&
this.maskSigUrl(body.signedUploadUrl, 'signed_upload_url') typeof body.signed_upload_url === 'string'
} ) {
if ('signedUrl' in body && body.signedUrl) { this.maskSigUrl(body.signed_upload_url)
this.maskSigUrl(body.signedUrl, 'signed_url') }
if ('signed_url' in body && typeof body.signed_url === 'string') {
this.maskSigUrl(body.signed_url)
}
} else {
debug('body is not an object or is null')
} }
} }