Skip to content

Commit 85df3c0

Browse files
hagouldCopilot
andcommitted
Address review: bound regex, revert scanFileForTokens, restore old-format test
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent eee6a34 commit 85df3c0

3 files changed

Lines changed: 25 additions & 42 deletions

File tree

lib/entry-points.js

Lines changed: 6 additions & 14 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: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ test("isAuthToken", (t) => {
5252
]),
5353
undefined,
5454
);
55-
t.is(
56-
isAuthToken(NEW_FORMAT_GHS_TOKEN, [
57-
{
58-
type: TokenType.ServerToServer,
59-
pattern: /ghs_[A-Za-z0-9._-]{36,}/g,
60-
},
61-
]),
62-
TokenType.ServerToServer,
63-
);
6455
});
6556

6657
const testTokens = [
@@ -83,18 +74,29 @@ const testTokens = [
8374
type: TokenType.UserToServer,
8475
value: `ghu_${makeTestToken()}`,
8576
},
77+
{
78+
type: TokenType.ServerToServer,
79+
value: `ghs_${makeTestToken()}`,
80+
checkPattern: "Server-to-Server",
81+
label: "legacy format",
82+
},
8683
{
8784
type: TokenType.ServerToServer,
8885
value: NEW_FORMAT_GHS_TOKEN,
86+
checkPattern: "Server-to-Server",
87+
label: "new format",
8988
},
9089
{
9190
type: TokenType.Refresh,
9291
value: `ghr_${makeTestToken()}`,
9392
},
9493
];
9594

96-
for (const { type, value, checkPattern } of testTokens) {
97-
test(`scanArtifactsForTokens detects GitHub ${type} tokens in files`, async (t) => {
95+
for (const { type, value, checkPattern, label } of testTokens) {
96+
const testName = label
97+
? `scanArtifactsForTokens detects GitHub ${type} (${label}) tokens in files`
98+
: `scanArtifactsForTokens detects GitHub ${type} tokens in files`;
99+
test(testName, async (t) => {
98100
const logMessages = [];
99101
const logger = getRecordingLogger(logMessages, { logToConsole: false });
100102
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "scanner-test-"));

src/artifact-scanner.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const GITHUB_TOKEN_PATTERNS: TokenPattern[] = [
5454
},
5555
{
5656
type: TokenType.ServerToServer,
57-
pattern: /ghs_[A-Za-z0-9._-]{36,}/g,
57+
pattern: /\bghs_[A-Za-z0-9._-]{36,516}(?![A-Za-z0-9._-])/g,
5858
},
5959
{
6060
type: TokenType.Refresh,
@@ -104,27 +104,16 @@ function scanFileForTokens(
104104
logger: Logger,
105105
): TokenFinding[] {
106106
const findings: TokenFinding[] = [];
107-
const seenMatches = new Set<number>();
108107
try {
109108
const content = fs.readFileSync(filePath, "utf8");
110109

111110
for (const { type, pattern } of GITHUB_TOKEN_PATTERNS) {
112-
const regex = new RegExp(pattern.source, pattern.flags);
113-
let matchCount = 0;
114-
115-
for (const match of content.matchAll(regex)) {
116-
const index = match.index;
117-
if (index === undefined || seenMatches.has(index)) {
118-
continue;
111+
const matches = content.match(pattern);
112+
if (matches) {
113+
for (let i = 0; i < matches.length; i++) {
114+
findings.push({ tokenType: type, filePath: relativePath });
119115
}
120-
121-
seenMatches.add(index);
122-
findings.push({ tokenType: type, filePath: relativePath });
123-
matchCount++;
124-
}
125-
126-
if (matchCount > 0) {
127-
logger.debug(`Found ${matchCount} ${type}(s) in ${relativePath}`);
116+
logger.debug(`Found ${matches.length} ${type}(s) in ${relativePath}`);
128117
}
129118
}
130119

0 commit comments

Comments
 (0)