Skip to content

fix(auth): drop raw IdP error bodies from CLI errors - #27

Merged
emsearcy merged 2 commits into
mainfrom
bug/IdP-token-endpoint-error
Nov 23, 2026
Merged

emsearcy merged 2 commits into
mainfrom
bug/IdP-token-endpoint-error

Conversation

@dealako

@dealako dealako commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Summary

When the device-code, polling, or refresh request gets a non-2xx answer with no error code, oauth2.RetrieveError.Error() builds its message as oauth2: cannot fetch token: <Status>\nResponse: <raw body>, with up to 1 MiB of body. lfx wrapped that error with %w and printed it to stderr. A hostile IdP, or a TLS interceptor with an OS-trusted certificate for the Auth0 domain, could forge stderr lines on any command that refreshes a token (CWE-150).

PR #25 already escapes control characters in main.go, but it keeps newlines, and its description left this ticket open for that gap.

Fixes linuxfoundation/lfx-self-serve-ops#1719 (Claude Security finding find_01Uy8E9MynboQV3pejq3FVrd).

Changes

  • New idpError helper in internal/commands/auth.go, used by the three wraps: request device code, unexpected error waiting for login, and refresh access token.
    • No error code: the message is <prefix>: identity provider returned HTTP <code>. The body is dropped.
    • With an error code: the code and description follow the status, each cut to 200 bytes on a rune boundary and escaped. Invalid UTF-8 is quoted too, since a form-encoded response can decode %9B to a raw 8-bit CSI byte.
    • Uses Response.StatusCode, not Response.Status, because the reason phrase is server-controlled.
    • Any other error still wraps with %w.
  • The special cases (expired_token, access_denied, deadline, invalid_grant) and the main.go escaping are unchanged.

Status-only output for uncoded responses is a deliberate choice for this fix. Maintainers can add richer IdP error messages later if they want them.

Testing

  • make check: 0 issues. make test: pass. gitleaks: clean.
  • New tests in internal/commands/auth_test.go:
    • The ticket's case: a body with \x1b[2J and \n and no error code gives a message with neither byte.
    • Control characters in the code and description are escaped.
    • A long description is cut to 200 bytes without splitting a multi-byte rune.
    • Invalid UTF-8 (0x9b) in the description is quoted.
    • Non-RetrieveError errors still wrap.
  • The tests cover the helper. The three call sites were checked by reading the code, since the Auth0 domain is compiled in.

🤖 Generated with Claude Code

