fix(sec): secret guard no longer leaks creds — case-insensitive + more patterns 🔑🛡️
Audit found the guard silently uploads real credentials: - patterns were case-sensitive: .ENV / KEY.PEM / ID_RSA / Server.Key all passed through on macOS/Windows. New ignore.CompileFold compiles the guard deny-list case-insensitively (.devignore stays case-sensitive, gitignore semantics). - added *.env and .envrc (prod.env / config.env hold the same secrets as .env). - .aws/credentials and .ssh/id_* were root-anchored — a nested copy leaked; now **/ depth-matched. Regression test covers all three.
This commit is contained in:
parent
3700e4a491
commit
7366ab757c
3 changed files with 55 additions and 8 deletions
|
|
@ -19,11 +19,20 @@ type rule struct {
|
|||
dirOnly bool
|
||||
}
|
||||
|
||||
// Compile parses the lines of a .devignore file into a Matcher.
|
||||
func Compile(patterns []string) (*Matcher, error) {
|
||||
// Compile parses the lines of a .devignore file into a Matcher (case-sensitive,
|
||||
// matching gitignore semantics).
|
||||
func Compile(patterns []string) (*Matcher, error) { return compile(patterns, false) }
|
||||
|
||||
// CompileFold is like Compile but matches case-insensitively. It exists for the
|
||||
// secret guard: on case-insensitive filesystems (macOS/Windows) a file literally
|
||||
// named .ENV / KEY.PEM / ID_RSA is the same secret as .env / key.pem / id_rsa and
|
||||
// must also be blocked. .devignore keeps using Compile (case-sensitive).
|
||||
func CompileFold(patterns []string) (*Matcher, error) { return compile(patterns, true) }
|
||||
|
||||
func compile(patterns []string, fold bool) (*Matcher, error) {
|
||||
m := &Matcher{}
|
||||
for _, p := range patterns {
|
||||
r, ok, err := compilePattern(p)
|
||||
r, ok, err := compilePattern(p, fold)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -34,7 +43,7 @@ func Compile(patterns []string) (*Matcher, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
func compilePattern(line string) (rule, bool, error) {
|
||||
func compilePattern(line string, fold bool) (rule, bool, error) {
|
||||
// Trim trailing whitespace; gitignore keeps leading whitespace literal.
|
||||
line = strings.TrimRight(line, " \t\r\n")
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
|
|
@ -56,7 +65,11 @@ func compilePattern(line string) (rule, bool, error) {
|
|||
anchored := strings.Contains(line, "/")
|
||||
line = strings.TrimPrefix(line, "/")
|
||||
|
||||
re, err := regexp.Compile(buildRegex(line, anchored))
|
||||
expr := buildRegex(line, anchored)
|
||||
if fold {
|
||||
expr = "(?i)" + expr // case-insensitive match (secret guard only)
|
||||
}
|
||||
re, err := regexp.Compile(expr)
|
||||
if err != nil {
|
||||
return rule{}, false, err
|
||||
}
|
||||
|
|
|
|||
32
internal/secret/leak_regression_test.go
Normal file
32
internal/secret/leak_regression_test.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package secret
|
||||
|
||||
import "testing"
|
||||
|
||||
// Regression for the audit's credential-leak findings: the secret guard must
|
||||
// block these despite case, .env naming variants, and nesting depth.
|
||||
func TestSecretGuardBlocksLeakyNames(t *testing.T) {
|
||||
g, err := New(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
blocked := []string{
|
||||
// #5 case-insensitive (macOS/Windows): same secret, different case.
|
||||
".ENV", ".Env", "KEY.PEM", "cert.PEM", "ID_RSA", "Server.Key",
|
||||
// #17 common env-file naming conventions.
|
||||
"prod.env", "config.env", ".envrc",
|
||||
// #16 nested copies of anchored secrets.
|
||||
"home/.aws/credentials", "deep/path/.aws/credentials", "x/.ssh/id_work",
|
||||
// sanity: the originals still block.
|
||||
".env", "id_rsa", "secrets/token",
|
||||
}
|
||||
for _, p := range blocked {
|
||||
if !g.Blocked(p) {
|
||||
t.Errorf("Blocked(%q) = false, want true (credential leak)", p)
|
||||
}
|
||||
}
|
||||
|
||||
// The non-secret template must stay allowed (negation still works).
|
||||
if g.Blocked(".env.example") {
|
||||
t.Error("Blocked(.env.example) = true, want false (it's a template, not a secret)")
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,8 @@ import "git.shoemoney.ai/shoemoney/devbox/internal/ignore"
|
|||
var DefaultPatterns = []string{
|
||||
".env",
|
||||
".env.*",
|
||||
"*.env", // prod.env / config.env conventions hold the same creds as .env
|
||||
".envrc",
|
||||
"!.env.example",
|
||||
"*.pem",
|
||||
"*.key",
|
||||
|
|
@ -23,8 +25,8 @@ var DefaultPatterns = []string{
|
|||
"id_ecdsa*",
|
||||
"id_ed25519*",
|
||||
"secrets/",
|
||||
".aws/credentials",
|
||||
".ssh/id_*",
|
||||
"**/.aws/credentials", // not just at root — a nested copy is just as sensitive
|
||||
"**/.ssh/id_*",
|
||||
}
|
||||
|
||||
// Guard decides whether a path must never be uploaded.
|
||||
|
|
@ -35,7 +37,7 @@ func New(extra []string) (*Guard, error) {
|
|||
pats := make([]string, 0, len(DefaultPatterns)+len(extra))
|
||||
pats = append(pats, DefaultPatterns...)
|
||||
pats = append(pats, extra...)
|
||||
m, err := ignore.Compile(pats)
|
||||
m, err := ignore.CompileFold(pats) // secrets block case-insensitively (.ENV == .env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue