Skip to content

Commit 0fcbec3

Browse files
committed
Add isAuthToken function, with tests
1 parent 0ae8b05 commit 0fcbec3

File tree

6 files changed

+108
-41
lines changed

6 files changed

+108
-41
lines changed

lib/analyze-action-post.js

Lines changed: 10 additions & 8 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: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-action-post.js

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

lib/upload-sarif-action-post.js

Lines changed: 10 additions & 8 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: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import * as path from "path";
44

55
import test from "ava";
66

7-
import { scanArtifactsForTokens, TokenType } from "./artifact-scanner";
7+
import {
8+
GITHUB_PAT_CLASSIC_PATTERN,
9+
isAuthToken,
10+
scanArtifactsForTokens,
11+
TokenType,
12+
} from "./artifact-scanner";
813
import { getRunnerLogger } from "./logging";
914
import {
1015
checkExpectedLogMessages,
@@ -23,6 +28,36 @@ test("makeTestToken", (t) => {
2328
t.is(makeTestToken(255).length, 255);
2429
});
2530

31+
test("isAuthToken", (t) => {
32+
// Undefined for strings that aren't tokens
33+
t.is(isAuthToken("some string"), undefined);
34+
t.is(isAuthToken("ghp_"), undefined);
35+
t.is(isAuthToken("ghp_123"), undefined);
36+
37+
// Token types for strings that are tokens.
38+
t.is(isAuthToken(`ghp_${makeTestToken()}`), TokenType.PersonalAccessClassic);
39+
t.is(
40+
isAuthToken(`ghs_${makeTestToken(255)}`),
41+
TokenType.AppInstallationAccess,
42+
);
43+
t.is(
44+
isAuthToken(`github_pat_${makeTestToken(22)}_${makeTestToken(59)}`),
45+
TokenType.PersonalAccessFineGrained,
46+
);
47+
48+
// With a custom pattern set
49+
t.is(
50+
isAuthToken(`ghp_${makeTestToken()}`, [GITHUB_PAT_CLASSIC_PATTERN]),
51+
TokenType.PersonalAccessClassic,
52+
);
53+
t.is(
54+
isAuthToken(`github_pat_${makeTestToken(22)}_${makeTestToken(59)}`, [
55+
GITHUB_PAT_CLASSIC_PATTERN,
56+
]),
57+
undefined,
58+
);
59+
});
60+
2661
const testTokens = [
2762
{
2863
type: TokenType.PersonalAccessClassic,

src/artifact-scanner.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,25 @@ export interface TokenPattern {
2626
pattern: RegExp;
2727
}
2828

29+
/** The pattern for PATs (Classic) */
30+
export const GITHUB_PAT_CLASSIC_PATTERN: TokenPattern = {
31+
type: TokenType.PersonalAccessClassic,
32+
pattern: /\bghp_[a-zA-Z0-9]{36}\b/g,
33+
};
34+
35+
/** The pattern for PATs (Fine-grained) */
36+
export const GITHUB_PAT_FINE_GRAINED_PATTERN: TokenPattern = {
37+
type: TokenType.PersonalAccessFineGrained,
38+
pattern: /\bgithub_pat_[a-zA-Z0-9_]+\b/g,
39+
};
40+
2941
/**
3042
* GitHub token patterns to scan for.
3143
* These patterns match various GitHub token formats.
3244
*/
3345
const GITHUB_TOKEN_PATTERNS: TokenPattern[] = [
34-
{
35-
type: TokenType.PersonalAccessClassic,
36-
pattern: /\bghp_[a-zA-Z0-9]{36}\b/g,
37-
},
38-
{
39-
type: TokenType.PersonalAccessFineGrained,
40-
pattern: /\bgithub_pat_[a-zA-Z0-9_]+\b/g,
41-
},
46+
GITHUB_PAT_CLASSIC_PATTERN,
47+
GITHUB_PAT_FINE_GRAINED_PATTERN,
4248
{
4349
type: TokenType.OAuth,
4450
pattern: /\bgho_[a-zA-Z0-9]{36}\b/g,
@@ -71,6 +77,24 @@ interface ScanResult {
7177
findings: TokenFinding[];
7278
}
7379

80+
/**
81+
* Checks whether `value` matches any token `patterns`.
82+
* @param value The value to match against.
83+
* @param patterns The patterns to check.
84+
* @returns The type of the first matching pattern, or `undefined` if none match.
85+
*/
86+
export function isAuthToken(
87+
value: string,
88+
patterns: TokenPattern[] = GITHUB_TOKEN_PATTERNS,
89+
) {
90+
for (const { type, pattern } of patterns) {
91+
if (pattern.test(value)) {
92+
return type;
93+
}
94+
}
95+
return undefined;
96+
}
97+
7498
/**
7599
* Scans a file for GitHub tokens.
76100
*

0 commit comments

Comments
 (0)