diff --git a/lib/entry-points.js b/lib/entry-points.js index 19f324e0f..681f56b89 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -170765,9 +170765,13 @@ function getApiClientWithExternalAuth(apiDetails, proxy) { return createApiClientWithDetails(apiDetails, { allowExternal: true, proxy }); } function getAuthorizationHeaderFor(logger2, apiDetails, url2) { + const token = apiDetails.auth?.trim(); + if (token === void 0 || token === "") { + return void 0; + } if (url2.startsWith(`${apiDetails.url}/`) || apiDetails.apiURL && url2.startsWith(`${apiDetails.apiURL}/`)) { logger2.debug(`Providing an authorization token.`); - return `token ${apiDetails.auth}`; + return `token ${token}`; } logger2.debug(`Not using an authorization token.`); return void 0; diff --git a/src/api-client.test.ts b/src/api-client.test.ts index 12fe81985..1a37425e4 100644 --- a/src/api-client.test.ts +++ b/src/api-client.test.ts @@ -8,7 +8,14 @@ import * as actionsUtil from "./actions-util"; import * as api from "./api-client"; import { DO_NOT_RETRY_STATUSES } from "./api-client"; import { ActionsEnvVars, RegistryProxyVars } from "./environment"; -import { callee, getTestEnv, setupTests } from "./testing-utils"; +import { GITHUB_DOTCOM_API_URL, GITHUB_DOTCOM_URL } from "./github"; +import { + callee, + getTestEnv, + RecordingLogger, + SAMPLE_DOTCOM_API_DETAILS, + setupTests, +} from "./testing-utils"; import * as util from "./util"; setupTests(test); @@ -284,3 +291,74 @@ test("makeProxyRequestOptions - returns fetch with custom proxy", async (t) => { githubUtils.defaults.request, ); }); + +test("getAuthorizationHeaderFor - returns undefined if there's no token", (t) => { + const logger = new RecordingLogger(); + + t.is( + api.getAuthorizationHeaderFor( + logger, + { ...SAMPLE_DOTCOM_API_DETAILS, auth: undefined }, + `${GITHUB_DOTCOM_URL}/github`, + ), + undefined, + ); + t.is( + api.getAuthorizationHeaderFor( + logger, + { ...SAMPLE_DOTCOM_API_DETAILS, auth: "" }, + `${GITHUB_DOTCOM_URL}/github`, + ), + undefined, + ); + t.is( + api.getAuthorizationHeaderFor( + logger, + { ...SAMPLE_DOTCOM_API_DETAILS, auth: " " }, + `${GITHUB_DOTCOM_URL}/github`, + ), + undefined, + ); +}); + +test("getAuthorizationHeaderFor - returns header value with token if url is allowed", (t) => { + const logger = new RecordingLogger(); + + t.is( + api.getAuthorizationHeaderFor( + logger, + SAMPLE_DOTCOM_API_DETAILS, + `${GITHUB_DOTCOM_URL}/github`, + ), + `token ${SAMPLE_DOTCOM_API_DETAILS.auth}`, + ); + t.is( + api.getAuthorizationHeaderFor( + logger, + SAMPLE_DOTCOM_API_DETAILS, + `${GITHUB_DOTCOM_API_URL}/github`, + ), + `token ${SAMPLE_DOTCOM_API_DETAILS.auth}`, + ); +}); + +test("getAuthorizationHeaderFor - returns undefined if url is not allowed", (t) => { + const logger = new RecordingLogger(); + + t.is( + api.getAuthorizationHeaderFor( + logger, + SAMPLE_DOTCOM_API_DETAILS, + `https://localhost/github`, + ), + undefined, + ); + t.is( + api.getAuthorizationHeaderFor( + logger, + SAMPLE_DOTCOM_API_DETAILS, + `https://api.localhost/github`, + ), + undefined, + ); +}); diff --git a/src/api-client.ts b/src/api-client.ts index 4d0a99920..b75aacbb7 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -195,6 +195,12 @@ export function getAuthorizationHeaderFor( apiDetails: GitHubApiDetails, url: string, ): string | undefined { + // We don't have to check the `url` if we don't have a token to begin with. + const token = apiDetails.auth?.trim(); + if (token === undefined || token === "") { + return undefined; + } + // We only want to provide an authorization header if we are downloading // from the same GitHub instance the Action is running on. // This avoids leaking Enterprise tokens to dotcom. @@ -203,7 +209,7 @@ export function getAuthorizationHeaderFor( (apiDetails.apiURL && url.startsWith(`${apiDetails.apiURL}/`)) ) { logger.debug(`Providing an authorization token.`); - return `token ${apiDetails.auth}`; + return `token ${token}`; } logger.debug(`Not using an authorization token.`);