Skip to content

Commit 0562045

Browse files
[feat] Add .gopass-audit-ignore support to ignore secrets from audits (#2822)
* [feat] Add .gopass-audit-ignore support to ignore secrets from audits This PR adds a new exclude file that is used during gopass audit to ignore entries from auditing. The file itself is using RE2 syntax. Fixes #2806 Signed-off-by: Dominik Schulz <[email protected]> * Add some documentation Signed-off-by: Dominik Schulz <[email protected]> --------- Signed-off-by: Dominik Schulz <[email protected]>
1 parent 9edbf30 commit 0562045

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

docs/commands/audit.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ The `audit` command will decrypt all secrets and scan for weak passwords or othe
88
$ gopass audit
99
```
1010

11+
## Excludes
12+
13+
You can exclude certain secrets from the audit by adding a `.gopass-audit-exclude` file to the secret. The file should contain a list of RE2 patters to exclude, one per line. For example:
14+
15+
```
16+
# Lines starting with # are ignored. Trailing comments are not supported.
17+
# Exclude all secrets in the pin folder.
18+
# Note: These are RE2, not Glob patterns!
19+
pin/.*
20+
# Literal matches are also valid RE2 patterns
21+
test_folder/ignore_this
22+
# Gopass internally uses forward slashes as path separators, even on Windows. So no need to escape backslashes.
23+
```
24+
1125
## Password strength backends
1226

1327
| Backend | Description |
1428
|-------------------------------------------------|------------------------------------------------------------------------|
1529
| [`zxcvbn`](https://github.com/nbutton23/zxcvbn) | [zxcvbn](https://github.com/dropbox/zxcvbn) password strength checker. |
1630
| [`crunchy`](https://github.com/muesli/crunchy) | Crunchy password strength checker |
1731
| `name` | Checks if password equals the name of the secret |
18-
19-

internal/action/audit.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@ func (s *Action) Audit(c *cli.Context) error {
4545
return nil
4646
}
4747

48+
var excludes string
49+
st := s.Store.Storage(ctx, c.Args().First())
50+
if buf, err := st.Get(ctx, ".gopass-audit-ignore"); err == nil && buf != nil {
51+
excludes = string(buf)
52+
}
53+
nList := audit.FilterExcludes(excludes, list)
54+
if len(nList) < len(list) {
55+
out.Warningf(ctx, "Excluding %d secrets based on .gopass-audit-ignore", len(list)-len(nList))
56+
}
57+
4858
a := audit.New(c.Context, s.Store)
49-
r, err := a.Batch(ctx, list)
59+
r, err := a.Batch(ctx, nList)
5060
if err != nil {
5161
return exit.Error(exit.Unknown, err, "failed to audit password store: %s", err)
5262
}

internal/audit/excludes.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package audit
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"github.com/gopasspw/gopass/pkg/debug"
8+
)
9+
10+
type res []*regexp.Regexp
11+
12+
func (r res) Matches(s string) bool {
13+
for _, re := range r {
14+
if re.MatchString(s) {
15+
debug.Log("Matched %s against %s", s, re.String())
16+
17+
return true
18+
}
19+
}
20+
21+
return false
22+
}
23+
24+
// FilterExcludes filters the given list of secrets against the given exclude patterns (RE2 syntax).
25+
func FilterExcludes(excludes string, in []string) []string {
26+
debug.Log("Filtering %d secrets against %d exclude patterns", len(in), strings.Count(excludes, "\n"))
27+
28+
res := make(res, 0, 10)
29+
for _, line := range strings.Split(excludes, "\n") {
30+
line = strings.TrimSpace(line)
31+
if line == "" {
32+
continue
33+
}
34+
if strings.HasPrefix(line, "#") {
35+
continue
36+
}
37+
re, err := regexp.Compile(line)
38+
if err != nil {
39+
debug.Log("failed to compile exclude pattern %q: %s", line, err)
40+
41+
continue
42+
}
43+
debug.Log("Adding exclude pattern %q", re.String())
44+
res = append(res, re)
45+
}
46+
47+
// shortcut if we have no excludes
48+
if len(res) < 1 {
49+
return in
50+
}
51+
52+
// check all secrets against all excludes
53+
out := make([]string, 0, len(in))
54+
for _, s := range in {
55+
if res.Matches(s) {
56+
continue
57+
}
58+
out = append(out, s)
59+
}
60+
61+
return out
62+
}

0 commit comments

Comments
 (0)