Skip to content

Commit

Permalink
add protection against bad timestamps in senml packs
Browse files Browse the repository at this point in the history
  • Loading branch information
istyf committed Jun 26, 2024
1 parent e868f48 commit 9a181c1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/pkg/application/functions/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ func TestAddToHistory(t *testing.T) {

f, _ := reg.Find(ctx, MatchSensor(sensorId))

_ = f[0].Handle(ctx, newMessageAccepted(1.67, time.Now().Add(1*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(2.67, time.Now().Add(2*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(3.67, time.Now().Add(3*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(4.67, time.Now().Add(4*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(5.67, time.Now().Add(5*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(6.67, time.Now().Add(6*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(1.67, time.Now().Add(-6*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(2.67, time.Now().Add(-5*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(3.67, time.Now().Add(-4*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(4.67, time.Now().Add(-3*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(5.67, time.Now().Add(-2*time.Hour)), msgctx)
_ = f[0].Handle(ctx, newMessageAccepted(6.67, time.Now().Add(-1*time.Hour)), msgctx)

h, _ := f[0].History(ctx, "", 0)
is.Equal(6, len(h))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package waterqualities

import (
"context"
"fmt"
"math"
"time"

Expand Down Expand Up @@ -37,6 +38,10 @@ func (wq *waterquality) Handle(ctx context.Context, e *events.MessageAccepted, o
r, tempOk := e.Pack.GetRecord(senml.FindByName(SensorValue))
ts, timeOk := e.Pack.GetTime(senml.FindByName(SensorValue))

if ts.After(time.Now().Add(5 * time.Second)) {
return false, fmt.Errorf("invalid timestamp %s in waterquality pack: %w", ts.Format(time.RFC3339), events.ErrBadTimestamp)
}

if tempOk && timeOk && r.Value != nil && ts.After(wq.Timestamp) {
temp := math.Round(*r.Value*10) / 10
oldTemp := wq.Temperature
Expand Down
1 change: 1 addition & 0 deletions pkg/messaging/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (m MessageAccepted) Error() error {
return nil
}

var ErrBadTimestamp = fmt.Errorf("bad timestamp")
var ErrNoMatch = fmt.Errorf("event mismatch")

func Matches(m MessageAccepted, objectURN string) bool {
Expand Down

0 comments on commit 9a181c1

Please sign in to comment.