httpcaddyfile: Fix panic in automation policy consolidation#4104
httpcaddyfile: Fix panic in automation policy consolidation#4104mholt merged 2 commits intocaddyserver:masterfrom
Conversation
mholt
left a comment
There was a problem hiding this comment.
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
| 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.
I'm actually wondering if decrementing i here is the bug, not decrementing it past 0... hmm.
caddyconfig/httpcaddyfile/tlsapp.go
Outdated
| if automationPolicyShadows(i, aps) >= j { | ||
| aps = append(aps[:i], aps[i+1:]...) | ||
| i-- | ||
| i = max(0, i-1) |
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 subjectsWhat 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 |
Fixes #4101
The panic:
The problem is that
igets 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.