mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
fix(configuration): time duration decode hook panic (#2960)
This fixes a potential panic in the time duration decode hook when the YAML value is a zero integer.
This commit is contained in:
parent
67846faacb
commit
1c1030c742
|
@ -67,7 +67,11 @@ func ToTimeDurationFunc() mapstructure.DecodeHookFuncType {
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case f.Kind() == reflect.String:
|
case f.Kind() == reflect.String:
|
||||||
break
|
dataStr := data.(string)
|
||||||
|
|
||||||
|
if duration, err = utils.ParseDurationString(dataStr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case f.Kind() == reflect.Int:
|
case f.Kind() == reflect.Int:
|
||||||
seconds := data.(int)
|
seconds := data.(int)
|
||||||
|
|
||||||
|
@ -84,14 +88,6 @@ func ToTimeDurationFunc() mapstructure.DecodeHookFuncType {
|
||||||
duration = time.Second * time.Duration(seconds)
|
duration = time.Second * time.Duration(seconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
if duration == 0 {
|
|
||||||
dataStr := data.(string)
|
|
||||||
|
|
||||||
if duration, err = utils.ParseDurationString(dataStr); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ptr {
|
if ptr {
|
||||||
return &duration, nil
|
return &duration, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -268,3 +268,25 @@ func TestToTimeDurationFunc_ShouldNotParse_FromBool(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, from, result)
|
assert.Equal(t, from, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToTimeDurationFunc_ShouldParse_FromZero(t *testing.T) {
|
||||||
|
hook := ToTimeDurationFunc()
|
||||||
|
|
||||||
|
var (
|
||||||
|
from = 0
|
||||||
|
expected = time.Duration(0)
|
||||||
|
|
||||||
|
to time.Duration
|
||||||
|
ptrTo *time.Duration
|
||||||
|
result interface{}
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
result, err = hook(reflect.TypeOf(from), reflect.TypeOf(to), from)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, result)
|
||||||
|
|
||||||
|
result, err = hook(reflect.TypeOf(from), reflect.TypeOf(ptrTo), from)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, &expected, result)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user