Skip to content

fix(github): mask a short token without a panic - #9450

Open
henry-rystic wants to merge 1 commit into
apache:mainfrom
henry-rystic:fix-9446-github-short-token
Open

henry-rystic wants to merge 1 commit into
apache:mainfrom
henry-rystic:fix-9446-github-short-token

Conversation

@henry-rystic

Copy link
Copy Markdown

Summary

A GitHub token with a known prefix, but too short, causes a panic.

Cause:

  • SanitizeToken in backend/plugins/github/models/connection.go checked only tokenLen >= prefixLen.
  • Then it read a part of the token that ends at character 32 (ghp_ and the other classic prefixes) or 85 (github_pat_).
  • For a shorter token, this read is out of range, and the code panics.
  • The connection is saved before this code runs. So every later read of the list also panics.

Fix:

  • When a token is shorter than the masked part, SanitizeToken now masks it with utils.SanitizeString. This shows the first 2 and the last 2 characters, for example gh*****rt.
  • A valid token is masked the same way as before.
  • SanitizeSecret had the same kind of bug. A GitHub App private key of 100 characters or less made strings.Repeat panic. It now uses utils.SanitizeString too.
  • After the new length check, the check prefixLen != 0 && hiddenLen != 0 is always true. It is removed.

Does this close any open issues?

Closes #9446

Screenshots

N/A

Other Information

Tests added in backend/plugins/github/models/connection_test.go:

  • New rows in the TestGithubConnection_Sanitize table, with literal expected values: ghp_ becomes g**_, ghp_short becomes gh*****rt, a classic token one character below 32, a classic token of exactly 32 characters, github_pat_ tokens of 30, 84 and 85 characters, and a list with a valid token and a short token.
  • TestGithubConn_SanitizeSecret: a table test for app keys of 5, 99, 100 and 101 characters.

Without the fix, each short case fails with slice bounds out of range or negative Repeat count. A key of exactly 100 characters was returned without a mask, and now it is masked. The rows at the exact limit (32 and 85 characters) and the key of 101 characters check the boundary, so they pass before and after the fix. go test ./plugins/github/... passes on MySQL 8.4 and PostgreSQL 17, and go vet passes.

Manual test. I built the backend image from this branch and ran it with MySQL 8.4. I ran the same steps on apache/devlake:main_261124_0230_52d63a1 to compare.

Step Before the fix After the fix
Send POST /plugins/github/connections with a valid token. 201 201
Send POST /plugins/github/connections with "token":"ghp_short". 500. The row is saved. 201. The token shows as gh*****rt.
Send GET /plugins/github/connections. 500 200. Both connections are listed.
Send GET /plugins/github/connections/<short id>. 500 200
Send POST /plugins/github/connections with a github_pat_ token of 30 characters. 500 201
Send DELETE /plugins/github/connections/<short id>. 500. The row is deleted. 200
Panics in the log 7 0

Not changed. A short token is still accepted on create, because masking is enough to fix the list. GitHub rejects the token when the connection is tested.

This PR was prepared with AI assistance (Claude Code). The commits carry a Co-Authored-By: trailer.

🤖 Generated with Claude Code

A token with a known prefix that is shorter than the masked range made
SanitizeToken slice out of range. The panic broke the create request
after the row was saved, and then every list request. Mask such a token
as a generic string instead. Do the same for a short app secret key.

Closes apache#9446

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Henry Starman <334243260+henry-rystic@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][GitHub] A short token causes a panic, and then the connection list returns 500

1 participant

⌂ Proxy