Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-15971.md
Original file line number Diff line number Diff line change
@@ -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).
8 changes: 7 additions & 1 deletion src/pkg/dashboard/contribute_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/pkg/dashboard/matrix_source_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dashboard

import (
"path"
"regexp"
"strings"
"unicode"
"unicode/utf8"
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions src/pkg/dashboard/matrix_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <redacted> 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 {
Expand Down Expand Up @@ -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()
}
Loading