bugfix: sanitize gh_token & avoid panic - #1027
Conversation
|
|
||
| Ok(ImpostorCommit { client }) | ||
| state | ||
| .github_client() |
There was a problem hiding this comment.
Flagging: maybe not worth fixing with this PR, but it seems non-ideal that we now have to re-check the error state on GitHubClient on each audit initialization. Maybe we should create a single Option<GitHubClient> on the audit state instead.
(Feel free to look into that if you feel like it; otherwise that can wait until another refactor 🙂)
There was a problem hiding this comment.
I tried to play around with lifetimes to avoid cloning the Client and keep a reference in the Audit implementations, but I've not been using much Rust in the past year and it was starting to get like a bigger rabbit hole than I can dig through at the moment 😄
There was a problem hiding this comment.
No worries at all, thanks for poking at it!
Move the client as a property on `AuditState` to check token validity (if given) early on. This triggers the check even if audit rules making use of GH API are not used. Additionally, instead of creating a new `Client` for audit rules needing it, we create it once and clone it.
woodruffw
left a comment
There was a problem hiding this comment.
LGTM -- I wish we didn't have to clone the GitHub client in so many places, but it's a pretty small cost given that we reuse each audit many times.
|
Thanks @acidghost! |
Fixes #1021.
✨ Copilot summary
This pull request refactors the handling of GitHub API tokens and clients across the codebase, replacing the previous token-based approach with a new `GitHubToken` struct and updating related logic to improve encapsulation and error handling. Additionally, it modifies the `AuditState` structure to directly store an optional GitHub client instead of a token, simplifying client initialization and usage.Refactoring GitHub API Token Handling:
GitHubTokenstruct incrates/zizmor/src/github_api.rsto encapsulate and validate GitHub tokens. Added methods for sanitizing token input and converting tokens to header values.Client::newmethod to accept aGitHubTokeninstance instead of a raw string, improving error handling during header creation. (F889a5b4L89R128)Changes to
AuditStateStructure:gh_tokenandcache_dirfields inAuditStatewith an optionalgh_clientfield, directly storing a pre-configured GitHub client. Updated theAuditState::newmethod to initialize the client based on the presence of a token. [1] [2]Updates to Audit Implementations:
ImpostorCommit,KnownVulnerableActions,RefConfusion,StaleActionRefs) to use the newgh_clientfield directly, simplifying error handling and client initialization logic. [1] [2] [3] [4]Test Updates:
gh_tokenand the addition ofgh_client. Added new tests forGitHubTokenvalidation and sanitization. [1] [2] [3] [4] [5]Other Changes:
crates/zizmor/src/main.rsto useGitHubToken::from_clapfor validating tokens.