Skip to content

Commit f2323bb

Browse files
author
Katrina Owen
authored
Merge pull request #866 from Jrank2013/masking_api_token
The API token outputted during verbose will now be masked by default
2 parents 9d1041e + c7bab26 commit f2323bb

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Download exercises and submit your solutions.`,
2424
if verbose, _ := cmd.Flags().GetBool("verbose"); verbose {
2525
debug.Verbose = verbose
2626
}
27+
if unmask, _ := cmd.Flags().GetBool("unmask-token"); unmask {
28+
debug.UnmaskAPIKey = unmask
29+
}
2730
if timeout, _ := cmd.Flags().GetInt("timeout"); timeout > 0 {
2831
cli.TimeoutInSeconds = timeout
2932
api.TimeoutInSeconds = timeout
@@ -46,4 +49,5 @@ func init() {
4649
api.UserAgent = fmt.Sprintf("github.com/exercism/cli v%s (%s/%s)", Version, runtime.GOOS, runtime.GOARCH)
4750
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
4851
RootCmd.PersistentFlags().IntP("timeout", "", 0, "override the default HTTP timeout (seconds)")
52+
RootCmd.PersistentFlags().BoolP("unmask-token", "", false, "will unmask the API during a request/response dump")
4953
}

cmd/troubleshoot.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"fmt"
66
"html/template"
77
"runtime"
8-
"strings"
98
"sync"
109
"time"
1110

1211
"github.com/exercism/cli/cli"
1312
"github.com/exercism/cli/config"
13+
"github.com/exercism/cli/debug"
1414
"github.com/spf13/cobra"
1515
"github.com/spf13/viper"
1616
)
@@ -192,7 +192,7 @@ func newConfigurationStatus(status *Status) configurationStatus {
192192
TokenURL: config.SettingsURL(v.GetString("apibaseurl")),
193193
}
194194
if status.Censor && cs.Token != "" {
195-
cs.Token = redact(cs.Token)
195+
cs.Token = debug.Redact(cs.Token)
196196
}
197197
return cs
198198
}
@@ -212,12 +212,6 @@ func (ping *apiPing) Call(wg *sync.WaitGroup) {
212212
ping.Status = "connected"
213213
}
214214

215-
func redact(token string) string {
216-
str := token[4 : len(token)-3]
217-
redaction := strings.Repeat("*", len(str))
218-
return string(token[:4]) + redaction + string(token[len(token)-3:])
219-
}
220-
221215
const tmplSelfTest = `
222216
Troubleshooting Information
223217
===========================

cmd/troubleshoot_test.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

debug/debug.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import (
99
"net/http"
1010
"net/http/httputil"
1111
"os"
12+
"strings"
1213
)
1314

1415
var (
1516
// Verbose determines if debugging output is displayed to the user
1617
Verbose bool
1718
output io.Writer = os.Stderr
19+
// UnmaskAPIKey determines if the API key should de displayed during a dump
20+
UnmaskAPIKey bool
1821
)
1922

2023
// Println conditionally outputs a message to Stderr
@@ -41,6 +44,14 @@ func DumpRequest(req *http.Request) {
4144
body := io.TeeReader(req.Body, &bodyCopy)
4245
req.Body = ioutil.NopCloser(body)
4346

47+
temp := req.Header.Get("Authorization")
48+
49+
if !UnmaskAPIKey {
50+
if token := strings.Split(temp, " ")[1]; token != "" {
51+
req.Header.Set("Authorization", "Bearer "+Redact(token))
52+
}
53+
}
54+
4455
dump, err := httputil.DumpRequest(req, req.ContentLength > 0)
4556
if err != nil {
4657
log.Fatal(err)
@@ -51,6 +62,7 @@ func DumpRequest(req *http.Request) {
5162
Println("========================= END DumpRequest =========================")
5263
Println("")
5364

65+
req.Header.Set("Authorization", temp)
5466
req.Body = ioutil.NopCloser(&bodyCopy)
5567
}
5668

@@ -76,3 +88,10 @@ func DumpResponse(res *http.Response) {
7688

7789
res.Body = ioutil.NopCloser(body)
7890
}
91+
92+
// Redact masks the given token by replacing part of the string with *
93+
func Redact(token string) string {
94+
str := token[4 : len(token)-3]
95+
redaction := strings.Repeat("*", len(str))
96+
return string(token[:4]) + redaction + string(token[len(token)-3:])
97+
}

debug/debug_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package debug
33
import (
44
"bytes"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestVerboseEnabled(t *testing.T) {
@@ -26,3 +28,10 @@ func TestVerboseDisabled(t *testing.T) {
2628
t.Error("expected '' got", b.String())
2729
}
2830
}
31+
32+
func TestRedact(t *testing.T) {
33+
fakeToken := "1a11111aaaa111aa1a11111a11111aa1"
34+
expected := "1a11*************************aa1"
35+
36+
assert.Equal(t, expected, Redact(fakeToken))
37+
}

0 commit comments

Comments
 (0)