cloudflare/pint

Public

mirrored fromhttps://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b13a87cdb942543011caed30a8da0abe98670667

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/git/filter.go

69lines · modecode

1package git
2
3import (
4 "context"
5 "log/slog"
6 "regexp"
7)
8
9func NewPathFilter(include, exclude, relaxed []*regexp.Regexp) PathFilter {
10 return PathFilter{
11 include: include,
12 exclude: exclude,
13 relaxed: relaxed,
14 }
15}
16
17type PathFilter struct {
18 include []*regexp.Regexp
19 exclude []*regexp.Regexp
20 relaxed []*regexp.Regexp
21}
22
23func (pf PathFilter) IsPathAllowed(path string) (ok bool) {
24 if len(pf.include) == 0 && len(pf.exclude) == 0 {
25 return true
26 }
27
28 for _, pattern := range pf.exclude {
29 if pattern.MatchString(path) {
30 slog.LogAttrs(
31 context.Background(), slog.LevelDebug, "File path is in the exclude list",
32 slog.String("path", path),
33 slog.Any("exclude", pf.exclude),
34 )
35 return false
36 }
37 }
38
39 for _, pattern := range pf.include {
40 if pattern.MatchString(path) {
41 slog.LogAttrs(
42 context.Background(), slog.LevelDebug, "File path is in the include list",
43 slog.String("path", path),
44 slog.Any("include", pf.include),
45 )
46 return true
47 }
48 }
49
50 ok = len(pf.include) == 0
51 if !ok {
52 slog.LogAttrs(
53 context.Background(), slog.LevelDebug, "File path is not allowed",
54 slog.String("path", path),
55 slog.Any("include", pf.include),
56 slog.Any("exclude", pf.exclude),
57 )
58 }
59 return ok
60}
61
62func (pf PathFilter) IsRelaxed(path string) bool {
63 for _, r := range pf.relaxed {
64 if v := r.MatchString(path); v {
65 return true
66 }
67 }
68 return false
69}
70