Skip to content

Commit 92efd5c

Browse files
committed
Check auth value in getAuthorizationHeaderFor
Also add some tests for it
1 parent b43adc3 commit 92efd5c

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/api-client.test.ts‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import * as actionsUtil from "./actions-util";
88
import * as api from "./api-client";
99
import { DO_NOT_RETRY_STATUSES } from "./api-client";
1010
import { ActionsEnvVars, RegistryProxyVars } from "./environment";
11-
import { callee, getTestEnv, setupTests } from "./testing-utils";
11+
import { GITHUB_DOTCOM_API_URL, GITHUB_DOTCOM_URL } from "./github";
12+
import {
13+
callee,
14+
getTestEnv,
15+
RecordingLogger,
16+
SAMPLE_DOTCOM_API_DETAILS,
17+
setupTests,
18+
} from "./testing-utils";
1219
import * as util from "./util";
1320

1421
setupTests(test);
@@ -284,3 +291,74 @@ test("makeProxyRequestOptions - returns fetch with custom proxy", async (t) => {
284291
githubUtils.defaults.request,
285292
);
286293
});
294+
295+
test("getAuthorizationHeaderFor - returns undefined if there's no token", (t) => {
296+
const logger = new RecordingLogger();
297+
298+
t.is(
299+
api.getAuthorizationHeaderFor(
300+
logger,
301+
{ ...SAMPLE_DOTCOM_API_DETAILS, auth: undefined },
302+
`${GITHUB_DOTCOM_URL}/github`,
303+
),
304+
undefined,
305+
);
306+
t.is(
307+
api.getAuthorizationHeaderFor(
308+
logger,
309+
{ ...SAMPLE_DOTCOM_API_DETAILS, auth: "" },
310+
`${GITHUB_DOTCOM_URL}/github`,
311+
),
312+
undefined,
313+
);
314+
t.is(
315+
api.getAuthorizationHeaderFor(
316+
logger,
317+
{ ...SAMPLE_DOTCOM_API_DETAILS, auth: " " },
318+
`${GITHUB_DOTCOM_URL}/github`,
319+
),
320+
undefined,
321+
);
322+
});
323+
324+
test("getAuthorizationHeaderFor - returns header value with token if url is allowed", (t) => {
325+
const logger = new RecordingLogger();
326+
327+
t.is(
328+
api.getAuthorizationHeaderFor(
329+
logger,
330+
SAMPLE_DOTCOM_API_DETAILS,
331+
`${GITHUB_DOTCOM_URL}/github`,
332+
),
333+
`token ${SAMPLE_DOTCOM_API_DETAILS.auth}`,
334+
);
335+
t.is(
336+
api.getAuthorizationHeaderFor(
337+
logger,
338+
SAMPLE_DOTCOM_API_DETAILS,
339+
`${GITHUB_DOTCOM_API_URL}/github`,
340+
),
341+
`token ${SAMPLE_DOTCOM_API_DETAILS.auth}`,
342+
);
343+
});
344+
345+
test("getAuthorizationHeaderFor - returns undefined if url is not allowed", (t) => {
346+
const logger = new RecordingLogger();
347+
348+
t.is(
349+
api.getAuthorizationHeaderFor(
350+
logger,
351+
SAMPLE_DOTCOM_API_DETAILS,
352+
`https://localhost/github`,
353+
),
354+
undefined,
355+
);
356+
t.is(
357+
api.getAuthorizationHeaderFor(
358+
logger,
359+
SAMPLE_DOTCOM_API_DETAILS,
360+
`https://api.localhost/github`,
361+
),
362+
undefined,
363+
);
364+
});

‎src/api-client.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ export function getAuthorizationHeaderFor(
195195
apiDetails: GitHubApiDetails,
196196
url: string,
197197
): string | undefined {
198+
// We don't have to check the `url` if we don't have a token to begin with.
199+
const token = apiDetails.auth?.trim();
200+
if (token === undefined || token === "") {
201+
return undefined;
202+
}
203+
198204
// We only want to provide an authorization header if we are downloading
199205
// from the same GitHub instance the Action is running on.
200206
// This avoids leaking Enterprise tokens to dotcom.
@@ -203,7 +209,7 @@ export function getAuthorizationHeaderFor(
203209
(apiDetails.apiURL && url.startsWith(`${apiDetails.apiURL}/`))
204210
) {
205211
logger.debug(`Providing an authorization token.`);
206-
return `token ${apiDetails.auth}`;
212+
return `token ${token}`;
207213
}
208214

209215
logger.debug(`Not using an authorization token.`);

0 commit comments

Comments
 (0)