Skip to content

Commit 3e76bff

Browse files
committed
fixed blocking logic
1 parent 991212c commit 3e76bff

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

.github/templates/README.template.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,11 @@ blockedEndpoints: [/v1/register, /v1/unregister, /v1/qrcodelink, /v1/contacts]
236236

237237
Override Blocked Endpoints by explicitly allowing endpoints in `allowedEndpoints`.
238238

239-
| Config (A) | (B) | Result | | | |
240-
| :-------------------------- | :------------------------------- | :---------: | --- | :------------: | --- |
241-
| `allowedEndpoints:` | | **all** | 🛑 | | |
242-
| `blockedEndpoints:` | | **all** | ✅ | | |
243-
| `allowedEndpoints:` | `blockedEndpoints: ["/v2/send"]` | **default** | ✅ | **`/v2/send`** | 🛑 |
244-
| `blockedEndpoints:` | `allowedEndpoints: ["/v2/send"]` | **default** | 🛑 | **`/v2/send`** | ✅ |
245-
| `blockedEndpoints: ["/v2"]` | `allowedEndpoints: ["/v2/send"]` | **`/v2*`** | 🛑 | **`/v2/send`** | ✅ |
239+
| Config (Allow) | (Block) | Result | | | |
240+
| :------------------------------- | :---------------------------------- | :--------: | --- | :---------------: | --- |
241+
| `allowedEndpoints: ["/v2/send"]` | `unset` | **all** | 🛑 | **`/v2/send`** | ✅ |
242+
| `unset` | `blockedEndpoints: ["/v1/receive"]` | **all** | ✅ | **`/v1/receive`** | 🛑 |
243+
| `blockedEndpoints: ["/v2"]` | `allowedEndpoints: ["/v2/send"]` | **`/v2*`** | 🛑 | **`/v2/send`** | ✅ |
246244

247245
```yaml
248246
allowedEndpoints: [/v2/send]

internals/proxy/middlewares/endpoints.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (data EndpointsMiddleware) Use() http.Handler {
2121
blockedEndpoints := settings.BLOCKED_ENDPOINTS
2222
allowedEndpoints := settings.ALLOWED_ENDPOINTS
2323

24-
if blockedEndpoints == nil {
24+
if blockedEndpoints == nil && allowedEndpoints == nil {
2525
blockedEndpoints = getSettings("*").BLOCKED_ENDPOINTS
2626
}
2727

@@ -38,25 +38,37 @@ func (data EndpointsMiddleware) Use() http.Handler {
3838
}
3939

4040
func isBlocked(endpoint string, allowed []string, blocked []string) bool {
41-
var result bool
42-
4341
if blocked == nil {
44-
return false
42+
blocked = []string{}
4543
}
4644

4745
if allowed == nil {
48-
return true
46+
allowed = []string{}
4947
}
5048

51-
isBlocked := slices.ContainsFunc(blocked, func(try string) bool {
49+
isExplicitlyBlocked := slices.ContainsFunc(blocked, func(try string) bool {
5250
return strings.HasPrefix(endpoint, try)
5351
})
5452

5553
isExplictlyAllowed := slices.ContainsFunc(allowed, func(try string) bool {
5654
return strings.HasPrefix(endpoint, try)
5755
})
5856

59-
result = isBlocked && !isExplictlyAllowed
57+
// Block all except explicitly Allowed
58+
if len(blocked) == 0 && len(allowed) != 0 {
59+
return !isExplictlyAllowed
60+
}
61+
62+
// Allow all except explicitly Blocked
63+
if len(allowed) == 0 && len(blocked) != 0{
64+
return isExplicitlyBlocked
65+
}
66+
67+
// Excplicitly Blocked except excplictly Allowed
68+
if len(blocked) != 0 && len(allowed) != 0 {
69+
return isExplicitlyBlocked && !isExplictlyAllowed
70+
}
6071

61-
return result
72+
// Block all
73+
return true
6274
}

0 commit comments

Comments
 (0)