From 3e96197da37782b3b2f32d82a47d759ef4ba9257 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 11:11:28 +0800 Subject: [PATCH] =?UTF-8?q?redact:=20an=20address's=20words=20joined=20by?= =?UTF-8?q?=20hyphens=20are=20no=20sk-=20key=20(sk-hynix-=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- internal/redact/redact.go | 20 +++++++++++++++++++- internal/redact/slug_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 internal/redact/slug_test.go diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 4ebc5ca23..31b6ef334 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -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") { diff --git a/internal/redact/slug_test.go b/internal/redact/slug_test.go new file mode 100644 index 000000000..1c495a5aa --- /dev/null +++ b/internal/redact/slug_test.go @@ -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) + } + } +}