@@ -7,37 +7,56 @@ import * as exec from "@actions/exec";
77import { Logger } from "./logging" ;
88import { 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 : / \b g h p _ [ a - z A - Z 0 - 9 ] { 36 } \b / g,
1837 } ,
1938 {
20- name : "Personal Access Token (Fine-grained)" ,
39+ type : TokenType . PersonalAccessFineGrained ,
2140 pattern : / \b g i t h u b _ p a t _ [ a - z A - Z 0 - 9 _ ] + \b / g,
2241 } ,
2342 {
24- name : " OAuth Access Token" ,
43+ type : TokenType . OAuth ,
2544 pattern : / \b g h o _ [ a - z A - Z 0 - 9 ] { 36 } \b / g,
2645 } ,
2746 {
28- name : "User-to-Server Token" ,
47+ type : TokenType . UserToServer ,
2948 pattern : / \b g h u _ [ a - z A - Z 0 - 9 ] { 36 } \b / g,
3049 } ,
3150 {
32- name : "Server-to-Server Token" ,
51+ type : TokenType . ServerToServer ,
3352 pattern : / \b g h s _ [ a - z A - Z 0 - 9 ] { 36 } \b / g,
3453 } ,
3554 {
36- name : " Refresh Token" ,
55+ type : TokenType . Refresh ,
3756 pattern : / \b g h r _ [ a - z A - Z 0 - 9 ] { 36 } \b / g,
3857 } ,
3958 {
40- name : "App Installation Access Token" ,
59+ type : TokenType . AppInstallationAccess ,
4160 pattern : / \b g h s _ [ a - z A - Z 0 - 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