Subscribe

Failing the golang build when someone logs a password or token with zap

Code review rarely catches a debug line that logs a token. Here is a small golang test that walks the source tree, splits every zap field key into words and fails the build on password, token or PIN, plus helpers to mask emails and cap logged bodies.

Failing the golang build when someone logs a password or token with zap

Logs are where secrets go to live forever. Somebody adds zap.String("token", tok) while debugging a login problem, the fix ships, the debug line ships with it, and now your log group holds live tokens for as long as your retention says. Nobody notices because nobody reads INFO lines.

Code review is supposed to catch this. In my experience it doesn't, at least not reliably. A reviewer reads the logic and skims the log lines. So in this blog we will put the rule into a plain golang test that walks the source tree and fails the build when a zap field key looks like a password, token or card PIN. We'll also add two small helpers for the cases where you do need to log something close to personal data.

The examples use zap v1.28.0 and Go 1.27.3.

Checking the key instead of the value

Checking values is hard. A token looks like any other random string. But the key is right there in the source as a string literal, and a key called token almost always holds a token. So the test looks for things like this:

log.Info("login", zap.String("password", req.Password))
log.Debug("refresh", zap.String("refreshToken", tok))

A regex finds the first string argument of any zap.Something( call:

// fieldKey finds the literal key of a zap field: zap.String("password", ...).
var fieldKey = regexp.MustCompile(`zap\.[A-Za-z0-9]+\(\s*"([^"]+)"`)

That covers zap.String, zap.Any, zap.Strings, zap.Int and the rest. It is not a parser and it doesn't pretend to be one. I'll come back to what it misses.

The denylist

The list of words live next to the helpers, in the logging package, so people see it when they open the package:

var DeniedWords = []string{
    "authorization",
    "cookie",
    "cvv",
    "apikey",
    "pan",
    "passwd",
    "password",
    "pwd",
    "pin",
    "secret",
    "token",
}

The simplest version compares the whole key against the list. Then refresh_token isn't token, and you end up listing every combination anyone could type. So I split the key into words first and check each word:

// camel splits "refreshToken" into "refresh Token", acronym splits
// "APIToken" into "API Token".
var (
    camel   = regexp.MustCompile(`([a-z0-9])([A-Z])`)
    acronym = regexp.MustCompile(`([A-Z]+)([A-Z][a-z])`)
)

// keyWords turns "user.refreshToken" or "card_PIN" into lower case words.
func keyWords(key string) []string {
    key = acronym.ReplaceAllString(key, "$1 $2")
    key = camel.ReplaceAllString(key, "$1 $2")
    return strings.FieldsFunc(strings.ToLower(key), func(r rune) bool {
        return r == '_' || r == '.' || r == '-' || r == ' '
    })
}

// deniedWord reports the first denied word in key. It checks each word and
// the whole key glued together, so "api_key" is caught as "apikey".
func deniedWord(key string) (string, bool) {
    words := keyWords(key)
    for _, w := range append(words, strings.Join(words, "")) {
        if slices.Contains(DeniedWords, w) {
            return w, true
        }
    }
    return "", false
}

Two things went wrong on the way and both were caught by the table test below. My first camel case split only handled lower-then-upper, so APIToken came out as one word, apitoken, and passed. The acronym regex fixes that. Then api_key was two harmless words, api and key, so I added the glued version of the whole key as one more candidate.

Splitting into words also matters the other way round. pin and pan are short. A plain substring check would flag spinner, shipping and company, and a test that cries wolf gets deleted within a week.

func TestKeyWords(t *testing.T) {
    for key, bad := range map[string]bool{
        "password":       true,
        "refreshToken":   true,
        "user.api_token": true,
        "card_PIN":       true,
        "APIToken":       true,
        "userPwd":        true,
        "api_key":        true,
        "apiKey":         true,
        "spinner":        false, // "pin" inside a word is fine
        "company":        false, // so is "pan"
        "token_count":    true,  // false alarm, rename it or mark logkey:ok
        "email":          false,
    } {
        if _, got := deniedWord(key); got != bad {
            t.Errorf("deniedWord(%q) = %v, want %v", key, got, bad)
        }
    }
}

Walking the tree

The test itself finds the module root (the folder with go.mod) and walks every .go file that isn't a test:

func scan(root string, skipTestdata bool) ([]finding, error) {
    var out []finding
    err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
        if err != nil {
            return err
        }
        if d.IsDir() {
            switch d.Name() {
            case ".git", "vendor", "node_modules":
                return filepath.SkipDir
            case "testdata":
                if skipTestdata {
                    return filepath.SkipDir
                }
            }
            return nil
        }
        if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
            return nil
        }
        b, err := os.ReadFile(path)
        if err != nil {
            return err
        }
        for i, line := range strings.Split(string(b), "\n") {
            if strings.Contains(line, "logkey:ok") {
                continue
            }
            for _, m := range fieldKey.FindAllStringSubmatch(line, -1) {
                if _, bad := deniedWord(m[1]); bad {
                    out = append(out, finding{path, i + 1, m[1]})
                }
            }
        }
        return nil
    })
    return out, err
}

