Prevent Logging Secrets in Go by Using Custom Types
Tags: go, security, beginners, tutorial
Published: 2022-08-17 02:31:00

In our codebase for [Granted Approvals](https://docs.commonfate.io/granted-approvals/introduction/), we’ve got a structure that looks like this:

```go
type Provider struct {
	OrgUrl string
	APIToken string
}
```

In this case, we’ve got a value named `apiToken`. It’s a secret, and we’d rather it doesn’t get leaked somewhere. Your secret value could also be an API token, or maybe it’s a password or a user address or anything else that you don’t want to get a Privacy Act lawsuit for.

The trouble with storing your secrets in strings like this is that it’s very easy to accidentally log them. It only takes one `fmt` or `zap` line for your secret value to be printed in plaintext and then possibly even sent to your external logging solutions. Using unexported fields can help, but Zap will still include them anyway.

If you’re reading this, then you probably already know why having sensitive data in your logs is dangerous. If you don’t, you can check out [this](https://www.theregister.com/2022/05/27/github_publishes_a_post_mortem/). Or [this](https://www.bleepingcomputer.com/news/security/twitter-admits-recording-plaintext-passwords-in-internal-logs-just-like-github/). Or [this](https://techcrunch.com/2022/07/26/justalk-spilled-millions-of-user-messages-and-locations-for-months/). Or…. you get the point.

And if you’re rolling your eyes and saying *I’d never log sensitive data*, I’d ask: are you sure? You’re never going to accidentally type the wrong variable name in your print function? You’re never going to have a new dev join your team? Nobody on your team is ever going to be one night away from a deadline debugging why the hell the password reset function isn’t working and just go *screw it, I don’t get paid enough to care about security anyway*? I’m not so sure.

**Our solution**: custom types.

```go
type Provider struct {
	OrgUrl StringValue
	APIToken SecretStringValue
}
```

Now instead of `orgUrl` and `apiToken` being type *string*, they’re *StringValue* and *SecretStringValue.* Both of these custom types provide a small abstraction over a standard string type:

```go

type StringValue struct {
	Value string
}

type SecretStringValue struct {
	Value string
}
```

The part that’s important to us though is how they both implement the [Stringer](https://go.dev/tour/methods/17) and MarshalJSON interfaces:

```go
func (s StringValue) String() string {
	return s.Value
}

func (s StringValue) MarshalJSON() ([]byte, error) {
	return json.Marshal(s.String())
}

func (s SecretStringValue) String() string {
	return "*****"
}

func (s SecretStringValue) MarshalJSON() ([]byte, error) {
	return json.Marshal(s.String())
}
```

The String() implementation for `SecretStringValue` will only return the redacted string “*****”, and same goes for the JSON. This makes it a whole lot more difficult to accidentally log the secret by just plugging it into fmt or zap.

So now, if you’re got something like:

```go
oops := Provider{OrgURL:"commonfate.io",APIToken:"secret"}
fmt.Printf("%s", oops.APIToken)
```

or:

```go
oops := Provider{OrgURL: "commonfate.io", APIToken: "secret"}
b, _ := json.Marshal(oops)
fmt.Print(string(b))
```

All you should hopefully get in return is `*****` or `{"OrgUrl":"commonfate.io","APIToken":"*****"}`.

Is it totally foolproof? No. You can still log the secret if someone decides to deliberately print the Value, but at least now the team has clearer context on whether or not something is intended to be a Secret or just a regular Value. It makes it just that little bit harder to make a mistake — and in security, that’s all we can really ask.

If you want to check out the code for yourself, here’s a [Go Playground link](https://go.dev/play/p/Ha3ade158vi). And if you’re interested in seeing how we’ve implemented this in our own project, you can check out our [gconfig package](https://github.com/common-fate/granted-approvals/tree/main/pkg/gconfig).