Skip to content

Commit 615a713

Browse files
Hirsch SinghalCopilot
andcommitted
Fix ghs_ token patterns to support new token format
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0e150e4 commit 615a713

3 files changed

Lines changed: 48 additions & 20 deletions

File tree

‎lib/entry-points.js‎

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

‎src/artifact-scanner.test.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ test("makeTestToken", (t) => {
2323
t.is(makeTestToken(255).length, 255);
2424
});
2525

26+
const NEW_FORMAT_GHS_TOKEN =
27+
"ghs_abc123.def456.ghi789_abc123.def456.ghi789";
28+
2629
test("isAuthToken", (t) => {
2730
// Undefined for strings that aren't tokens
2831
t.is(isAuthToken("some string"), undefined);
@@ -32,9 +35,10 @@ test("isAuthToken", (t) => {
3235
// Token types for strings that are tokens.
3336
t.is(isAuthToken(`ghp_${makeTestToken()}`), TokenType.PersonalAccessClassic);
3437
t.is(isAuthToken(`ghp_${makeTestToken()}`), TokenType.PersonalAccessClassic);
38+
t.is(isAuthToken(NEW_FORMAT_GHS_TOKEN), TokenType.ServerToServer);
3539
t.is(
3640
isAuthToken(`ghs_${makeTestToken(255)}`),
37-
TokenType.AppInstallationAccess,
41+
TokenType.ServerToServer,
3842
);
3943
t.is(
4044
isAuthToken(`github_pat_${makeTestToken(22)}_${makeTestToken(59)}`),
@@ -52,6 +56,15 @@ test("isAuthToken", (t) => {
5256
]),
5357
undefined,
5458
);
59+
t.is(
60+
isAuthToken(NEW_FORMAT_GHS_TOKEN, [
61+
{
62+
type: TokenType.AppInstallationAccess,
63+
pattern: /ghs_[A-Za-z0-9._]{36,}/g,
64+
},
65+
]),
66+
TokenType.AppInstallationAccess,
67+
);
5568
});
5669

5770
const testTokens = [
@@ -76,16 +89,12 @@ const testTokens = [
7689
},
7790
{
7891
type: TokenType.ServerToServer,
79-
value: `ghs_${makeTestToken()}`,
92+
value: NEW_FORMAT_GHS_TOKEN,
8093
},
8194
{
8295
type: TokenType.Refresh,
8396
value: `ghr_${makeTestToken()}`,
8497
},
85-
{
86-
type: TokenType.AppInstallationAccess,
87-
value: `ghs_${makeTestToken(255)}`,
88-
},
8998
];
9099

91100
for (const { type, value, checkPattern } of testTokens) {

‎src/artifact-scanner.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ const GITHUB_TOKEN_PATTERNS: TokenPattern[] = [
5555
},
5656
{
5757
type: TokenType.ServerToServer,
58-
pattern: /\bghs_[a-zA-Z0-9]{36}\b/g,
58+
pattern: /ghs_[A-Za-z0-9._]{36,}/g,
5959
},
6060
{
6161
type: TokenType.Refresh,
6262
pattern: /\bghr_[a-zA-Z0-9]{36}\b/g,
6363
},
6464
{
6565
type: TokenType.AppInstallationAccess,
66-
pattern: /\bghs_[a-zA-Z0-9]{255}\b/g,
66+
pattern: /ghs_[A-Za-z0-9._]{36,}/g,
6767
},
6868
];
6969

@@ -109,16 +109,27 @@ function scanFileForTokens(
109109
logger: Logger,
110110
): TokenFinding[] {
111111
const findings: TokenFinding[] = [];
112+
const seenMatches = new Set<number>();
112113
try {
113114
const content = fs.readFileSync(filePath, "utf8");
114115

115116
for (const { type, pattern } of GITHUB_TOKEN_PATTERNS) {
116-
const matches = content.match(pattern);
117-
if (matches) {
118-
for (let i = 0; i < matches.length; i++) {
119-
findings.push({ tokenType: type, filePath: relativePath });
117+
const regex = new RegExp(pattern.source, pattern.flags);
118+
let matchCount = 0;
119+
120+
for (const match of content.matchAll(regex)) {
121+
const index = match.index;
122+
if (index === undefined || seenMatches.has(index)) {
123+
continue;
120124
}
121-
logger.debug(`Found ${matches.length} ${type}(s) in ${relativePath}`);
125+
126+
seenMatches.add(index);
127+
findings.push({ tokenType: type, filePath: relativePath });
128+
matchCount++;
129+
}
130+
131+
if (matchCount > 0) {
132+
logger.debug(`Found ${matchCount} ${type}(s) in ${relativePath}`);
122133
}
123134
}
124135

0 commit comments

Comments
 (0)