oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runnerproto
2
3import (
4 "regexp"
5 "strings"
6 "unicode"
7 "unicode/utf8"
8)
9
10// DetailLimit bounds an error's words in a log line or a step report.
11const DetailLimit = 500
12
13var (
14 // A header that carries a credential, its value (and scheme) gone.
15 detailHeader = regexp.MustCompile(`(?i)\b((?:proxy-)?authorization|set-cookie|cookie|x-api-key|private-token)(["']?\s*[:=]\s*["']?)(?:(?:bearer|basic|token|digest|negotiate)\s+)?[^\s,;"']+`)
16 // A URL's userinfo (user:password@).
17 detailUserinfo = regexp.MustCompile(`(?i)\b([a-z][a-z0-9+.-]*://)[^/\s@?#'"]+@`)
18 // A JSON member whose name says it holds a credential.
19 detailJSONKey = regexp.MustCompile(`(?i)("[a-z0-9_-]*(?:token|secret|password|passwd|api[_-]?key|authorization|credentials?)"\s*:\s*")[^"]*"`)
20 // A JSON web token, and any long opaque run (a hub token is 64 hex;
21 // a commit SHA-1, 40, is kept).
22 detailJWT = regexp.MustCompile(`\beyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]*`)
23 detailOpaque = regexp.MustCompile(`\b[A-Za-z0-9_+]{41,}=*`)
24 detailSpace = regexp.MustCompile(`\s+`)
25)
26
27// RedactDetail is an error's words made safe to log or report: no
28// credential (bearer and basic values, Authorization and cookie headers,
29// URL userinfo, credential-named JSON members, known token prefixes,
30// JSON web tokens, long opaque runs), one line without control or
31// format characters, backticks as quotes (it may sit in a code block),
32// and at most limit bytes, the cut marked. Redaction runs before the
33// cut, so a credential across the bound is never half shown.
34func RedactDetail(s string, limit int) string {
35 s = strings.ToValidUTF8(s, "?")
36 s = strings.Map(func(r rune) rune {
37 if unicode.IsControl(r) || unicode.Is(unicode.Cf, r) {
38 return ' '
39 }
40 if r == '`' {
41 return '\''
42 }
43 return r
44 }, s)
45 s = detailHeader.ReplaceAllString(s, "${1}${2}***")
46 s = detailUserinfo.ReplaceAllString(s, "${1}***@")
47 s = detailJSONKey.ReplaceAllString(s, `${1}***"`)
48 s = detailJWT.ReplaceAllString(s, "***")
49 s = RedactText(s, nil)
50 s = detailOpaque.ReplaceAllString(s, "***")
51 s = strings.TrimSpace(detailSpace.ReplaceAllString(s, " "))
52 if len(s) <= limit {
53 return s
54 }
55 mark := "…"
56 if limit < len(mark) {
57 mark = ""
58 }
59 s = s[:max(limit-len(mark), 0)]
60 for !utf8.ValidString(s) {
61 s = s[:len(s)-1]
62 }
63 return s + mark
64}
65