Skip to content

Commit

Permalink
[bug] fix date underflow/overflow with deny/warn days (#89)
Browse files Browse the repository at this point in the history
Signed-off-by: Benji Visser <benji@093b.org>
  • Loading branch information
noqcks authored Jul 27, 2023
1 parent 1b5c7fc commit 655b4e4
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions xeol/policy/policy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package policy

import (
"math"
"sort"
"time"

Expand All @@ -18,6 +19,12 @@ const (
DateLayout = "2006-01-02"
PolicyTypeWarn EvaluationType = "WARN"
PolicyTypeDeny EvaluationType = "DENY"

// set a max days for deny/warn policies
// to avoid overflow/underflow errors when
// calculating dates. 10 years should be
// more than enough for most use cases
MaxNumDays = 10 * 365
)

var timeNow = time.Now
Expand Down Expand Up @@ -93,12 +100,17 @@ func warnMatch(policy *xeolio.Policy, match match.Match) bool {
}

if policy.WarnDays != nil {
warnDays := *policy.WarnDays * -1
if math.Abs(float64(warnDays)) > MaxNumDays {
log.Debugf("warn days (%d) is greater than max days, setting to max days (%d)", warnDays, MaxNumDays)
warnDays = MaxNumDays
}
eolDate, err := time.Parse(DateLayout, match.Cycle.Eol)
if err != nil {
log.Errorf("invalid eol date: %s, %s", match.Cycle.Eol, err)
return false
}
warnDate = eolDate.Add(time.Duration(*policy.WarnDays*-1) * time.Hour * 24)
warnDate = eolDate.Add(time.Duration(warnDays) * time.Hour * 24)
}

if warnDate.IsZero() {
Expand All @@ -125,12 +137,18 @@ func denyMatch(policy *xeolio.Policy, match match.Match) bool {
}

if policy.DenyDays != nil {
denyDays := *policy.DenyDays * -1
if math.Abs(float64(denyDays)) > MaxNumDays {
log.Debugf("deny days (%d) is greater than max days, setting to max days (%d)", denyDays, MaxNumDays)
denyDays = MaxNumDays
}

eolDate, err := time.Parse(DateLayout, match.Cycle.Eol)
if err != nil {
log.Errorf("invalid eol date: %s, %s", match.Cycle.Eol, err)
return false
}
denyDate = eolDate.Add(time.Duration(*policy.DenyDays*-1) * time.Hour * 24)
denyDate = eolDate.Add(time.Duration(denyDays) * time.Hour * 24)
policy.DenyDate = denyDate.Format(DateLayout)
}

Expand Down

0 comments on commit 655b4e4

Please sign in to comment.