Skip to content

Replace godebug with go-cmp#598

Open
jfantinhardesty wants to merge 3 commits into
AzureAD:mainfrom
jfantinhardesty:remove_godebug
Open

Replace godebug with go-cmp#598
jfantinhardesty wants to merge 3 commits into
AzureAD:mainfrom
jfantinhardesty:remove_godebug

Conversation

@jfantinhardesty
Copy link
Copy Markdown

@jfantinhardesty jfantinhardesty commented Feb 28, 2026

This PR refactors the codebase to replace the use of the github.com/kylelemons/godebug package with github.com/google/go-cmp package. Godebug is no longer maintained and has not been updated in 6 years and go-cmp is actively maintained. This mainly just replaces pretty.Compare with cmp.Diff. And the errors.Verbose() function now uses the httputil library to dump the requests and responses.

Comment thread apps/errors/errors.go
e.Resp.Request = nil // This brings in a bunch of TLS crap we don't need
e.Resp.TLS = nil // Same
return fmt.Sprintf("%s:\nRequest:\n%s\nResponse:\n%s", e.Err, prettyConf.Sprint(e.Req), prettyConf.Sprint(e.Resp))
return fmt.Sprintf("%s:\nRequest:\n%s\nResponse:\n%s", e.Err, dumpRequest(e.Req), dumpResponse(e.Resp))
Copy link
Copy Markdown
Member

@bgavrilMS bgavrilMS Mar 2, 2026

Choose a reason for hiding this comment

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

This part needs some cleanup logic. Something like:

For dumpRequest:

  • Log the URL host and path only (no query string values)
  • Log HTTP method
  • Log header names only (not values — Authorization header leaks tokens)
  • Log GET/POST parameter names only (not values — body leaks secrets/assertions)

For dumpResponse:

  • On error responses (non-2xx): log field names + values (safe, it's just an error description)
  • On success responses: log field names only, never values

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I made the output more similar to how it worked before with godebug/pretty. I do believe the prior version with godebug/pretty could leak header tokens and secrets since there was no processing of those values, but that may have been intentional since it is a verbose log. I made it a bit more structured so it should be easier to decide what to print. Maybe you could help with that as that seems like there are a few edge cases and more like a separate PR, since the current code can leak these tokens.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed, let's keep it simple. Print minimal information only. From what I can see, app owner can choose to print full HTTP details on their side anyway.

"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/accesstokens"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/shared"
"github.com/kylelemons/godebug/pretty"
"github.com/google/go-cmp/cmp"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@chlowell - would appreciate your review on this PR.

@bgavrilMS bgavrilMS requested a review from chlowell March 2, 2026 11:55
Comment thread apps/errors/errors.go Outdated
fmt.Fprintf(&b, " URL: {Scheme: %q,\n", req.URL.Scheme)
fmt.Fprintf(&b, " Host: %q,\n", req.URL.Host)
fmt.Fprintf(&b, " Path: %q,\n", req.URL.Path)
fmt.Fprintf(&b, " RawQuery: %q},\n", req.URL.RawQuery)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The rawQuery should not be printed. It may contain login hint, i.e. user emails.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed rawQuery.

Comment thread apps/errors/errors.go Outdated
fmt.Fprintf(&b, " Path: %q,\n", req.URL.Path)
fmt.Fprintf(&b, " RawQuery: %q},\n", req.URL.RawQuery)
fmt.Fprintf(&b, " Proto: %q,\n", req.Proto)
fmt.Fprintf(&b, " ProtoMajor: %d,\n", req.ProtoMajor)
Copy link
Copy Markdown
Member

@bgavrilMS bgavrilMS Mar 3, 2026

Choose a reason for hiding this comment

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

nit: Isn't Proto enough?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes! godebug/pretty would print it, so I added that to the prints. But now it will just print Proto.

Comment thread apps/errors/errors.go Outdated
return b.String()
}

func formatHeaders(header http.Header, indent string) string {
Copy link
Copy Markdown
Member

@bgavrilMS bgavrilMS Mar 3, 2026

Choose a reason for hiding this comment

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

We should not print header values here. It's ok to not print headers at all. We can decide to print specific headers in other layers of the library.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed printing headers.

Comment thread apps/errors/errors.go Outdated
fmt.Fprintf(&b, " Proto: %q,\n", resp.Proto)
fmt.Fprintf(&b, " ProtoMajor: %d,\n", resp.ProtoMajor)
fmt.Fprintf(&b, " ProtoMinor: %d,\n", resp.ProtoMinor)
fmt.Fprintf(&b, " Header: %s,\n", formatHeaders(resp.Header, " "))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As above, let's not print headers.

Comment thread apps/errors/errors.go Outdated
fmt.Fprintf(&b, " ProtoMajor: %d,\n", resp.ProtoMajor)
fmt.Fprintf(&b, " ProtoMinor: %d,\n", resp.ProtoMinor)
fmt.Fprintf(&b, " Header: %s,\n", formatHeaders(resp.Header, " "))
if bodyStr == "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

body can contain tokens, better not print it at all.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed printing body.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

@chlowell chlowell left a comment

Choose a reason for hiding this comment

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

Looks good overall, the blocker for me is the nil URL guard. Version upgrade policy is up to @bgavrilMS

Comment thread go.mod
module github.com/AzureAD/microsoft-authentication-library-for-go

go 1.18
go 1.21
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.

I doubt this will cause problems because Google stopped supporting 1.20 a couple years ago and upgrading to 1.21 should be easy, however I think it's important to note this is raising the library's minimum required version to enable a test change having no functional impact on the tests or customer applications. Telemetry, if there is any, would help evaluate this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't have telemetry on Go.

Comment thread apps/errors/errors.go
Comment on lines +75 to +77
fmt.Fprintf(&b, " URL: {Scheme: %q,\n", req.URL.Scheme)
fmt.Fprintf(&b, " Host: %q,\n", req.URL.Host)
fmt.Fprintf(&b, " Path: %q},\n", req.URL.Path)
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.

We should guard against req.URL == nil, if only to satisfy static analysis and LLMs

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