mirror of
https://github.com/SSLMate/certspotter.git
synced 2025-12-17 20:34:20 +01:00
Truncate the response body that's returned in error messages from logs
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"software.sslmate.com/src/certspotter/cttypes"
|
"software.sslmate.com/src/certspotter/cttypes"
|
||||||
@@ -83,7 +84,7 @@ func get(ctx context.Context, httpClient *http.Client, fullURL string) ([]byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if response.StatusCode != 200 {
|
if response.StatusCode != 200 {
|
||||||
return nil, fmt.Errorf("Get %q: %s (%q)", fullURL, response.Status, bytes.TrimSpace(responseBody))
|
return nil, fmt.Errorf("Get %q: %s (%s)", fullURL, response.Status, formatResponseBody(responseBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
return responseBody, nil
|
return responseBody, nil
|
||||||
@@ -152,7 +153,7 @@ func addChainOrPreChain(ctx context.Context, httpClient *http.Client, logURL *ur
|
|||||||
}
|
}
|
||||||
|
|
||||||
if response.StatusCode != 200 {
|
if response.StatusCode != 200 {
|
||||||
return nil, fmt.Errorf("Post %q: %s (%q)", fullURL, response.Status, bytes.TrimSpace(responseBody))
|
return nil, fmt.Errorf("Post %q: %s (%s)", fullURL, response.Status, formatResponseBody(responseBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
sct := new(cttypes.SignedCertificateTimestamp)
|
sct := new(cttypes.SignedCertificateTimestamp)
|
||||||
@@ -162,3 +163,13 @@ func addChainOrPreChain(ctx context.Context, httpClient *http.Client, logURL *ur
|
|||||||
|
|
||||||
return sct, nil
|
return sct, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatResponseBody(body []byte) string {
|
||||||
|
const maxLen = 200
|
||||||
|
body = bytes.TrimSpace(body)
|
||||||
|
if len(body) > maxLen {
|
||||||
|
return strconv.QuoteToASCII(string(body[:maxLen])) + "..."
|
||||||
|
} else {
|
||||||
|
return strconv.QuoteToASCII(string(body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user