mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-07-15 22:41:14 +00:00
refactor: rework the way trusted proxies ip work (#1007)
This commit is contained in:
@@ -215,6 +215,10 @@ type IPAllowedRule struct {
|
||||
}
|
||||
|
||||
func (rule *IPAllowedRule) Evaluate(ctx *ACLContext) Effect {
|
||||
if !ctx.TrustedProxiesConfigured {
|
||||
return EffectAllow // We can't block the proxy
|
||||
}
|
||||
|
||||
// merge global and per-app block/allow lists
|
||||
blockedIps := append([]string{}, rule.Config.Auth.IP.Block...)
|
||||
allowedIPs := append([]string{}, rule.Config.Auth.IP.Allow...)
|
||||
@@ -263,6 +267,10 @@ type IPBypassedRule struct {
|
||||
}
|
||||
|
||||
func (rule *IPBypassedRule) Evaluate(ctx *ACLContext) Effect {
|
||||
if !ctx.TrustedProxiesConfigured {
|
||||
return EffectDeny
|
||||
}
|
||||
|
||||
// merge global and per-app bypass lists
|
||||
bypassList := append([]string{}, rule.Config.Auth.IP.Bypass...)
|
||||
if ctx.ACLs != nil {
|
||||
|
||||
@@ -611,11 +611,20 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
ctx *ACLContext
|
||||
expected Effect
|
||||
}{
|
||||
{
|
||||
name: "when trusted proxies are not configured, IP is allowed",
|
||||
ctx: &ACLContext{
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
{
|
||||
name: "allows when ACLs are nil and no global lists configured",
|
||||
ctx: &ACLContext{
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -625,7 +634,8 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Block: []string{"10.0.0.1"}},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -637,8 +647,9 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
},
|
||||
},
|
||||
ctx: &ACLContext{
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -648,7 +659,8 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Allow: []string{"192.168.1.0/24"}},
|
||||
},
|
||||
IP: net.ParseIP("192.168.1.10"),
|
||||
IP: net.ParseIP("192.168.1.10"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -660,8 +672,9 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
},
|
||||
},
|
||||
ctx: &ACLContext{
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("192.168.1.10"),
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("192.168.1.10"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -671,15 +684,17 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Allow: []string{"192.168.1.0/24"}},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
{
|
||||
name: "allows when no block or allow lists are configured",
|
||||
ctx: &ACLContext{
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -692,7 +707,8 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
Allow: []string{"10.0.0.1"},
|
||||
},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -705,7 +721,8 @@ func TestIPAllowedRule(t *testing.T) {
|
||||
Allow: []string{"10.0.0.1"},
|
||||
},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -735,12 +752,23 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
ctx *ACLContext
|
||||
expected Effect
|
||||
}{
|
||||
{
|
||||
name: "when trusted proxies are not configured, IP is not bypassed",
|
||||
rule: defaultIPBR,
|
||||
ctx: &ACLContext{
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: false,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
{
|
||||
name: "deny when ACLs are nil and no global bypass",
|
||||
rule: defaultIPBR,
|
||||
ctx: &ACLContext{
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -748,8 +776,9 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
name: "allows when ACLs are nil but IP matches global bypass",
|
||||
rule: globBypassIPBR,
|
||||
ctx: &ACLContext{
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -757,8 +786,9 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
name: "denies when ACLs are nil and IP does not match global bypass",
|
||||
rule: globBypassIPBR,
|
||||
ctx: &ACLContext{
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("192.168.1.1"),
|
||||
ACLs: nil,
|
||||
IP: net.ParseIP("192.168.1.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -769,7 +799,8 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Bypass: []string{"10.0.0.0/24"}},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -780,7 +811,8 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Bypass: []string{"172.16.0.0/24"}},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -791,7 +823,8 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Bypass: []string{"10.0.0.0/24"}},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
IP: net.ParseIP("10.0.0.5"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
@@ -802,7 +835,8 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Bypass: []string{"10.0.0.0/24"}},
|
||||
},
|
||||
IP: net.ParseIP("192.168.1.1"),
|
||||
IP: net.ParseIP("192.168.1.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -810,8 +844,9 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
name: "denies when bypass list is empty",
|
||||
rule: defaultIPBR,
|
||||
ctx: &ACLContext{
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
ACLs: &model.App{},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectDeny,
|
||||
},
|
||||
@@ -822,7 +857,8 @@ func TestIPBypassedRule(t *testing.T) {
|
||||
ACLs: &model.App{
|
||||
IP: model.AppIP{Bypass: []string{"not-an-ip", "10.0.0.1"}},
|
||||
},
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
IP: net.ParseIP("10.0.0.1"),
|
||||
TrustedProxiesConfigured: true,
|
||||
},
|
||||
expected: EffectAllow,
|
||||
},
|
||||
|
||||
@@ -29,10 +29,11 @@ type Rule interface {
|
||||
}
|
||||
|
||||
type ACLContext struct {
|
||||
ACLs *model.App
|
||||
UserContext *model.UserContext
|
||||
IP net.IP
|
||||
Path string
|
||||
ACLs *model.App
|
||||
UserContext *model.UserContext
|
||||
IP net.IP
|
||||
Path string
|
||||
TrustedProxiesConfigured bool
|
||||
}
|
||||
|
||||
type PolicyEngine struct {
|
||||
|
||||
Reference in New Issue
Block a user