Skip to content

Commit 35bdfa8

Browse files
authored
fix: acl rule precedence (#1219)
1 parent c3b5c04 commit 35bdfa8

3 files changed

Lines changed: 152 additions & 10 deletions

File tree

‎internal/controller/proxy_controller.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
134134
return
135135
}
136136

137-
if controller.policyEngine.Evaluate(service.RuleAuthEnabled, aclsCtx) {
138-
controller.log.App.Debug().Msg("Authentication is disabled for this resource, allowing access without authentication")
139-
controller.setHeaders(c, acls)
140-
c.JSON(200, gin.H{
141-
"status": 200,
142-
"message": "Authenticated",
143-
})
144-
return
145-
}
146-
147137
if !controller.policyEngine.Evaluate(service.RuleIPAllowed, aclsCtx) {
148138
queries, err := query.Values(UnauthorizedQuery{
149139
Resource: strings.Split(proxyCtx.Host, ".")[0],
@@ -171,6 +161,16 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
171161
return
172162
}
173163

164+
if controller.policyEngine.Evaluate(service.RuleAuthEnabled, aclsCtx) {
165+
controller.log.App.Debug().Msg("Authentication is disabled for this resource, allowing access without authentication")
166+
controller.setHeaders(c, acls)
167+
c.JSON(200, gin.H{
168+
"status": 200,
169+
"message": "Authenticated",
170+
})
171+
return
172+
}
173+
174174
userContext, err := new(model.UserContext).NewFromGin(c)
175175

176176
if err != nil {

‎internal/controller/proxy_controller_test.go‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestProxyController(t *testing.T) {
2525
log.Init()
2626

2727
cfg, runtime := test.CreateTestConfigs(t)
28+
cfg.Auth.IP.Block = []string{"198.51.100.9"}
2829

2930
const browserUserAgent = `
3031
Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Mobile Safari/537.36`
@@ -510,6 +511,94 @@ func TestProxyController(t *testing.T) {
510511
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
511512
},
512513
},
514+
{
515+
description: "App IP block takes precedence over path rules",
516+
middlewares: []gin.HandlerFunc{},
517+
run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) {
518+
req := httptest.NewRequest("GET", "/api/auth/traefik", nil)
519+
req.Header.Set("x-forwarded-host", "acl-precedence-block.example.com")
520+
req.Header.Set("x-forwarded-proto", "https")
521+
req.Header.Set("x-forwarded-uri", "/allowed")
522+
req.Header.Set("x-forwarded-for", "10.10.10.10")
523+
router.ServeHTTP(recorder, req)
524+
525+
assert.Equal(t, http.StatusForbidden, recorder.Code)
526+
assert.Contains(t, recorder.Header().Get("x-tinyauth-location"), "ip=10.10.10.10")
527+
},
528+
},
529+
{
530+
description: "App IP allow list takes precedence over path rules",
531+
middlewares: []gin.HandlerFunc{},
532+
run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) {
533+
req := httptest.NewRequest("GET", "/api/auth/traefik", nil)
534+
req.Header.Set("x-forwarded-host", "acl-precedence-allow.example.com")
535+
req.Header.Set("x-forwarded-proto", "https")
536+
req.Header.Set("x-forwarded-uri", "/allowed")
537+
req.Header.Set("x-forwarded-for", "10.10.10.11")
538+
router.ServeHTTP(recorder, req)
539+
540+
assert.Equal(t, http.StatusForbidden, recorder.Code)
541+
assert.Contains(t, recorder.Header().Get("x-tinyauth-location"), "ip=10.10.10.11")
542+
},
543+
},
544+
{
545+
description: "Global IP block takes precedence over path rules",
546+
middlewares: []gin.HandlerFunc{},
547+
run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) {
548+
req := httptest.NewRequest("GET", "/api/auth/traefik", nil)
549+
req.Header.Set("x-forwarded-host", "acl-precedence-global.example.com")
550+
req.Header.Set("x-forwarded-proto", "https")
551+
req.Header.Set("x-forwarded-uri", "/allowed")
552+
req.Header.Set("x-forwarded-for", "198.51.100.9")
553+
router.ServeHTTP(recorder, req)
554+
555+
assert.Equal(t, http.StatusForbidden, recorder.Code)
556+
assert.Contains(t, recorder.Header().Get("x-tinyauth-location"), "ip=198.51.100.9")
557+
},
558+
},
559+
{
560+
description: "App IP block takes precedence over path block rules",
561+
middlewares: []gin.HandlerFunc{},
562+
run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) {
563+
req := httptest.NewRequest("GET", "/api/auth/traefik", nil)
564+
req.Header.Set("x-forwarded-host", "acl-precedence-path-block.example.com")
565+
req.Header.Set("x-forwarded-proto", "https")
566+
req.Header.Set("x-forwarded-uri", "/public")
567+
req.Header.Set("x-forwarded-for", "10.10.10.10")
568+
router.ServeHTTP(recorder, req)
569+
570+
assert.Equal(t, http.StatusForbidden, recorder.Code)
571+
assert.Contains(t, recorder.Header().Get("x-tinyauth-location"), "ip=10.10.10.10")
572+
},
573+
},
574+
{
575+
description: "Path rules allow unrestricted IPs",
576+
middlewares: []gin.HandlerFunc{},
577+
run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) {
578+
req := httptest.NewRequest("GET", "/api/auth/traefik", nil)
579+
req.Header.Set("x-forwarded-host", "acl-precedence-block.example.com")
580+
req.Header.Set("x-forwarded-proto", "https")
581+
req.Header.Set("x-forwarded-uri", "/allowed")
582+
req.Header.Set("x-forwarded-for", "10.10.10.11")
583+
router.ServeHTTP(recorder, req)
584+
585+
assert.Equal(t, http.StatusOK, recorder.Code)
586+
},
587+
},
588+
{
589+
description: "IP bypass retains highest precedence",
590+
middlewares: []gin.HandlerFunc{},
591+
run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) {
592+
req := httptest.NewRequest("GET", "/api/auth/traefik", nil)
593+
req.Header.Set("x-forwarded-host", "acl-precedence-bypass.example.com")
594+
req.Header.Set("x-forwarded-proto", "https")
595+
req.Header.Set("x-forwarded-uri", "/allowed")
596+
req.Header.Set("x-forwarded-for", "10.10.10.10")
597+
router.ServeHTTP(recorder, req)
598+
599+
assert.Equal(t, http.StatusOK, recorder.Code)
600+
},
601+
},
513602
{
514603
description: "Ensure ip bypass ACL works on forward auth",
515604
middlewares: []gin.HandlerFunc{},

‎internal/test/test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,59 @@ func CreateTestConfigs(t *testing.T) (model.Config, model.RuntimeConfig) {
7676
Block: "^/admin$",
7777
},
7878
},
79+
"app_acl_precedence_block": {
80+
Config: model.AppConfig{
81+
Domain: "acl-precedence-block.example.com",
82+
},
83+
Path: model.AppPath{
84+
Allow: "^/allowed$",
85+
},
86+
IP: model.AppIP{
87+
Block: []string{"10.10.10.10"},
88+
},
89+
},
90+
"app_acl_precedence_allow": {
91+
Config: model.AppConfig{
92+
Domain: "acl-precedence-allow.example.com",
93+
},
94+
Path: model.AppPath{
95+
Allow: "^/allowed$",
96+
},
97+
IP: model.AppIP{
98+
Allow: []string{"10.10.10.10"},
99+
},
100+
},
101+
"app_acl_precedence_global": {
102+
Config: model.AppConfig{
103+
Domain: "acl-precedence-global.example.com",
104+
},
105+
Path: model.AppPath{
106+
Allow: "^/allowed$",
107+
},
108+
},
109+
"app_acl_precedence_path_block": {
110+
Config: model.AppConfig{
111+
Domain: "acl-precedence-path-block.example.com",
112+
},
113+
Path: model.AppPath{
114+
Block: "^/private$",
115+
},
116+
IP: model.AppIP{
117+
Block: []string{"10.10.10.10"},
118+
},
119+
},
120+
"app_acl_precedence_bypass": {
121+
Config: model.AppConfig{
122+
Domain: "acl-precedence-bypass.example.com",
123+
},
124+
Path: model.AppPath{
125+
Allow: "^/allowed$",
126+
},
127+
IP: model.AppIP{
128+
Block: []string{"10.10.10.10"},
129+
Bypass: []string{"10.10.10.10"},
130+
},
131+
},
79132
"app_user_allow": {
80133
Config: model.AppConfig{
81134
Domain: "user-allow.example.com",

0 commit comments

Comments
 (0)