Fix ghs_ token patterns to support new token format#3931
Closed
hpsin wants to merge 3 commits into
Closed
Conversation
Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the artifact token scanner to recognize the new ghs_ token format (allowing . and _ and variable length) and adjusts scanning logic to avoid double-counting overlapping matches.
Changes:
- Updates
ghs_token regexes to allow the new character set and non-fixed length. - Changes file scanning from
String.match()tomatchAll()with match-index tracking to deduplicate overlaps. - Updates unit tests to include a new-format
ghs_example and align expectations with the new matching behavior.
Show a summary per file
| File | Description |
|---|---|
| src/artifact-scanner.ts | Updates ghs_ regexes and changes scanning to index-based matchAll() with deduplication. |
| src/artifact-scanner.test.ts | Updates tests to cover new-format ghs_ strings and adjusts expected token type behavior. |
| lib/entry-points.js | Generated build output corresponding to the TypeScript changes (not reviewed). |
Copilot's findings
Comments suppressed due to low confidence (1)
src/artifact-scanner.ts:129
scanFileForTokensdeduplicates matches across all token patterns solely bymatch.index. With the current patterns this changes token-type attribution based on pattern order (the first pattern wins) and can hide overlaps in a non-obvious way. If deduplication is only intended for specific overlapping patterns (e.g.,ghs_), consider scoping it to those patterns or selecting the “best” match/type explicitly.
const seenMatches = new Set<number>();
try {
const content = fs.readFileSync(filePath, "utf8");
for (const { type, pattern } of GITHUB_TOKEN_PATTERNS) {
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;
}
seenMatches.add(index);
findings.push({ tokenType: type, filePath: relativePath });
matchCount++;
}
- Files reviewed: 2/3 changed files
- Comments generated: 3
Comment on lines
56
to
67
| { | ||
| type: TokenType.ServerToServer, | ||
| pattern: /\bghs_[a-zA-Z0-9]{36}\b/g, | ||
| pattern: /ghs_[A-Za-z0-9._]{36,}/g, | ||
| }, | ||
| { | ||
| type: TokenType.Refresh, | ||
| pattern: /\bghr_[a-zA-Z0-9]{36}\b/g, | ||
| }, | ||
| { | ||
| type: TokenType.AppInstallationAccess, | ||
| pattern: /\bghs_[a-zA-Z0-9]{255}\b/g, | ||
| pattern: /ghs_[A-Za-z0-9._]{36,}/g, | ||
| }, |
Member
Author
There was a problem hiding this comment.
I'll leave this to the codeQL team to make a call on this. s2s and installation access token are the same thing, so they're expected to be the same regex.
Comment on lines
57
to
67
| type: TokenType.ServerToServer, | ||
| pattern: /\bghs_[a-zA-Z0-9]{36}\b/g, | ||
| pattern: /ghs_[A-Za-z0-9._]{36,}/g, | ||
| }, | ||
| { | ||
| type: TokenType.Refresh, | ||
| pattern: /\bghr_[a-zA-Z0-9]{36}\b/g, | ||
| }, | ||
| { | ||
| type: TokenType.AppInstallationAccess, | ||
| pattern: /\bghs_[a-zA-Z0-9]{255}\b/g, | ||
| pattern: /ghs_[A-Za-z0-9._]{36,}/g, | ||
| }, |
Comment on lines
70
to
98
| @@ -76,16 +89,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)}`, | |||
| }, | |||
| ]; | |||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Removed