Skip to content

Commit a852ce2

Browse files
committed
added allowedEndpoints
1 parent d19f75f commit a852ce2

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

.github/templates/README.template.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ like Blocked Endpoints and any sort of Auth.
210210
> [!NOTE]
211211
> Blocked Endpoints can be reactivated by manually configuring them
212212

213-
### Blocked Endpoints
213+
### Endpoints
214214

215215
Because Secured Signal API is just a Proxy you can use all of the [Signal REST API](https://github.com/bbernhard/signal-cli-rest-api/blob/master/doc/EXAMPLES.md) endpoints except for...
216216

@@ -225,12 +225,29 @@ Because Secured Signal API is just a Proxy you can use all of the [Signal REST A
225225
| **/v1/accounts** |
226226
| **/v1/contacts** |
227227

228+
> [!NOTE]
229+
> Matching works by checking if the requested Endpoints startswith a Blocked or Allowed Endpoint
230+
228231
These Endpoints are blocked by default due to Security Risks, but can be modified by setting `blockedEndpoints` in your config:
229232

230233
```yaml
231234
blockedEndpoints: [/v1/register, /v1/unregister, /v1/qrcodelink, /v1/contacts]
232235
```
233236

237+
Override Blocked Endpoints by explicitly allowing endpoints in `allowedEndpoints`.
238+
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`** | ✅ |
246+
247+
```yaml
248+
allowedEndpoints: [/v2/send]
249+
```
250+
234251
### Variables
235252

236253
Placeholders can be added under `variables` and can then be referenced in the Body, Query or URL.

examples/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ messageAliases: [{ alias: "msg", score: 100 }]
1515

1616
blockedEndpoints:
1717
- /v1/about
18+
allowedEndpoints:
19+
- /v2/send

internals/proxy/middlewares/endpoints.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middlewares
33
import (
44
"net/http"
55
"slices"
6+
"strings"
67

78
log "github.com/codeshelldev/secured-signal-api/utils/logger"
89
)
@@ -15,15 +16,18 @@ func (data EndpointsMiddleware) Use() http.Handler {
1516
next := data.Next
1617

1718
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
18-
blockedEndpoints := getSettingsByReq(req).BLOCKED_ENDPOINTS
19+
settings := getSettingsByReq(req)
20+
21+
blockedEndpoints := settings.BLOCKED_ENDPOINTS
22+
allowedEndpoints := settings.ALLOWED_ENDPOINTS
1923

2024
if blockedEndpoints == nil {
2125
blockedEndpoints = getSettings("*").BLOCKED_ENDPOINTS
2226
}
2327

2428
reqPath := req.URL.Path
2529

26-
if slices.Contains(blockedEndpoints, reqPath) {
30+
if isBlocked(reqPath, allowedEndpoints, blockedEndpoints) {
2731
log.Warn("User tried to access blocked endpoint: ", reqPath)
2832
http.Error(w, "Forbidden", http.StatusForbidden)
2933
return
@@ -32,3 +36,27 @@ func (data EndpointsMiddleware) Use() http.Handler {
3236
next.ServeHTTP(w, req)
3337
})
3438
}
39+
40+
func isBlocked(endpoint string, allowed []string, blocked []string) bool {
41+
var result bool
42+
43+
if blocked == nil {
44+
return false
45+
}
46+
47+
if allowed == nil {
48+
return true
49+
}
50+
51+
isBlocked := slices.ContainsFunc(blocked, func(try string) bool {
52+
return strings.HasPrefix(endpoint, try)
53+
})
54+
55+
isExplictlyAllowed := slices.ContainsFunc(allowed, func(try string) bool {
56+
return strings.HasPrefix(endpoint, try)
57+
})
58+
59+
result = isBlocked && !isExplictlyAllowed
60+
61+
return result
62+
}

utils/config/loader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type ENV_ struct {
2626

2727
type SETTING_ struct {
2828
BLOCKED_ENDPOINTS []string `koanf:"blockedendpoints"`
29+
ALLOWED_ENDPOINTS []string `koanf:"allowedendpoints"`
2930
VARIABLES map[string]any `koanf:"variables"`
3031
MESSAGE_ALIASES []middlewareTypes.MessageAlias `koanf:"messagealiases"`
3132
}
@@ -38,6 +39,7 @@ var ENV *ENV_ = &ENV_{
3839
SETTINGS: map[string]*SETTING_{
3940
"*": {
4041
BLOCKED_ENDPOINTS: []string{},
42+
ALLOWED_ENDPOINTS: []string{},
4143
MESSAGE_ALIASES: []middlewareTypes.MessageAlias{},
4244
VARIABLES: map[string]any{},
4345
},
@@ -63,6 +65,7 @@ func InitEnv() {
6365
config.Unmarshal("variables", &defaultSettings.VARIABLES)
6466

6567
defaultSettings.BLOCKED_ENDPOINTS = config.Strings("blockedendpoints")
68+
defaultSettings.ALLOWED_ENDPOINTS = config.Strings("allowedendpoints")
6669
}
6770

6871
func Load() {

0 commit comments

Comments
 (0)