Repository navigation
PLAT-1314/feat: add opt-in HTTP diagnostics on stderr #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
44cf04b
aeef373
9c387b5
bef76b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // beginRequestDiagnostics returns a completion hook shared by ordinary and | ||
| // paginated operations. It never changes the API result or consumes a body | ||
| // that the request still needs. | ||
| func beginRequestDiagnostics(cmd *cobra.Command, req *http.Request) func(*http.Response, []byte, error) { | ||
| level := flagVerbose | ||
| if level == 0 { | ||
| return func(*http.Response, []byte, error) {} | ||
| } | ||
| started := time.Now() | ||
| return func(resp *http.Response, body []byte, err error) { | ||
| url := diagnosticURL(req) | ||
| record := map[string]any{ | ||
| "method": req.Method, "url": url, | ||
| "duration_ms": float64(time.Since(started)) / float64(time.Millisecond), | ||
| } | ||
| if resp != nil { | ||
| record["status"] = resp.StatusCode | ||
| if id := captureRequestID(resp.Header); id != "" { | ||
| record["request_id"] = id | ||
| } | ||
| } | ||
| if err != nil { | ||
| message := strings.ReplaceAll(err.Error(), req.URL.String(), url) | ||
| record["error"] = secretValueRe.ReplaceAllString(message, "[REDACTED]") | ||
| } | ||
| if level >= 2 { | ||
| addDiagnosticDetails(record, req, resp, body) | ||
| } | ||
| // A failed diagnostic write must not change the API operation's result. | ||
| _ = json.NewEncoder(cmd.ErrOrStderr()).Encode(record) | ||
| } | ||
| } | ||
|
|
||
| func addDiagnosticDetails(record map[string]any, req *http.Request, resp *http.Response, body []byte) { | ||
| record["request_headers"] = diagnosticHeaders(req.Header) | ||
| record["request_body"] = diagnosticRequestBody(req) | ||
| if resp != nil { | ||
| record["response_headers"] = diagnosticHeaders(resp.Header) | ||
| record["response_body"] = diagnosticBody(body) | ||
| } | ||
| } | ||
|
|
||
| func diagnosticURL(req *http.Request) string { | ||
| url := *req.URL | ||
| query := url.Query() | ||
| for key, values := range query { | ||
| if sensitiveHeaderRe.MatchString(key) { | ||
| query.Set(key, "[REDACTED]") | ||
| continue | ||
| } | ||
| for i, value := range values { | ||
| values[i] = secretValueRe.ReplaceAllString(value, "[REDACTED]") | ||
| } | ||
| } | ||
| url.RawQuery = query.Encode() | ||
| return url.String() | ||
| } | ||
|
|
||
| func diagnosticHeaders(headers http.Header) map[string]string { | ||
| out := redactHeaders(headers) | ||
| for key := range out { | ||
| if sensitiveHeaderRe.MatchString(key) { | ||
| out[key] = "[REDACTED]" | ||
| } else { | ||
| out[key] = secretValueRe.ReplaceAllString(out[key], "[REDACTED]") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func diagnosticRequestBody(req *http.Request) any { | ||
| if req.Body == nil || req.Body == http.NoBody { | ||
| return nil | ||
| } | ||
| if req.GetBody == nil { | ||
| return "[body unavailable]" | ||
| } | ||
| copy, err := req.GetBody() | ||
| if err != nil { | ||
| return "[body unavailable]" | ||
| } | ||
| defer copy.Close() | ||
| raw, err := io.ReadAll(io.LimitReader(copy, maxResponseBytes+1)) | ||
| if err != nil { | ||
| return "[body unavailable]" | ||
| } | ||
| return diagnosticBody(raw) | ||
| } | ||
|
|
||
| func diagnosticBody(raw []byte) any { | ||
| if len(raw) == 0 { | ||
| return nil | ||
| } | ||
| if len(raw) > maxResponseBytes { | ||
| return "[body exceeds diagnostic limit]" | ||
| } | ||
| decoder := json.NewDecoder(bytes.NewReader(raw)) | ||
| decoder.UseNumber() | ||
| var value any | ||
| if err := decoder.Decode(&value); err != nil { | ||
| return redactDiagnosticString(string(raw)) | ||
| } | ||
| return redactDiagnosticValue(value) | ||
| } | ||
|
|
||
| func redactDiagnosticValue(value any) any { | ||
| switch value := value.(type) { | ||
| case map[string]any: | ||
| for key, child := range value { | ||
| if sensitiveHeaderRe.MatchString(key) { | ||
| value[key] = "[REDACTED]" | ||
| } else { | ||
| value[key] = redactDiagnosticValue(child) | ||
| } | ||
| } | ||
| case []any: | ||
| for i, child := range value { | ||
| value[i] = redactDiagnosticValue(child) | ||
| } | ||
| case string: | ||
| return redactDiagnosticString(value) | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| // redactDiagnosticString handles URL-valued body fields as well as API keys. | ||
| // Malformed URLs fail closed rather than returning possibly credential-bearing | ||
| // text. Only the diagnostic copy is changed. | ||
| func redactDiagnosticString(value string) string { | ||
| if !strings.Contains(value, "://") && !strings.HasPrefix(value, "/") && !strings.HasPrefix(value, "?") { | ||
| return secretValueRe.ReplaceAllString(value, "[REDACTED]") | ||
| } | ||
| parsed, err := url.Parse(value) | ||
| if err != nil { | ||
| return "[REDACTED]" | ||
| } | ||
| query, err := url.ParseQuery(parsed.RawQuery) | ||
| if err != nil { | ||
| return "[REDACTED]" | ||
| } | ||
| for key, values := range query { | ||
| if sensitiveHeaderRe.MatchString(key) { | ||
| query.Set(key, "[REDACTED]") | ||
| continue | ||
| } | ||
| for i, child := range values { | ||
| values[i] = secretValueRe.ReplaceAllString(child, "[REDACTED]") | ||
| } | ||
| } | ||
| parsed.RawQuery = query.Encode() | ||
| if parsed.User != nil { | ||
| parsed.User = url.User("[REDACTED]") | ||
| } | ||
| if parsed.Fragment != "" { | ||
| parsed.Fragment = "[REDACTED]" | ||
| parsed.RawFragment = "" | ||
| } | ||
| return secretValueRe.ReplaceAllString(parsed.String(), "[REDACTED]") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a redirect target contains a credential-bearing URL and the redirect fails (for example, a signed
LocationURL whose host is unreachable or a redirect loop), Go includes that target URL inclient.Do's error. This code only replaces the original request URL and then redacts Two API-key-shaped strings, so an ordinary value such as?token=abc123is emitted unchanged even with-v. Sanitize URLs embedded in the error or avoid logging the raw transport error.Useful? React with 👍 / 👎.