Skip to content

Commit 659fb56

Browse files
authored
Merge pull request #38 from CodeShellDev/dev
Update Main
2 parents 0d5e641 + 8486c72 commit 659fb56

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

.github/templates/README.template.md

Lines changed: 16 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,27 @@ 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 (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`** | ✅ |
244+
245+
```yaml
246+
allowedEndpoints: [/v2/send]
247+
```
248+
234249
### Variables
235250

236251
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: 43 additions & 3 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)
1920

20-
if blockedEndpoints == nil {
21+
blockedEndpoints := settings.BLOCKED_ENDPOINTS
22+
allowedEndpoints := settings.ALLOWED_ENDPOINTS
23+
24+
if blockedEndpoints == nil && allowedEndpoints == 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,39 @@ 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+
if blocked == nil {
42+
blocked = []string{}
43+
}
44+
45+
if allowed == nil {
46+
allowed = []string{}
47+
}
48+
49+
isExplicitlyBlocked := slices.ContainsFunc(blocked, func(try string) bool {
50+
return strings.HasPrefix(endpoint, try)
51+
})
52+
53+
isExplictlyAllowed := slices.ContainsFunc(allowed, func(try string) bool {
54+
return strings.HasPrefix(endpoint, try)
55+
})
56+
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+
}
71+
72+
// Block all
73+
return true
74+
}

utils/config/loader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
middlewareTypes "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares/types"
11+
"github.com/codeshelldev/secured-signal-api/utils"
1112
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1213
"github.com/knadh/koanf/parsers/yaml"
1314
)
@@ -26,6 +27,7 @@ type ENV_ struct {
2627

2728
type SETTING_ struct {
2829
BLOCKED_ENDPOINTS []string `koanf:"blockedendpoints"`
30+
ALLOWED_ENDPOINTS []string `koanf:"allowedendpoints"`
2931
VARIABLES map[string]any `koanf:"variables"`
3032
MESSAGE_ALIASES []middlewareTypes.MessageAlias `koanf:"messagealiases"`
3133
}
@@ -38,6 +40,7 @@ var ENV *ENV_ = &ENV_{
3840
SETTINGS: map[string]*SETTING_{
3941
"*": {
4042
BLOCKED_ENDPOINTS: []string{},
43+
ALLOWED_ENDPOINTS: []string{},
4144
MESSAGE_ALIASES: []middlewareTypes.MessageAlias{},
4245
VARIABLES: map[string]any{},
4346
},
@@ -63,6 +66,7 @@ func InitEnv() {
6366
config.Unmarshal("variables", &defaultSettings.VARIABLES)
6467

6568
defaultSettings.BLOCKED_ENDPOINTS = config.Strings("blockedendpoints")
69+
defaultSettings.ALLOWED_ENDPOINTS = config.Strings("allowedendpoints")
6670
}
6771

6872
func Load() {
@@ -87,6 +91,9 @@ func Load() {
8791
InitEnv()
8892

8993
log.Info("Finished Loading Configuration")
94+
95+
log.Dev("Loaded Config:\n" + utils.ToJson(config.All()))
96+
log.Dev("Loaded Token Configs:\n" + utils.ToJson(tokensLayer.All()))
9097
}
9198

9299
func LoadDefaults() {

0 commit comments

Comments
 (0)