Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/entry-points.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions src/artifact-scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ test("makeTestToken", (t) => {
t.is(makeTestToken(255).length, 255);
});

const NEW_FORMAT_GHS_TOKEN = "ghs_abc123.def456.ghi789_abc123.def456.ghi789";

test("isAuthToken", (t) => {
// Undefined for strings that aren't tokens
t.is(isAuthToken("some string"), undefined);
Expand All @@ -32,10 +34,8 @@ test("isAuthToken", (t) => {
// Token types for strings that are tokens.
t.is(isAuthToken(`ghp_${makeTestToken()}`), TokenType.PersonalAccessClassic);
t.is(isAuthToken(`ghp_${makeTestToken()}`), TokenType.PersonalAccessClassic);
t.is(
isAuthToken(`ghs_${makeTestToken(255)}`),
TokenType.AppInstallationAccess,
);
t.is(isAuthToken(NEW_FORMAT_GHS_TOKEN), TokenType.ServerToServer);
t.is(isAuthToken(`ghs_${makeTestToken(255)}`), TokenType.ServerToServer);
t.is(
isAuthToken(`github_pat_${makeTestToken(22)}_${makeTestToken(59)}`),
TokenType.PersonalAccessFineGrained,
Expand All @@ -52,6 +52,15 @@ test("isAuthToken", (t) => {
]),
undefined,
);
t.is(
isAuthToken(NEW_FORMAT_GHS_TOKEN, [
{
type: TokenType.ServerToServer,
pattern: /ghs_[A-Za-z0-9._-]{36,}/g,
},
]),
TokenType.ServerToServer,
);
});

const testTokens = [
Expand All @@ -76,16 +85,12 @@ const testTokens = [
},
{
type: TokenType.ServerToServer,
value: `ghs_${makeTestToken()}`,
value: NEW_FORMAT_GHS_TOKEN,
},
{
type: TokenType.Refresh,
value: `ghr_${makeTestToken()}`,
},
{
type: TokenType.AppInstallationAccess,
value: `ghs_${makeTestToken(255)}`,
},
];

for (const { type, value, checkPattern } of testTokens) {
Expand Down
28 changes: 17 additions & 11 deletions src/artifact-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export enum TokenType {
UserToServer = "User-to-Server Token",
ServerToServer = "Server-to-Server Token",
Refresh = "Refresh Token",
AppInstallationAccess = "App Installation Access Token",
}

/** A value of this type associates a token type with its pattern. */
Expand Down Expand Up @@ -55,16 +54,12 @@ const GITHUB_TOKEN_PATTERNS: TokenPattern[] = [
},
{
type: TokenType.ServerToServer,
pattern: /\bghs_[a-zA-Z0-9]{36}\b/g,
pattern: /ghs_[A-Za-z0-9._-]{36,}/g,
Comment thread
hagould marked this conversation as resolved.
},
{
type: TokenType.Refresh,
pattern: /\bghr_[a-zA-Z0-9]{36}\b/g,
},
{
type: TokenType.AppInstallationAccess,
pattern: /\bghs_[a-zA-Z0-9]{255}\b/g,
},
];

interface TokenFinding {
Expand Down Expand Up @@ -109,16 +104,27 @@ function scanFileForTokens(
logger: Logger,
): TokenFinding[] {
const findings: TokenFinding[] = [];
const seenMatches = new Set<number>();
Comment thread
hagould marked this conversation as resolved.
Comment thread
hagould marked this conversation as resolved.
try {
const content = fs.readFileSync(filePath, "utf8");

for (const { type, pattern } of GITHUB_TOKEN_PATTERNS) {
const matches = content.match(pattern);
if (matches) {
for (let i = 0; i < matches.length; i++) {
findings.push({ tokenType: type, filePath: relativePath });
const regex = new RegExp(pattern.source, pattern.flags);
let matchCount = 0;

for (const match of content.matchAll(regex)) {
const index = match.index;
if (index === undefined || seenMatches.has(index)) {
continue;
}
logger.debug(`Found ${matches.length} ${type}(s) in ${relativePath}`);

seenMatches.add(index);
findings.push({ tokenType: type, filePath: relativePath });
matchCount++;
Comment thread
hagould marked this conversation as resolved.
}
Comment thread
hagould marked this conversation as resolved.

if (matchCount > 0) {
logger.debug(`Found ${matchCount} ${type}(s) in ${relativePath}`);
}
}

Expand Down
Loading