redact: an address's words joined by hyphens are no sk- key (sk-hynix-…)

innerpeacebuild on X: search results naming articles about SK hynix,
cnbc.com/…/sk-hynix-third-quarter-profit.html and the like, went to the
model with each slug as {{API_KEY_…}}. The sk- rule took any 20
characters of letters, digits, - and _.

A key's characters are random, so it now needs a capital or a run of 16
or more with no hyphen. DeepSeek's sk- and 32 hex digits and
OpenRouter's sk-or-v1- and 64 are lower case and still masked.

Run on mote at this tree: go test ./internal/redact ok;
TestSlugIsNoKey (the reporter's four addresses beside four keys) is red
on the old rule (each slug masked) and green on the new. go vet for
darwin, linux and windows ok, gofmt clean. Not run: the full suite;
only internal/redact changed.
This commit is contained in:
yetone
2026-10-28 11:11:45 +08:00
parent c3b0484a96
commit 3e96197da3
2 changed files with 55 additions and 1 deletions
+19 -1
View File
@@ -141,7 +141,7 @@ const (
var rules = append([]rule{
{kind: "PRIVATE_KEY", re: regexp.MustCompile(`-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----[\s\S]+?-----END (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----`), markers: []string{"PRIVATE KEY"}},
{kind: "API_KEY", re: regexp.MustCompile(`sk-(?:ant-|proj-|or-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}`), markers: []string{"sk-"}, bound: tokenCh},
{kind: "API_KEY", re: regexp.MustCompile(`sk-(?:ant-|proj-|or-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}`), markers: []string{"sk-"}, bound: tokenCh, ok: notASlug},
// a gateway's or a relay's own: oc_sk_…, or_sk_… (#195)
{kind: "API_KEY", re: regexp.MustCompile(`[a-z]{2,4}_sk_[A-Za-z0-9_-]{20,}`), markers: []string{"_sk_"}, bound: tokenCh, ok: secretValue},
{kind: "API_KEY", re: regexp.MustCompile(`(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,}|glpat-[A-Za-z0-9_-]{20,})`), markers: []string{"gh", "github_pat_", "glpat-"}, bound: tokenCh},
@@ -399,6 +399,24 @@ func inUserinfo(s string, a int) bool {
// the user every git host is reached by over SSH (git@github.com:o/r.git,
// git@gitee.com, a company's own git@git.corp.com), no one's mailbox, and
// was masked so a remote no longer read as one (jumy on Discord).
// notASlug is false for words in lower case joined by hyphens, as a
// page's address names an article about SK hynix
// (cnbc.com/…/sk-hynix-third-quarter-profit.html), which went to the model
// as a key. A key's characters are random: it has a capital, or a run of
// 16 or more with no hyphen (DeepSeek's sk- and 32 hex digits, OpenRouter's
// sk-or-v1- and 64).
func notASlug(v string) bool {
if strings.ContainsFunc(v, func(r rune) bool { return r >= 'A' && r <= 'Z' }) {
return true
}
for _, w := range strings.FieldsFunc(v, func(r rune) bool { return r == '-' || r == '_' }) {
if len(w) >= 16 {
return true
}
}
return false
}
func realEmail(v string) bool {
at := strings.LastIndexByte(v, '@')
if strings.EqualFold(v[:at], "git") {
+36
View File
@@ -0,0 +1,36 @@
package redact
import (
"strings"
"testing"
)
// Addresses of articles about SK hynix were masked as sk- keys
// (innerpeacebuild on X, the reporter's search results). A key beside
// them still is, and so are keys in lower case: DeepSeek's and
// OpenRouter's.
func TestSlugIsNoKey(t *testing.T) {
o := Options{Secrets: true}
for _, s := range []string{
"https://www.cnbc.com/2026/10/28/sk-hynix-third-quarter-profit.html",
"https://finance.yahoo.com/markets/stocks/articles/sk-hynix-announces-3q26-financial-234500011.html",
"https://www.techpowerup.com/354203/sk-hynix-announces-3q26-financial-results",
"https://au.investing.com/news/stock-market-news/sk-hynix-q3-2026-slides-revenue-tops-100t-80-margin-on-ai-boom",
} {
if out, n := Mask(s, o); n != 0 || out != s {
t.Errorf("%q masked: %q", s, out)
}
}
for _, key := range []string{
"sk-proj-abcdEFGH1234ijklMNOP5678qrst",
"sk-" + strings.Repeat("0123456789abcdef", 2),
"sk-or-v1-" + strings.Repeat("0123456789abcdef", 4), // built, as a literal one stops GitHub's push
"sk-ant-api03-Ab3dE-fGh1_jKl2mN-oPq3rSt4",
} {
in := "url sk-hynix-third-quarter-profit key " + key
out, n := Mask(in, o)
if n != 1 || strings.Contains(unmarked(out), key) || !strings.Contains(out, "sk-hynix-third-quarter-profit") {
t.Errorf("%q: %d masked: %q", key, n, out)
}
}
}