-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
httpcaddyfile: Fix panic in automation policy consolidation #4104
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, you beat me to it!
I will want to look into this too, since I'm not sure either of us 100% understand what the correct logic should be (yet). I just might need a day or ... three.
caddyconfig/httpcaddyfile/tlsapp.go
Outdated
@@ -497,7 +497,7 @@ func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls | |||
// if they're exactly equal in every way, just keep one of them | |||
if reflect.DeepEqual(aps[i], aps[j]) { | |||
aps = append(aps[:j], aps[j+1:]...) | |||
i-- | |||
i = max(0, i-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually wondering if decrementing i
here is the bug, not decrementing it past 0... hmm.
caddyconfig/httpcaddyfile/tlsapp.go
Outdated
@@ -523,7 +523,7 @@ func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls | |||
// '*.com', which might be different (yes we've seen this happen) | |||
if automationPolicyShadows(i, aps) >= j { | |||
aps = append(aps[:i], aps[i+1:]...) | |||
i-- | |||
i = max(0, i-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, this decrement was added in 7846bc1.
77be389
to
eff6359
Compare
Thanks for the test case -- I had a chance to look at this more, and this patch makes all the tests (including your new one) pass: diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go
index 45ba9d21..d14d2335 100644
--- a/caddyconfig/httpcaddyfile/tlsapp.go
+++ b/caddyconfig/httpcaddyfile/tlsapp.go
@@ -491,13 +491,14 @@ func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls
}
// remove or combine duplicate policies
+outer:
for i := 0; i < len(aps); i++ {
// compare only with next policies; we sorted by specificity so we must not delete earlier policies
for j := i + 1; j < len(aps); j++ {
// if they're exactly equal in every way, just keep one of them
if reflect.DeepEqual(aps[i], aps[j]) {
aps = append(aps[:j], aps[j+1:]...)
- i--
+ j--
break
}
@@ -524,6 +525,7 @@ func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls
if automationPolicyShadows(i, aps) >= j {
aps = append(aps[:i], aps[i+1:]...)
i--
+ continue outer
}
} else {
// avoid repeated subjects What do you think? It occurred to me that even if |
That sounds reasonable 👍 I'll adjust |
eff6359
to
f764d34
Compare
f764d34
to
17e5444
Compare
golangci-lint detected that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kewl, let's give this a try.
Fixes #4101
The panic:
The problem is that
i
gets decremented to-1
, which makes the next iteration of the loop try to accessaps[-1]
, out of range.I think this fix is good enough but I might have missed some subtleties.
I confirmed that the adapt test I added also triggers the panic before applying the code changes.