Skip to content

refactor(oauth): fix double body close, harden security, and clean up code#17

Merged
appleboy merged 2 commits intomainfrom
refactor/security-and-cleanup
Mar 24, 2026
Merged

refactor(oauth): fix double body close, harden security, and clean up code#17
appleboy merged 2 commits intomainfrom
refactor/security-and-cleanup

Conversation

@appleboy
Copy link
Copy Markdown
Member

Summary

  • Bug fix: Fixed double resp.Body.Close() in makeAPICallWithAutoRefresh — the deferred close at the top of the function fired on an already-closed body when entering the 401 retry branch. Moved the single defer after the branch so only one close occurs per response.
  • Security: State parameter CSRF validation now uses crypto/subtle.ConstantTimeCompare instead of != to prevent timing attacks. Replaced panic() with fmt.Fprintf(os.Stderr, ...) + os.Exit(1) for retry client init failure, consistent with all other config error handling.
  • Code quality: Extracted isRefreshTokenError helper to deduplicate inline OAuth error parsing in refreshAccessToken. Removed redundant tokenStoreMode global (now local). Eliminated confusing err/err2 naming. Replaced custom containsSubstring/findSubstring test helpers with strings.Contains.

Test plan

  • make test — all 24 tests pass
  • make lint — 0 issues
  • go build ./... — compiles cleanly

🤖 Generated with Claude Code

… code

- Fix double resp.Body.Close() in makeAPICallWithAutoRefresh by moving
  defer after the 401 retry branch
- Use crypto/subtle.ConstantTimeCompare for CSRF state validation
- Replace panic with os.Exit(1) for retry client init failure
- Extract isRefreshTokenError helper to deduplicate error parsing
- Remove redundant tokenStoreMode global variable
- Eliminate err2 naming by reusing err after consumption
- Replace custom containsSubstring with strings.Contains in tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 24, 2026 13:17
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 24, 2026

Codecov Report

❌ Patch coverage is 12.50000% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
main.go 0.00% 14 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors and hardens the OAuth CLI flow by tightening error handling, reducing duplicated logic, and improving security in the callback CSRF validation.

Changes:

  • Fixes response body lifecycle in the 401 → refresh → retry API call flow to avoid double-closing.
  • Uses constant-time state comparison for CSRF validation in the callback handler.
  • Cleans up config/init and token refresh code (remove redundant global, dedupe refresh-token error detection, simplify tests).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
main.go Refactors config initialization/error handling, deduplicates refresh-token error parsing, and fixes response body closing in auto-refresh API call flow.
callback.go Hardens CSRF state validation by switching to constant-time comparison.
main_test.go Replaces custom substring helpers with strings.Contains and removes the now-unneeded helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ison

- Fail fast when state parameter length differs from expected,
  avoiding unnecessary memory allocation from maliciously large
  query values before the constant-time comparison

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@appleboy appleboy merged commit ac4fc17 into main Mar 24, 2026
24 checks passed
@appleboy appleboy deleted the refactor/security-and-cleanup branch March 24, 2026 13:31
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.

3 participants