func TestNoSensitiveLogKeys(t *testing.T) {
    found, err := scan(moduleRoot(t), true)
    if err != nil {
        t.Fatal(err)
    }
    for _, f := range found {
        t.Errorf("%s: sensitive word in a log key. Drop the field or use logx.Redact.", f)
    }
}

The logkey:ok comment is the escape hatch. token_count for LLM usage is a real example: it counts LLM tokens and has nothing to do with credentials. The comment has to sit on the same line, so the reason is right there in the diff for the reviewer to argue with.

To see it fail, I added a password field to the example login handler and ran go test ./...:

--- FAIL: TestNoSensitiveLogKeys (0.00s)
    keys_test.go:110: .../handlers/login.go:37 key "password": sensitive word in a log key. Drop the field or use logx.Redact.
FAIL
FAIL    example.com/app/logx    0.163s

File, line, key and what to do instead. That's what I want a new joiner to see, right in the failing build.

Test the scanner too

A broken regex would let everything through forever, the test would stay green. So there is a file under logx/testdata/bad/ with lines that must be reported, plus two that must not:

func handler() {
    log.Info("login", zap.String("password", req.Password))
    log.Debug("refresh", zap.String("refreshToken", tok))
    log.Info("card", zap.String("card_pin", pin), zap.String("user_id", id))
    log.Warn("cvv check", zap.Any("cvv", code))
    log.Info("headers", zap.Strings("Authorization", h))
    log.Info("spinner", zap.Int("spinner_count", 3))
    log.Info("budget", zap.Int("token_count", n)) // logkey:ok, LLM token usage, not a credential
}

Go ignores testdata directories when building, so this file never has to compile. The main test skips it, and TestScannerCatchesBadKeys scans only it and expects exactly those five keys in that order.

When you do need the value: mask it

Some fields are not secrets but are still personal: an email, a phone number, an account ID. Dropping them completely makes "which customer was this?" impossible to answer from logs, and that is usually why they were logged in full to begin with. So mask them and keep enough to match a support ticket:

func Mask(v string) string {
    v = strings.TrimSpace(v)
    if v == "" {
        return ""
    }
    if at := strings.LastIndex(v, "@"); at > 0 {
        local := v[:at]
        if len(local) <= 2 {
            return "**" + v[at:]
        }
        return local[:1] + strings.Repeat("*", len(local)-2) + local[len(local)-1:] + v[at:]
    }
    if len(v) <= 2 {
        return "**"
    }
    return strings.Repeat("*", len(v)-2) + v[len(v)-2:]
}

func Redact(key, value string) zap.Field {
    return zap.String(key, Mask(value))
}

aravind@example.com becomes a*****d@example.com, a phone number keeps its last two digits. When I cleaned up our logging I found error lines for failed email sends that had the full recipient address in them. They now go through Redact, and support can still match them.

Cap the bodies

The other leak is a whole request or response body logged on error. Third party APIs send back whatever they like, sometimes the user's data. We log bodies only at ERROR and cut them at 1000 characters, the same limit our mobile app uses in its own logging so one incident doesn't have two different cut-off points:

const MaxBody = 1000

func CapBody(body string) string {
    if len(body) <= MaxBody {
        return body
    }
    return body[:MaxBody] + "...[truncated " + strconv.Itoa(len(body)-MaxBody) + " chars]"
}

The suffix tells you whether a response was short or trimmed. The error code at the start of a vendor response survives, which is usually all you need.

What this test does not catch

I'd rather be honest about the gaps, because a green test makes people relax. It misses keys held in constants (zap.String(keyPassword, v)), the sugared logger's Infow("msg", "password", v) pairs, a whole struct passed to zap.Any, and any secret formatted into the message string with fmt.Sprintf. A go/analysis pass could cover more of that. For us the regex caught the common mistake, was cheap to write and runs with the normal go test ./..., so it runs in CI next to the other checks like govulncheck.

There we have it. One test file, one small list, and the next zap.String("token", ...) fails the pull request instead of sitting in CloudWatch for a year.