diff --git a/changelog.d/fixed-15971.md b/changelog.d/fixed-15971.md new file mode 100644 index 0000000000..4e520275de --- /dev/null +++ b/changelog.d/fixed-15971.md @@ -0,0 +1 @@ +- Matrix lines that look like short `key=value` secrets (for example `password: hunter2`) are now dropped, and `Server.Close` now stops the Matrix line source goroutine (#15971, #15972). diff --git a/src/pkg/dashboard/contribute_ws.go b/src/pkg/dashboard/contribute_ws.go index 0ca6e2c8c3..4db0e7f962 100644 --- a/src/pkg/dashboard/contribute_ws.go +++ b/src/pkg/dashboard/contribute_ws.go @@ -5068,12 +5068,18 @@ func (s *Server) CloseContributeHub() { } // Close shuts down server-managed background resources, including the -// contributor hub. +// contributor hub and the Matrix line source. func (s *Server) Close() { if s == nil { return } s.CloseContributeHub() + s.matrixMu.Lock() + m := s.matrix + s.matrixMu.Unlock() + if m != nil { + m.close() + } } func contributorStatePath(contributorsDir, currentPath, name string) string { diff --git a/src/pkg/dashboard/matrix_source_filter.go b/src/pkg/dashboard/matrix_source_filter.go index b1e49b9d0e..0828eccb9f 100644 --- a/src/pkg/dashboard/matrix_source_filter.go +++ b/src/pkg/dashboard/matrix_source_filter.go @@ -2,6 +2,7 @@ package dashboard import ( "path" + "regexp" "strings" "unicode" "unicode/utf8" @@ -13,6 +14,13 @@ import ( // line may carry; a longer run looks like a key, hash or encoded secret. const matrixEntropyRun = 32 +// matrixKeyValueSecret matches a secret-looking key, optionally closed by a +// quote as in JSON, PHP or Ruby (`"password": "x"`, `'password' => 'x'`), +// followed by `=`, `:` or `=>` and a value, so short secrets such as +// `password: hunter2` are dropped. Go's `:=` and `==` are excluded so ordinary +// code is not over-dropped. +var matrixKeyValueSecret = regexp.MustCompile(`(?i)(?:password|passwd|pwd|secret|api[_-]?key|access[_-]?key|auth[_-]?token|token)["']?\s*(?:=[^=]|:[^=])\s*["']?[^\s"']`) + var matrixLockfiles = map[string]bool{ "go.sum": true, "package-lock.json": true, @@ -125,6 +133,9 @@ func matrixAcceptLine(raw string) (string, bool) { if matrixHasEntropyRun(line) { return "", false } + if matrixKeyValueSecret.MatchString(line) { + return "", false + } if logscrub.ContainsRedactionMarker(line) { return "", false } diff --git a/src/pkg/dashboard/matrix_source_test.go b/src/pkg/dashboard/matrix_source_test.go index 37c510f69a..fd6304f514 100644 --- a/src/pkg/dashboard/matrix_source_test.go +++ b/src/pkg/dashboard/matrix_source_test.go @@ -202,6 +202,16 @@ func TestMatrixAcceptLine(t *testing.T) { {name: "entropy run 31 ok", in: "id " + strings.Repeat("a", 31), want: "id " + strings.Repeat("a", 31), ok: true}, {name: "redaction marker", in: "value here"}, {name: "scrub changes", in: "key AKIA" + "IOSFODNN7EXAMPLE end"}, + {name: "yaml password", in: "password: hunter2"}, + {name: "env api key", in: "API_KEY=abc123"}, + {name: "quoted secret", in: `secret: "x"`}, + {name: "json password", in: `"password": "hunter2"`}, + {name: "json api key compact", in: `"api_key":"abc"`}, + {name: "php password", in: `'password' => 'hunter2'`}, + {name: "ruby symbol token", in: `token: "abc"`}, + {name: "go short declare kept", in: "token := parse(r)", want: "token := parse(r)", ok: true}, + {name: "comment kept", in: "// the token is refreshed hourly", want: "// the token is refreshed hourly", ok: true}, + {name: "identifier kept", in: "tokenize(line)", want: "tokenize(line)", ok: true}, {name: "pane scrub changes", in: "use sk-" + "abcdef12 now"}, } for _, tc := range tests { @@ -481,3 +491,19 @@ func TestMatrixSourceExpiredHashInLivePR(t *testing.T) { t.Fatalf("totalHashes = %d, want 2", m.totalHashes) } } + +func TestServerCloseStopsMatrixSource(t *testing.T) { + s := &Server{} + if s.MatrixObserver(matrixTestConfig()) == nil { + t.Fatal("active matrix returned no observer") + } + src := s.matrix + s.Close() + select { + case <-src.done: + case <-time.After(5 * time.Second): + t.Fatal("Server.Close did not stop the matrix goroutine") + } + s.Close() + (&Server{}).Close() +}