Skip to content

Commit 9fccf27

Browse files
committed
Warn if a private registry configuration uses a PAT, but has no username
1 parent c12cf8d commit 9fccf27

File tree

3 files changed

+130
-19
lines changed

3 files changed

+130
-19
lines changed

lib/start-proxy-action.js

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

src/start-proxy.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { KnownLanguage } from "./languages";
77
import { getRunnerLogger } from "./logging";
88
import * as startProxyExports from "./start-proxy";
99
import { parseLanguage } from "./start-proxy";
10-
import { setupTests } from "./testing-utils";
10+
import {
11+
checkExpectedLogMessages,
12+
getRecordingLogger,
13+
makeTestToken,
14+
setupTests,
15+
} from "./testing-utils";
1116

1217
setupTests(test);
1318

@@ -174,6 +179,37 @@ test("getCredentials throws an error when non-printable characters are used", as
174179
}
175180
});
176181

182+
test("getCredentials logs a warning when a PAT is used without a username", async (t) => {
183+
const loggedMessages = [];
184+
const logger = getRecordingLogger(loggedMessages);
185+
const likelyWrongCredentials = toEncodedJSON([
186+
{
187+
type: "git_server",
188+
host: "https://github.com/",
189+
password: `ghp_${makeTestToken()}`,
190+
},
191+
]);
192+
193+
const results = startProxyExports.getCredentials(
194+
logger,
195+
undefined,
196+
likelyWrongCredentials,
197+
undefined,
198+
);
199+
200+
// The configuration should be accepted, despite the likely problem.
201+
t.assert(results);
202+
t.is(results.length, 1);
203+
t.is(results[0].type, "git_server");
204+
t.is(results[0].host, "https://github.com/");
205+
t.assert(results[0].password?.startsWith("ghp_"));
206+
207+
// A warning should have been logged.
208+
checkExpectedLogMessages(t, loggedMessages, [
209+
"using a GitHub Personal Access Token (PAT), but no username was provided",
210+
]);
211+
});
212+
177213
test("parseLanguage", async (t) => {
178214
// Exact matches
179215
t.deepEqual(parseLanguage("csharp"), KnownLanguage.csharp);

src/start-proxy.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from "@actions/core";
22

33
import { getApiClient } from "./api-client";
4+
import * as artifactScanner from "./artifact-scanner";
45
import * as defaults from "./defaults.json";
56
import { KnownLanguage } from "./languages";
67
import { Logger } from "./logging";
@@ -62,6 +63,13 @@ export function parseLanguage(language: string): KnownLanguage | undefined {
6263
return undefined;
6364
}
6465

66+
function isPAT(value: string) {
67+
return artifactScanner.isAuthToken(value, [
68+
artifactScanner.GITHUB_PAT_CLASSIC_PATTERN,
69+
artifactScanner.GITHUB_PAT_FINE_GRAINED_PATTERN,
70+
]);
71+
}
72+
6573
const LANGUAGE_TO_REGISTRY_TYPE: Partial<Record<KnownLanguage, string[]>> = {
6674
java: ["maven_repository"],
6775
csharp: ["nuget_feed"],
@@ -161,6 +169,19 @@ export function getCredentials(
161169
);
162170
}
163171

172+
// If the password or token looks like a GitHub PAT, warn if no username is configured.
173+
if (
174+
!isDefined(e.username) &&
175+
((isDefined(e.password) && isPAT(e.password)) ||
176+
(isDefined(e.token) && isPAT(e.token)))
177+
) {
178+
logger.warning(
179+
`A ${e.type} private registry is configured for ${e.host || e.url} using a GitHub Personal Access Token (PAT), but no username was provided. ` +
180+
`This may not work correctly. When configuring a private registry using a PAT, select "Username and password" and enter the username of the user ` +
181+
`who generated the PAT.`,
182+
);
183+
}
184+
164185
out.push({
165186
type: e.type,
166187
host: e.host,

0 commit comments

Comments
 (0)