When a device-code, polling, or refresh request gets a non-2xx answer
with no `error` code, oauth2.RetrieveError.Error() embeds the raw
response body after a newline. The auth commands wrapped that error
with %w, so a hostile IdP or a TLS interceptor could forge stderr lines.
main.go already escapes control characters (PR #25) but keeps newlines.

Replace the three wraps with idpError, which rebuilds the message from
the HTTP status code and, when present, the error code and description,
each cut to 200 bytes and escaped. Invalid UTF-8 is quoted as well, since
a form-encoded response can decode to a raw 0x9b (8-bit CSI). Other
errors still wrap with %w.

Refs: linuxfoundation/lfx-self-serve-ops#1719

Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: David Deal <ddeal@linuxfoundation.org>
@dealako
dealako requested review from a team and emsearcy as code owners November 20, 2026 23:40
@dealako dealako added the ai-assisted A task or activity that was supported by AI, such as CoPilot, ChatGPT, or other AI technology. label Nov 20, 2026
Copilot AI balanced review requested due to automatic review settings November 20, 2026 23:40
@lfx-one

lfx-one commented Nov 20, 2026 •

Copy link
Copy Markdown

@dealako I'm starting review 1 of this pull request now (started 2026-11-20 23:40 UTC using review agent v0.1.34). This usually takes a few minutes. I'll update this comment with the summary when I'm done. If there's no summary here after about 45 minutes, the review stopped: push a new commit, or post a comment that begins with a mention of me followed by re-review, to start another. For help with the review agent, see https://github.com/linuxfoundation/lfx-review-agent/issues.


Thanks for the clear write-up and the thorough tests, David.

Summary

This pull request replaces the three raw %w wraps of oauth2 errors with an idpError helper. The helper keeps only the HTTP status code and, when present, a truncated and escaped error code and description. The change is small and focused, and I found no issues.

Decision

⚠️ Review passed, human approval required

This pull request passed automated review. This repository is set to review only, so I never approve pull requests here. A maintainer needs to review and approve it before merge.

Details

Overall impression: idpError drops the response body and the server-controlled reason phrase, and it reads Response.StatusCode directly. idpErrorText cuts on a rune boundary and quotes invalid UTF-8, so a raw 0x9b byte can't reach the terminal. The expired_token, access_denied, invalid_grant and deadline special cases still run before the helper, so they are unchanged. The tests cover the uncoded-body case, escaping, truncation, invalid UTF-8 and non-RetrieveError wrapping. RetrieveError.Response is dereferenced without a nil check. The oauth2 library always sets it for errors it builds, so I did not raise it as a finding.

Documentation reminder: changed identity provider error messages for login and token refresh (HTTP status only, escaped code and description). Please check product pages and external docs; the author and maintainers own those updates.

Rule files: read AGENTS.md; skipped none

Questions, problems, or feedback: https://github.com/linuxfoundation/lfx-review-agent/issues

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the authentication-error sanitization change.

No open issues 🎉

What changed in this PR

This PR hardens LFX CLI authentication errors by dropping raw identity-provider response bodies, closing the newline-forging gap left by the earlier terminal-output escaping change.

Changes:

  • Routes device-code, polling, and refresh failures through a shared error helper.
  • Reports numeric HTTP status codes and bounded, escaped OAuth error fields while preserving wrapping for other errors.
  • Adds tests for body removal, control-character escaping, UTF-8-safe truncation, invalid UTF-8, and error wrapping.
File Description
internal/​commands/​auth.go Adds sanitized identity-provider error formatting at all three authentication error sites.
internal/​commands/​auth_test.go Adds regression tests for safe error output and preserved wrapping behavior.

Review effort: Balanced

@lfx-one lfx-one left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This pull request passed automated review. This repository is set to review only, so I never approve pull requests here. A maintainer needs to review and approve it before merge.

Documentation

This pull request changes behavior that may also be described outside this repository, such as product pages or an external docs site. I can't read those, so please check them; the author and maintainers own those updates. Changed here:

  • identity provider error messages for login and token refresh (HTTP status only, escaped code and description) (behavior changed)

audigregorie
audigregorie previously approved these changes Nov 23, 2026

@audigregorie audigregorie left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

PR Review — #27: fix(auth): drop raw IdP error bodies from CLI errors

Branch: bug/IdP-token-endpoint-error → main
Reviewed commits: 0ba1f7eba73f0c66411d26ebea6ee363bb258705..8b92d00cf891ab84fbd8db4b71307995af2cd40a
Commits: 1 | Files changed: 2 | Scope: Go CLI authentication error handling
Issue counts: Critical: 0 | Major: 0 | Minor: 0
Issue threads from this run: 0 | Unposted findings: 0

No defensible defects retained after the applicable source-review passes.

What's Done Well ✅

  • All three OAuth error exits use the sanitizer, while expiry, denial, and revoked-session messages remain unchanged.
  • Raw response bodies and server-controlled HTTP reason phrases are excluded. Displayed error fields are bounded and escape both control characters and invalid UTF-8.
  • Other errors retain %w wrapping; the API and Goa command callers propagate the sanitized refresh error without relying on its former concrete type.

Review coverage and limitations

  • Both changed files were read in full at the pinned head; removed wrappers were inspected in the merge-base diff.
  • Traced device login, token refresh, auth token, runAPI, resolveAuthenticatedBaseURL, goaService.run, and the final stderr sanitizer. Compared existing terminal-safety and error-propagation patterns.
  • Checked the linked issue's fix criteria: https://github.com/linuxfoundation/lfx-self-serve-ops/issues/1719. Source supports the described status-only uncoded errors, bounded/escaped coded errors, numeric status use, and unchanged special cases.
  • Inspected official golang.org/x/oauth2 v0.38.0 source (deviceauth.go, internal/token.go, token.go, and relevant oauth2.go token-source methods), confirming RetrieveError response construction and propagation rather than relying on the issue's older dependency assumptions.
  • Completed consistency, truthfulness, available-rules, correctness, edge-case, security, integration/data, and regression passes, including missing codes, malformed error bodies, control bytes, truncation boundaries, and ordinary transport errors.
  • CLAUDE.md is absent from the pinned checkout. The fixed Angular/NestJS/cross-app checklists are not applicable to this Go CLI change.
  • Tests were not executed or assessed for strength. The PR's reported test/lint results were not independently verified. Lint, type checks, and builds were not run; this is source inspection, not runtime verification.
  • No additional follow-up work identified within this review's scope.

Rating: ⭐⭐⭐⭐⭐

Review outcome: APPROVE — no retained defects

Zero findings do not imply exhaustive runtime verification.

@emsearcy emsearcy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Styling (from revive, per Go standard style):

internal/commands/auth.go:779:7: const maxIdPErrorTextBytes should be maxIDPErrorTextBytes

MegaLinter's revive var-naming rule failed on maxIdPErrorTextBytes.
Rename it to maxIDPErrorTextBytes, and rename the test for a body
without an error code so cspell has no new unknown word.

Refs: linuxfoundation/lfx-self-serve-ops#1719

Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: David Deal <ddeal@linuxfoundation.org>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:38
@lfx-one

lfx-one commented Nov 23, 2026 •

Copy link
Copy Markdown

@dealako I'm starting review 2 of this pull request now (started 2026-11-23 17:38 UTC using review agent v0.1.34). This usually takes a few minutes. I'll update this comment with the summary when I'm done. If there's no summary here after about 45 minutes, the review stopped: push a new commit, or post a comment that begins with a mention of me followed by re-review, to start another. For help with the review agent, see https://github.com/linuxfoundation/lfx-review-agent/issues.


Thanks for the quick turnaround on the lint finding, David.

Summary

The new commit renames the error-text cap constant to maxIDPErrorTextBytes to satisfy revive and renames the matching test. It changes no behavior, and I found no issues.

Decision

⚠️ Review passed, human approval required

This pull request passed automated review. This repository is set to review only, so I never approve pull requests here. A maintainer needs to review and approve it before merge.

Details

Overall impression: The commit f095d61 is a rename only. It renames the constant maxIDPErrorTextBytes (the revive var-naming finding that emsearcy raised) and its uses, including in the test, and renames one test. idpError and idpErrorText are the same as in review 1. The rename leaves the 200-byte cap, the rune-boundary truncation and the invalid-UTF-8 quoting as they were. Review 1 had no findings, so there is nothing to track. The required checks DCO and License Header Check / License Header Check passed on f095d61. The change request from emsearcy is a human review that only that reviewer can dismiss or clear.

👏 Nice work: You fixed the lint finding within minutes of it being reported, and you renamed the test as well so cspell has no new unknown word.

Rule files: read AGENTS.md; skipped none

Questions, problems, or feedback: https://github.com/linuxfoundation/lfx-review-agent/issues

@lfx-one lfx-one left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This pull request passed automated review. This repository is set to review only, so I never approve pull requests here. A maintainer needs to review and approve it before merge.

The new commit only renames the error-text cap to maxIDPErrorTextBytes (the revive finding) and the matching test; I found no new issues.

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

The PR replaces raw identity-provider error messages at the device-code, polling, and refresh call sites.

  • Authentication errors show status without raw response bodies.

No actionable issues found. dealako explicitly chose status-only output for responses without an error code. dealako also acknowledged helper-only tests and manual call-site review. Tests were reviewed, not run.

Reviews (1) · Last reviewed commit: "style(auth): use revive's IDP initialism..." · Reviewed by Greptile

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the authentication-error sanitization change.

No open issues 🎉

Review effort: Balanced

@dealako

dealako commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@emsearcy Done. Commit f095d61 renames the constant to maxIDPErrorTextBytes, which clears revive's var-naming finding (your review). It also renames one test so cspell has no new unknown word. There is no behavior change, and MegaLinter passes on the new head.

@dealako

dealako commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Review Feedback Addressed

Commit: f095d61

Changes Made

  • internal/commands/auth.go, internal/commands/auth_test.go: renamed maxIdPErrorTextBytes to maxIDPErrorTextBytes for revive's var-naming rule (review; per @emsearcy). The test for a body without an error code is renamed too, so cspell has no new unknown word.

No Change Needed

  • Docs reminder (review; from the lfx-one review agent): no Markdown in this repo and nothing in the generated docs/cli.md on gh-pages quotes the identity provider error messages, so there is nothing to update. I don't know of any external docs that describe them.

Feedback Items Addressed

2 of 2 actionable feedback items addressed, both from general review bodies.

Threads Resolved

0 of 0. The PR has no inline review threads.

MegaLinter and every other check pass on f095d61.

@dealako
dealako dismissed emsearcy’s stale review November 23, 2026 18:09

Review feedback has been addressed in commit f095d61. Re-requesting your review.

@dealako
dealako requested a review from emsearcy November 23, 2026 18:09
@emsearcy
emsearcy merged commit 7f1aac8 into main Nov 23, 2026
10 checks passed
@emsearcy
emsearcy deleted the bug/IdP-token-endpoint-error branch November 23, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted A task or activity that was supported by AI, such as CoPilot, ChatGPT, or other AI technology.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants