Skip to content
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

fix: ensure Fastly-Key is stripped from the dump #453

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion fastly/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fastly
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
Expand Down Expand Up @@ -293,6 +294,24 @@ func (c *Client) DeleteJSONAPIBulk(p string, i interface{}, ro *RequestOptions)
return c.RequestJSONAPIBulk("DELETE", p, i, ro)
}

// stripKey removes the Fastly-Key value from the request dump.
func stripKey(dump []byte) ([]byte, error) {
index := bytes.Index(dump, []byte("Fastly-Key: "))
if index == -1 {
return dump, errors.New("a Fastly-Key was not found")
}

tokenStart := index + len("Fastly-Key: ")
tokenEnd := bytes.IndexByte(dump[tokenStart:], '\r')
if tokenEnd == -1 {
return dump, errors.New("no end of token was found")
}

redactedToken := strings.Repeat("X", len(dump[tokenStart:tokenStart+tokenEnd]))
copy(dump[tokenStart:tokenStart+tokenEnd], []byte(redactedToken))
return dump, nil
}

// Request makes an HTTP request against the HTTPClient using the given verb,
// Path, and request options.
func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, error) {
Expand All @@ -308,7 +327,9 @@ func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, er

if c.debugMode {
dump, _ := httputil.DumpRequest(req, true)
fmt.Printf("http.Request (dump): %q\n", dump)
if stripped, err := stripKey(dump); err == nil {
fmt.Printf("http.Request (dump): %q\n", stripped)
}
}

// nosemgrep: trailofbits.go.invalid-usage-of-modified-variable.invalid-usage-of-modified-variable
Expand Down