|
4 | 4 | "net/http" |
5 | 5 | "slices" |
6 | 6 | "strings" |
| 7 | + "path" |
7 | 8 |
|
8 | 9 | log "github.com/codeshelldev/secured-signal-api/utils/logger" |
9 | 10 | ) |
@@ -52,38 +53,45 @@ func getEndpoints(endpoints []string) ([]string, []string) { |
52 | 53 | return allowedEndpoints, blockedEndpoints |
53 | 54 | } |
54 | 55 |
|
| 56 | +func matchesPattern(endpoint, pattern string) bool { |
| 57 | + ok, _ := path.Match(pattern, endpoint) |
| 58 | + return |
| 59 | +} |
| 60 | + |
55 | 61 | func isBlocked(endpoint string, endpoints []string) bool { |
56 | | - if endpoints == nil { |
57 | | - return false |
58 | | - } else if len(endpoints) <= 0 { |
59 | | - return false |
| 62 | + if len(endpoints) == 0 { |
| 63 | + // default: block all |
| 64 | + return true |
60 | 65 | } |
61 | 66 |
|
62 | 67 | allowed, blocked := getEndpoints(endpoints) |
63 | 68 |
|
64 | | - isExplicitlyBlocked := slices.ContainsFunc(blocked, func(try string) bool { |
65 | | - return strings.HasPrefix(endpoint, try) |
| 69 | + isExplicitlyAllowed := slices.ContainsFunc(allowed, func(try string) bool { |
| 70 | + return matchesPattern(endpoint, try) |
66 | 71 | }) |
67 | | - |
68 | | - isExplictlyAllowed := slices.ContainsFunc(allowed, func(try string) bool { |
69 | | - return strings.HasPrefix(endpoint, try) |
| 72 | + isExplicitlyBlocked := slices.ContainsFunc(blocked, func(try string) bool { |
| 73 | + return matchesPattern(endpoint, try) |
70 | 74 | }) |
71 | 75 |
|
72 | | - // Block all except explicitly Allowed |
73 | | - if len(blocked) == 0 && len(allowed) != 0 { |
74 | | - return !isExplictlyAllowed |
| 76 | + // explicit allow > block |
| 77 | + if isExplicitlyAllowed { |
| 78 | + return false |
| 79 | + } |
| 80 | + |
| 81 | + if isExplicitlyBlocked { |
| 82 | + return true |
75 | 83 | } |
76 | 84 |
|
77 | | - // Allow all except explicitly Blocked |
78 | | - if len(allowed) == 0 && len(blocked) != 0 { |
79 | | - return isExplicitlyBlocked |
| 85 | + // only allowed endpoints -> block anything not allowed |
| 86 | + if len(allowed) > 0 && len(blocked) == 0 { |
| 87 | + return true |
80 | 88 | } |
81 | 89 |
|
82 | | - // Excplicitly Blocked except excplictly Allowed |
83 | | - if len(blocked) != 0 && len(allowed) != 0 { |
84 | | - return isExplicitlyBlocked && !isExplictlyAllowed |
| 90 | + // only blocked endpoints -> allow anything not blocked |
| 91 | + if len(blocked) > 0 && len(allowed) == 0 { |
| 92 | + return false |
85 | 93 | } |
86 | 94 |
|
87 | | - // Block all |
| 95 | + // no match -> default: block all |
88 | 96 | return true |
89 | 97 | } |
0 commit comments