work around missing timezone in timestamps in tls reports from microsoft

This commit is contained in:
Mechiel Lukkien
2023-02-05 10:55:34 +01:00
parent ffb2a10a4e
commit 49dd5b7ba9
3 changed files with 167 additions and 0 deletions

View File

@ -32,6 +32,47 @@ type TLSRPTDateRange struct {
End time.Time `json:"end-datetime"`
}
// UnmarshalJSON is defined on the date range, not the individual time.Time fields
// because it is easier to keep the unmodified time.Time fields stored in the
// database.
func (dr *TLSRPTDateRange) UnmarshalJSON(buf []byte) error {
var v struct {
Start xtime `json:"start-datetime"`
End xtime `json:"end-datetime"`
}
if err := json.Unmarshal(buf, &v); err != nil {
return err
}
dr.Start = time.Time(v.Start)
dr.End = time.Time(v.End)
return nil
}
// xtime and its UnmarshalJSON exists to work around a specific invalid date-time encoding seen in the wild.
type xtime time.Time
func (x *xtime) UnmarshalJSON(buf []byte) error {
var t time.Time
err := t.UnmarshalJSON(buf)
if err == nil {
*x = xtime(t)
return nil
}
// Microsoft is sending reports with invalid start-datetime/end-datetime (missing
// timezone, ../rfc/8460:682 ../rfc/3339:415). We compensate.
var s string
if err := json.Unmarshal(buf, &s); err != nil {
return err
}
t, err = time.Parse("2006-01-02T15:04:05", s)
if err != nil {
return err
}
*x = xtime(t)
return nil
}
type Result struct {
Policy ResultPolicy `json:"policy"`
Summary Summary `json:"summary"`