Skip to content

Commit 39842d8

Browse files
authored
Merge pull request #3146 from github/mbg/start-proxy/authenticate
Provide `Authorization` header when downloading `update-job-proxy`
2 parents 435f474 + 6ccec2a commit 39842d8

File tree

9 files changed

+137
-44
lines changed

9 files changed

+137
-44
lines changed

lib/analyze-action.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-action.js

Lines changed: 25 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as retry from "@octokit/plugin-retry";
44
import consoleLogLevel from "console-log-level";
55

66
import { getActionVersion, getRequiredInput } from "./actions-util";
7+
import { Logger } from "./logging";
78
import { getRepositoryNwo, RepositoryNwo } from "./repository";
89
import {
910
ConfigurationError,
@@ -54,7 +55,7 @@ function createApiClientWithDetails(
5455
);
5556
}
5657

57-
export function getApiDetails() {
58+
export function getApiDetails(): GitHubApiDetails {
5859
return {
5960
auth: getRequiredInput("token"),
6061
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
@@ -72,6 +73,36 @@ export function getApiClientWithExternalAuth(
7273
return createApiClientWithDetails(apiDetails, { allowExternal: true });
7374
}
7475

76+
/**
77+
* Gets a value for the `Authorization` header for a request to `url`; or `undefined` if the
78+
* `Authorization` header should not be set for `url`.
79+
*
80+
* @param logger The logger to use for debugging messages.
81+
* @param apiDetails Details of the GitHub API we are using.
82+
* @param url The URL for which we want to add an `Authorization` header.
83+
*
84+
* @returns The value for the `Authorization` header or `undefined` if it shouldn't be populated.
85+
*/
86+
export function getAuthorizationHeaderFor(
87+
logger: Logger,
88+
apiDetails: GitHubApiDetails,
89+
url: string,
90+
): string | undefined {
91+
// We only want to provide an authorization header if we are downloading
92+
// from the same GitHub instance the Action is running on.
93+
// This avoids leaking Enterprise tokens to dotcom.
94+
if (
95+
url.startsWith(`${apiDetails.url}/`) ||
96+
(apiDetails.apiURL && url.startsWith(`${apiDetails.apiURL}/`))
97+
) {
98+
logger.debug(`Providing an authorization token.`);
99+
return `token ${apiDetails.auth}`;
100+
}
101+
102+
logger.debug(`Not using an authorization token.`);
103+
return undefined;
104+
}
105+
75106
let cachedGitHubVersion: GitHubVersion | undefined = undefined;
76107

77108
export async function getGitHubVersionFromApi(

src/setup-codeql.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -567,21 +567,17 @@ export const downloadCodeQL = async function (
567567
const headers: OutgoingHttpHeaders = {
568568
accept: "application/octet-stream",
569569
};
570-
// We only want to provide an authorization header if we are downloading
571-
// from the same GitHub instance the Action is running on.
572-
// This avoids leaking Enterprise tokens to dotcom.
573-
// We also don't want to send an authorization header if there's already a token provided in the URL.
574570
let authorization: string | undefined = undefined;
571+
572+
// We don't want to send an authorization header if there's already a token provided in the URL.
575573
if (searchParams.has("token")) {
576574
logger.debug("CodeQL tools URL contains an authorization token.");
577-
} else if (
578-
codeqlURL.startsWith(`${apiDetails.url}/`) ||
579-
(apiDetails.apiURL && codeqlURL.startsWith(`${apiDetails.apiURL}/`))
580-
) {
581-
logger.debug("Providing an authorization token to download CodeQL tools.");
582-
authorization = `token ${apiDetails.auth}`;
583575
} else {
584-
logger.debug("Downloading CodeQL tools without an authorization token.");
576+
authorization = api.getAuthorizationHeaderFor(
577+
logger,
578+
apiDetails,
579+
codeqlURL,
580+
);
585581
}
586582

587583
const toolcacheInfo = getToolcacheDestinationInfo(

src/start-proxy-action.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as toolcache from "@actions/tool-cache";
66
import { pki } from "node-forge";
77

88
import * as actionsUtil from "./actions-util";
9+
import { getApiDetails, getAuthorizationHeaderFor } from "./api-client";
910
import { getActionsLogger, Logger } from "./logging";
1011
import {
1112
Credential,
@@ -192,10 +193,16 @@ async function getProxyBinaryPath(logger: Logger): Promise<string> {
192193

193194
let proxyBin = toolcache.find(proxyFileName, proxyInfo.version);
194195
if (!proxyBin) {
196+
const apiDetails = getApiDetails();
197+
const authorization = getAuthorizationHeaderFor(
198+
logger,
199+
apiDetails,
200+
proxyInfo.url,
201+
);
195202
const temp = await toolcache.downloadTool(
196203
proxyInfo.url,
197204
undefined,
198-
undefined,
205+
authorization,
199206
{
200207
accept: "application/octet-stream",
201208
},

0 commit comments

Comments
 (0)