Skip to content

Commit 49cdf74

Browse files
committed
Use enum for token types
1 parent aac4202 commit 49cdf74

File tree

5 files changed

+70
-51
lines changed

5 files changed

+70
-51
lines changed

lib/analyze-action-post.js

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

src/artifact-scanner.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,56 @@ import * as exec from "@actions/exec";
77
import { Logger } from "./logging";
88
import { getErrorMessage } from "./util";
99

10+
/**
11+
* Enumerates known types of GitHub token formats.
12+
*/
13+
export enum TokenType {
14+
PersonalAccessClassic = "Personal Access Token (Classic)",
15+
PersonalAccessFineGrained = "Personal Access Token (Fine-grained)",
16+
OAuth = "OAuth Access Token",
17+
UserToServer = "User-to-Server Token",
18+
ServerToServer = "Server-to-Server Token",
19+
Refresh = "Refresh Token",
20+
AppInstallationAccess = "App Installation Access Token",
21+
}
22+
23+
/** A value of this type associates a token type with its pattern. */
24+
export interface TokenPattern {
25+
type: TokenType;
26+
pattern: RegExp;
27+
}
28+
1029
/**
1130
* GitHub token patterns to scan for.
1231
* These patterns match various GitHub token formats.
1332
*/
14-
const GITHUB_TOKEN_PATTERNS = [
33+
const GITHUB_TOKEN_PATTERNS: TokenPattern[] = [
1534
{
16-
name: "Personal Access Token (Classic)",
35+
type: TokenType.PersonalAccessClassic,
1736
pattern: /\bghp_[a-zA-Z0-9]{36}\b/g,
1837
},
1938
{
20-
name: "Personal Access Token (Fine-grained)",
39+
type: TokenType.PersonalAccessFineGrained,
2140
pattern: /\bgithub_pat_[a-zA-Z0-9_]+\b/g,
2241
},
2342
{
24-
name: "OAuth Access Token",
43+
type: TokenType.OAuth,
2544
pattern: /\bgho_[a-zA-Z0-9]{36}\b/g,
2645
},
2746
{
28-
name: "User-to-Server Token",
47+
type: TokenType.UserToServer,
2948
pattern: /\bghu_[a-zA-Z0-9]{36}\b/g,
3049
},
3150
{
32-
name: "Server-to-Server Token",
51+
type: TokenType.ServerToServer,
3352
pattern: /\bghs_[a-zA-Z0-9]{36}\b/g,
3453
},
3554
{
36-
name: "Refresh Token",
55+
type: TokenType.Refresh,
3756
pattern: /\bghr_[a-zA-Z0-9]{36}\b/g,
3857
},
3958
{
40-
name: "App Installation Access Token",
59+
type: TokenType.AppInstallationAccess,
4160
pattern: /\bghs_[a-zA-Z0-9]{255}\b/g,
4261
},
4362
];
@@ -69,13 +88,13 @@ function scanFileForTokens(
6988
try {
7089
const content = fs.readFileSync(filePath, "utf8");
7190

72-
for (const { name, pattern } of GITHUB_TOKEN_PATTERNS) {
91+
for (const { type, pattern } of GITHUB_TOKEN_PATTERNS) {
7392
const matches = content.match(pattern);
7493
if (matches) {
7594
for (let i = 0; i < matches.length; i++) {
76-
findings.push({ tokenType: name, filePath: relativePath });
95+
findings.push({ tokenType: type, filePath: relativePath });
7796
}
78-
logger.debug(`Found ${matches.length} ${name}(s) in ${relativePath}`);
97+
logger.debug(`Found ${matches.length} ${type}(s) in ${relativePath}`);
7998
}
8099
}
81100

0 commit comments

Comments
 (0)