redact: a tool call's arguments stay JSON when a value follows an escape (#3022)
A history tool call's function.arguments is JSON text inside a string.
MaskJSON masked it as plain text, so the n of a \n started a match:
"\n@mcp.tool()" became "\{{EMAIL_…}}()", the arguments were no longer
JSON, and Ollama Cloud refused every turn that replayed the call with
400 "invalid tool call arguments". Reported by xiaobaifly7 and reproduced
independently by Shane-Meng (\n\n\n@pytest.mark…).
Where a string holds JSON text (asJSON), mask now reads it the way the
vendor reads it once it is unescaped:
- unescapedView: a same-length view in which \n, \r, \t, \b and \f read
as control characters and a \u escape reads as line breaks, so no word
starts or ends on an escape's letter. \" \\ \/ stay as written, so the
SECRET rule's \"-aware quote and Windows paths read as before.
- outsideEscapes: any span (rule, word, model, known) that starts or
ends inside an escape is cut back to the escape's edges, so the JSON
holds and what the span held is still masked. A span left empty is
dropped.
- unescapedPut: the value kept for Restore is the unescaped one, so
Restore (which escapes it again in arguments) gives the agent exactly
what it wrote. Before this, a private key's \n came back as \\n.
Ported from ggbdpq's hunt53-f-redact-json-escape. Their test
TestMaskJSONKeepsArgumentsJSON is kept verbatim. Their guard alone let
"\njo@quill.dev" through unmasked, so this port also adds the view, the
trimming and the restore.
Run at base 673651e8d (origin/main) plus this change:
- gofmt -l: clean
- go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: ok
- go test -tags nogui -race -count=3 ./internal/redact: ok
- go test -tags nogui -count=1 -timeout 45m ./internal/redact/...
./internal/gateway/...: ok (redact 3.7s, gateway 423s; internal/masking has no tests)
- Full suite NOT run: another session's `go test -timeout 45m ./...`
was already running on this machine (one suite per machine).
Broken by hand (each run against the new tests in escape_test.go):
- old code (origin/main's redact.go, json.go and scrub.go, with a no-op
outsideEscapes shim): all 4 tests red. TestMaskJSONKeepsArgumentsJSON
"2 masked, want 1"; Escapes "invalid character '{' in string escape
code" with \{{EMAIL_…}}; Restore gets \\n back; CutShort red.
- unescapedView off: KeepsArgumentsJSON, Escapes and CutShort red.
- unescapedPut off: TestRestoreArgumentsFromArguments red.
- outsideEscapes call off: CutShort red (a user rule
`REL-[a-z\\]+` ending on the backslash of \" breaks the JSON).
- trimming replaced by dropping: CutShort's unit check red.
Fixtures come from the reporters' bytes: xiaobaifly7's \n@mcp.tool() and
Shane-Meng's \n\n\n@pytest.mark…. They also cover an address after \n,
\t, \" or \r\n, after a Windows path, and after é/—; an npm key
after \n; a key-context password; and cut-short arguments. Each checks
that the arguments still parse, the values are gone and the decorators
are unchanged.
Not tried / not covered:
- No real Ollama Cloud round trip, and nothing end to end on Windows.
- A user rule that reads past the string's closing quote (e.g. \S+ over
JSON text) can still break arguments. That was true before this change
and is not an escape case.
- Allow-list matching and a Hit's hint use the escaped text as written.
A value remembered earlier with real newlines is not found by
knownSpans in arguments unless a rule finds it again.
- A personal value written as a JSON number in arguments is not masked
(masking it would break the JSON).
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A tool call's arguments ride in the request as a string that holds JSON
|
||||
// of its own: the newline in the command in it is the two characters \
|
||||
// and n, and what follows reads with the n for the start of a word. The
|
||||
// EMAIL rule took n@mcp.tool for an address, and the placeholder went in
|
||||
// where the n had been, the backslash left before it — \{{EMAIL_…}}, and
|
||||
// the arguments no longer parsed. The vendor turned the whole request away
|
||||
// with 400 "invalid tool call arguments", the history replayed, and the
|
||||
// conversation stayed broken (#3022).
|
||||
func TestMaskJSONKeepsArgumentsJSON(t *testing.T) {
|
||||
command := "print('before')\n@mcp.tool()\ndef demo():\n return 1"
|
||||
inner, _ := json.Marshal(map[string]string{"command": command})
|
||||
args, _ := json.Marshal(string(inner)) // the string that carries the arguments
|
||||
body := `{"messages":[` +
|
||||
`{"role":"user","content":"write mara@quill.dev about the demo"},` +
|
||||
`{"role":"assistant","content":null,"tool_calls":[{"id":"call_1","type":"function",` +
|
||||
`"function":{"name":"pwsh","arguments":` + string(args) + `}}]},` +
|
||||
`{"role":"tool","tool_call_id":"call_1","content":"done"}]}`
|
||||
out, n := MaskJSON([]byte(body), Options{Personal: true})
|
||||
if !json.Valid(out) {
|
||||
t.Fatalf("body no longer JSON: %s", out)
|
||||
}
|
||||
if n != 1 { // the real email in the user's words, not the decorator
|
||||
t.Fatalf("%d masked, want 1: %s", n, out)
|
||||
}
|
||||
var v struct {
|
||||
Messages []struct {
|
||||
ToolCalls []struct {
|
||||
Function struct {
|
||||
Arguments string `json:"arguments"`
|
||||
} `json:"function"`
|
||||
} `json:"tool_calls"`
|
||||
} `json:"messages"`
|
||||
}
|
||||
if err := json.Unmarshal(out, &v); err != nil {
|
||||
t.Fatalf("body: %v", err)
|
||||
}
|
||||
arguments := v.Messages[1].ToolCalls[0].Function.Arguments
|
||||
if !json.Valid([]byte(arguments)) {
|
||||
t.Fatalf("arguments no longer JSON: %s", arguments)
|
||||
}
|
||||
var got struct {
|
||||
Command string
|
||||
}
|
||||
if err := json.Unmarshal([]byte(arguments), &got); err != nil {
|
||||
t.Fatalf("arguments: %v", err)
|
||||
}
|
||||
if got.Command != command {
|
||||
t.Fatalf("command changed: %q", got.Command)
|
||||
}
|
||||
// and the email the same request really has still goes
|
||||
if !strings.Contains(string(out), "{{EMAIL_") || strings.Contains(unmarked(string(out)), "mara@quill.dev") {
|
||||
t.Fatalf("the address went through: %s", out)
|
||||
}
|
||||
|
||||
// a string that is no JSON of its own is read after one unescaping, so
|
||||
// a Windows path in it goes as before
|
||||
body2 := `{"cwd":"C:\\Users\\rkato\\proj"}`
|
||||
out2, n2 := MaskJSON([]byte(body2), Options{Personal: true, Kinds: map[string]bool{"home": true}})
|
||||
if n2 != 1 || !strings.Contains(string(out2), `C:\\Users\\{{USERNAME_`) {
|
||||
t.Fatalf("%d: %s", n2, out2)
|
||||
}
|
||||
}
|
||||
|
||||
// argsOf is the arguments of each tool call in a Chat Completions body.
|
||||
func argsOf(t *testing.T, body []byte) []string {
|
||||
t.Helper()
|
||||
var v struct {
|
||||
Messages []struct {
|
||||
ToolCalls []struct {
|
||||
Function struct {
|
||||
Arguments string `json:"arguments"`
|
||||
} `json:"function"`
|
||||
} `json:"tool_calls"`
|
||||
} `json:"messages"`
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
ToolCalls []struct {
|
||||
Function struct {
|
||||
Arguments string `json:"arguments"`
|
||||
} `json:"function"`
|
||||
} `json:"tool_calls"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &v); err != nil {
|
||||
t.Fatalf("body no longer JSON: %v: %s", err, body)
|
||||
}
|
||||
var out []string
|
||||
for _, m := range v.Messages {
|
||||
for _, c := range m.ToolCalls {
|
||||
out = append(out, c.Function.Arguments)
|
||||
}
|
||||
}
|
||||
for _, c := range v.Choices {
|
||||
for _, tc := range c.Message.ToolCalls {
|
||||
out = append(out, tc.Function.Arguments)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// chatWithCall is a Chat Completions history whose assistant turn called
|
||||
// pwsh with arguments.
|
||||
func chatWithCall(arguments string) []byte {
|
||||
a, _ := json.Marshal(arguments)
|
||||
return []byte(`{"messages":[{"role":"user","content":"Reply OK."},` +
|
||||
`{"role":"assistant","content":null,"tool_calls":[{"id":"call_repro","type":"function",` +
|
||||
`"function":{"name":"pwsh","arguments":` + string(a) + `}}]},` +
|
||||
`{"role":"tool","tool_call_id":"call_repro","content":"Completed."}]}`)
|
||||
}
|
||||
|
||||
// A tool call's arguments in the history hold what a session really has
|
||||
// in them: decorators after newlines (xiaobaifly7's \n@mcp.tool(),
|
||||
// Shane-Meng's \n\n\n@pytest.mark…), and real addresses and keys right
|
||||
// after an escape — a newline, a tab, a quote, a backslash, a \u escape.
|
||||
// Each of the arguments still parses, each real value is masked, and
|
||||
// every decorator reads as it was (#3022).
|
||||
func TestMaskJSONArgumentsEscapes(t *testing.T) {
|
||||
const npmKey = "npm_abcdEFGH1234ijklMNOP5678qrstUVWX9012"
|
||||
commands := []struct {
|
||||
command string
|
||||
gone []string
|
||||
}{
|
||||
{command: "print('before')\n@mcp.tool()\ndef demo():\n return 1"},
|
||||
{command: "looked into a possible new issue\n\n\n@pytest.mark.parametrize('x', [1])\ndef test_x(x):\n pass"},
|
||||
{command: "@app.route('/')\n@login.required\r\n@cache.cached(timeout=60)\ndef index(): pass"},
|
||||
{command: "mail\njo@quill.dev about it", gone: []string{"jo@quill.dev"}},
|
||||
{command: "to:\tmara@quill.dev", gone: []string{"mara@quill.dev"}},
|
||||
{command: `git config user.email "rkato@quill.dev"`, gone: []string{"rkato@quill.dev"}},
|
||||
{command: `type C:\Users\rkato\notes.txt` + "\nzoe@quill.dev", gone: []string{"zoe@quill.dev"}},
|
||||
{command: "caf\u00e9 \u2014 lin@quill.dev", gone: []string{"lin@quill.dev"}},
|
||||
{command: "export NPM_TOKEN=\n" + npmKey, gone: []string{npmKey}},
|
||||
}
|
||||
for _, c := range commands {
|
||||
inner, _ := json.Marshal(map[string]any{"command": c.command, "timeout": 30})
|
||||
// \u escapes too, as Python's json.dumps writes them
|
||||
ascii := strings.NewReplacer("é", `\u00e9`, "—", `\u2014`).Replace(string(inner))
|
||||
for _, args := range []string{string(inner), ascii} {
|
||||
out, _ := MaskJSON(chatWithCall(args), Options{Personal: true, Secrets: true})
|
||||
got := argsOf(t, out)[0]
|
||||
var v struct {
|
||||
Command string
|
||||
Timeout int
|
||||
}
|
||||
if err := json.Unmarshal([]byte(got), &v); err != nil {
|
||||
t.Fatalf("arguments no longer JSON (%v): %s", err, got)
|
||||
}
|
||||
if v.Timeout != 30 {
|
||||
t.Fatalf("timeout changed: %s", got)
|
||||
}
|
||||
if len(c.gone) == 0 && v.Command != c.command {
|
||||
t.Fatalf("command changed:\n%q\n%q", v.Command, c.command)
|
||||
}
|
||||
for _, g := range c.gone {
|
||||
if strings.Contains(v.Command, g) || strings.Contains(string(out), g) {
|
||||
t.Fatalf("%s went through: %s", g, got)
|
||||
}
|
||||
}
|
||||
if len(c.gone) > 0 && unmarked(v.Command) == v.Command {
|
||||
t.Fatalf("nothing masked: %q", v.Command)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a secret known by its field's name, which only the arguments' text
|
||||
// says: masked as it was
|
||||
out, n := MaskJSON(chatWithCall(`{"password":"hunter2Secret99","note":"line\nzoe@quill.dev"}`), Options{Personal: true, Secrets: true})
|
||||
if got := argsOf(t, out)[0]; n != 2 || !json.Valid([]byte(got)) || strings.Contains(got, "hunter2Secret99") || strings.Contains(got, "zoe@") {
|
||||
t.Fatalf("%d: %s", n, got)
|
||||
}
|
||||
|
||||
// a word of the user's own, after an escape: "\tango" is a tab and ango
|
||||
out, _ = MaskJSON(chatWithCall(`{"command":"a\tango and tango"}`), Options{Words: []string{"tango"}})
|
||||
got := argsOf(t, out)[0]
|
||||
var v struct{ Command string }
|
||||
if err := json.Unmarshal([]byte(got), &v); err != nil {
|
||||
t.Fatalf("arguments no longer JSON (%v): %s", err, got)
|
||||
}
|
||||
if !strings.HasPrefix(v.Command, "a\tango and {{TERM_") {
|
||||
t.Fatalf("command: %q", v.Command)
|
||||
}
|
||||
}
|
||||
|
||||
// What the vendor writes back in a tool call of its own, the placeholder
|
||||
// in its arguments, is put back as the value the agent wrote: a private
|
||||
// key's newlines are newlines again, not the two characters \ and n, and
|
||||
// the arguments still parse.
|
||||
func TestRestoreArgumentsFromArguments(t *testing.T) {
|
||||
key := "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA3Tz2mr\n-----END RSA PRIVATE KEY-----"
|
||||
command := "cat > id_rsa <<'EOF'\n" + key + "\nEOF\ngit config user.email \"rkato@quill.dev\""
|
||||
inner, _ := json.Marshal(map[string]string{"command": command})
|
||||
out, n := MaskJSON(chatWithCall(string(inner)), Options{Personal: true, Secrets: true})
|
||||
if n != 2 {
|
||||
t.Fatalf("%d masked, want 2: %s", n, out)
|
||||
}
|
||||
sent := argsOf(t, out)[0]
|
||||
if strings.Contains(sent, "MIIEow") || strings.Contains(sent, "rkato@quill.dev") {
|
||||
t.Fatalf("went through: %s", sent)
|
||||
}
|
||||
// the vendor calls the tool again with what it read
|
||||
a, _ := json.Marshal(sent)
|
||||
reply := []byte(`{"choices":[{"index":0,"message":{"role":"assistant","content":null,"tool_calls":[{"id":"call_2","type":"function","function":{"name":"pwsh","arguments":` + string(a) + `}}]},"finish_reason":"tool_calls"}]}`)
|
||||
back := argsOf(t, RestoreJSON(reply))[0]
|
||||
var v struct{ Command string }
|
||||
if err := json.Unmarshal([]byte(back), &v); err != nil {
|
||||
t.Fatalf("restored arguments no longer JSON (%v): %s", err, back)
|
||||
}
|
||||
if v.Command != command {
|
||||
t.Fatalf("restored:\n%q\nwant\n%q", v.Command, command)
|
||||
}
|
||||
}
|
||||
|
||||
// Arguments cut short by the model are no JSON as a whole, and are read
|
||||
// the same way: the decorator goes as it is, and the address after a
|
||||
// quote is still masked.
|
||||
func TestMaskJSONArgumentsCutShort(t *testing.T) {
|
||||
args := `{"command":"print('before')\n@mcp.tool()\ngit config user.email \"rkato@quill.dev\"\ndef demo(`
|
||||
out, n := MaskJSON(chatWithCall(args), Options{Personal: true})
|
||||
got := argsOf(t, out)[0]
|
||||
if n != 1 || strings.Contains(got, `\{{`) || !strings.Contains(got, `\n@mcp.tool()`) || strings.Contains(got, "rkato@quill.dev") {
|
||||
t.Fatalf("%d: %s", n, got)
|
||||
}
|
||||
|
||||
// a value is cut back to the escapes' edges: a \u escape's digits are
|
||||
// inside it too
|
||||
s := `x\u0040y\nz`
|
||||
found := outsideEscapes(s, []span{
|
||||
{3, 5, "A", ByRule}, // 00 of \u0040: nothing left
|
||||
{1, 7, "B", ByRule}, // the whole \u0040: as it is
|
||||
{0, 4, "C", ByRule}, // ends in it: x
|
||||
{7, 9, "D", ByRule}, // y and the backslash of \n: y
|
||||
{9, 11, "E", ByRule}, // the n of \n and z: z
|
||||
{8, 11, "F", ByRule}, // \nz: as it is
|
||||
})
|
||||
var kept []string
|
||||
for _, f := range found {
|
||||
kept = append(kept, f.kind+":"+s[f.start:f.end])
|
||||
}
|
||||
if got := strings.Join(kept, " "); got != `B:\u0040 C:x D:y E:z F:\nz` {
|
||||
t.Fatalf("kept %s", got)
|
||||
}
|
||||
|
||||
// a user's rule that reads up to the backslash of \": the JSON holds,
|
||||
// and what the rule found before it is masked
|
||||
out, n = MaskJSON(chatWithCall(`{"cmd":"x REL-ab\"c d"}`), Options{Secrets: true, Rules: []Rule{{Kind: "rel", Regex: `REL-[a-z\\]+`}}})
|
||||
if got := argsOf(t, out)[0]; n != 1 || !json.Valid([]byte(got)) || strings.Contains(got, "REL-ab") || !strings.Contains(got, `\"c d`) {
|
||||
t.Fatalf("%d: %s", n, got)
|
||||
}
|
||||
}
|
||||
@@ -396,7 +396,7 @@ func MaskJSON(body []byte, o Options) ([]byte, int) {
|
||||
if !masked(path, key, s) {
|
||||
return s
|
||||
}
|
||||
t, n := mask(s, o, put)
|
||||
t, n := mask(s, o, put, asJSON(key, ""))
|
||||
total += n
|
||||
return t
|
||||
})
|
||||
|
||||
+125
-7
@@ -21,6 +21,7 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -477,21 +478,39 @@ func anyCase(w string) *regexp.Regexp {
|
||||
|
||||
// Mask swaps what o covers in s for placeholders, and says how many.
|
||||
func Mask(s string, o Options) (string, int) {
|
||||
return mask(s, withModel(o, []string{s}), placeholder)
|
||||
return mask(s, withModel(o, []string{s}), placeholder, false)
|
||||
}
|
||||
|
||||
// Preview is Mask with nothing remembered: each value goes as the
|
||||
// placeholder it would have, which isn't kept for Restore or as a value
|
||||
// seen before (known.go).
|
||||
func Preview(s string, o Options) string {
|
||||
out, _ := mask(s, withModel(o, []string{s}), tag)
|
||||
out, _ := mask(s, withModel(o, []string{s}), tag, false)
|
||||
return out
|
||||
}
|
||||
|
||||
// mask swaps what o covers in s for what put makes of each value.
|
||||
func mask(s string, o Options, put func(kind, v string) string) (string, int) {
|
||||
if len(s) < 3 {
|
||||
return s, 0
|
||||
//
|
||||
// escaped says s holds JSON text of its own (asJSON: a tool call's
|
||||
// arguments), which the vendor reads after one more unescaping. There the
|
||||
// finders read s as the vendor does (unescapedView): the \n before
|
||||
// @mcp.tool is a newline, not an n that starts an address, and the \n
|
||||
// before jo@quill.dev is no letter of it either. No value starts or ends
|
||||
// inside an escape (outsideEscapes), and what is kept to put back is the
|
||||
// value as it reads unescaped, so Restore, which escapes it again in
|
||||
// arguments, gives the agent what it wrote. Read as plain text, the n of
|
||||
// a \n went as the first letter of n@mcp.tool, the backslash was left
|
||||
// before the placeholder (\{{EMAIL_…}}), the arguments were JSON no
|
||||
// longer, and Ollama Cloud turned away every turn that replayed the call
|
||||
// with 400 "invalid tool call arguments" (#3022).
|
||||
func mask(raw string, o Options, put func(kind, v string) string, escaped bool) (string, int) {
|
||||
if len(raw) < 3 {
|
||||
return raw, 0
|
||||
}
|
||||
s := raw // what the finders read: as long as raw, a place in one is the same in the other
|
||||
if escaped {
|
||||
s = unescapedView(raw)
|
||||
put = unescapedPut(put)
|
||||
}
|
||||
var found []span
|
||||
for _, w := range o.Words {
|
||||
@@ -583,9 +602,108 @@ func mask(s string, o Options, put func(kind, v string) string) (string, int) {
|
||||
}
|
||||
// after the rules', so where a rule finds the same value it keeps the
|
||||
// kind it has always had
|
||||
found = append(found, modelSpans(s, o)...)
|
||||
found = append(found, modelSpans(raw, o)...) // the model was asked about raw
|
||||
found = append(found, knownSpans(s, o)...)
|
||||
return apply(s, noDecimals(s, noIDs(s, found)), put, o)
|
||||
if escaped {
|
||||
found = outsideEscapes(raw, found)
|
||||
}
|
||||
return apply(raw, noDecimals(s, noIDs(s, found)), put, o)
|
||||
}
|
||||
|
||||
// unescapedView is s, JSON text, as long as s and read as the vendor reads
|
||||
// it once unescaped, as far as the finders tell: an escape that stands for
|
||||
// a newline, a tab or another control character has its letter read as
|
||||
// one (\n as \ and a newline), and a \u escape's u and digits as line
|
||||
// breaks, so neither starts or ends a word. A backslash, a quote and a
|
||||
// slash escaped stay as written: the rules read \" as a quote and C:\\Users
|
||||
// as a Windows path.
|
||||
func unescapedView(s string) string {
|
||||
if strings.IndexByte(s, '\\') < 0 {
|
||||
return s
|
||||
}
|
||||
b := []byte(s)
|
||||
for i := 0; i+1 < len(b); i++ {
|
||||
if b[i] != '\\' {
|
||||
continue
|
||||
}
|
||||
i++
|
||||
switch b[i] {
|
||||
case 'n', 'b', 'f':
|
||||
b[i] = '\n'
|
||||
case 'r':
|
||||
b[i] = '\r'
|
||||
case 't':
|
||||
b[i] = '\t'
|
||||
case 'u':
|
||||
for j := i; j < i+5 && j < len(b); j++ {
|
||||
b[j] = '\n'
|
||||
}
|
||||
i += 4
|
||||
}
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// unescapedPut is put given each value unescaped, as the vendor and the
|
||||
// agent read it: a private key's \n are newlines. A value that isn't a
|
||||
// JSON string's text goes as it is.
|
||||
func unescapedPut(put func(kind, v string) string) func(kind, v string) string {
|
||||
return func(kind, v string) string {
|
||||
if strings.IndexByte(v, '\\') >= 0 {
|
||||
var u string
|
||||
if json.Unmarshal([]byte(`"`+v+`"`), &u) == nil {
|
||||
v = u
|
||||
}
|
||||
}
|
||||
return put(kind, v)
|
||||
}
|
||||
}
|
||||
|
||||
// outsideEscapes is found with no value starting or ending inside one of
|
||||
// the escapes of s, JSON text: after an escape's backslash, as the n of
|
||||
// \n, or among a \u00e9's digits. Masked there, it would leave a
|
||||
// backslash before the placeholder or cut one off from its letter, and s
|
||||
// would be JSON no longer (#3022). A value is cut back to the escapes'
|
||||
// edges instead, so what it holds is still masked: the zoe@quill.dev of a
|
||||
// model's "nzoe@quill.dev" read in \nzoe@quill.dev, a user's rule that
|
||||
// reads up to the backslash of \". One left with nothing goes. Every
|
||||
// finder's values go through here: the rules', the user's words, the
|
||||
// model's and those known before.
|
||||
func outsideEscapes(s string, found []span) []span {
|
||||
if len(found) == 0 || strings.IndexByte(s, '\\') < 0 {
|
||||
return found
|
||||
}
|
||||
// esc[i], for i inside an escape past its backslash: where the escape
|
||||
// starts and ends, plus one each so a zero is outside one
|
||||
type edges struct{ from, to int }
|
||||
esc := make([]edges, len(s)+1)
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] != '\\' {
|
||||
continue
|
||||
}
|
||||
n := 2
|
||||
if i+1 < len(s) && s[i+1] == 'u' {
|
||||
n = 6
|
||||
}
|
||||
end := min(i+n, len(s))
|
||||
for j := i + 1; j < end; j++ {
|
||||
esc[j] = edges{i + 1, end + 1}
|
||||
}
|
||||
i = end - 1
|
||||
}
|
||||
out := found[:0]
|
||||
for _, f := range found {
|
||||
if e := esc[f.start]; e.to > 0 {
|
||||
f.start = e.to - 1
|
||||
}
|
||||
if e := esc[f.end]; e.from > 0 {
|
||||
f.end = e.from - 1
|
||||
}
|
||||
if f.start < f.end {
|
||||
out = append(out, f)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// noDecimals is found without what is a piece of a decimal number of s:
|
||||
|
||||
@@ -51,7 +51,7 @@ func scrub(s string) string {
|
||||
if len(s) >= 256 {
|
||||
scrubRuns.Add(1)
|
||||
}
|
||||
out, _ := mask(s, Options{Secrets: true}, scrubbed)
|
||||
out, _ := mask(s, Options{Secrets: true}, scrubbed, false)
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user