From 059887bd7b5cb2b3c8bd78c33544154aba9a004f Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 13 Jun 2019 17:39:24 -0700 Subject: [PATCH] watchtower/wtdb: apply sanity checks to session policy before accepting --- watchtower/wtdb/tower_db.go | 6 ++++++ watchtower/wtdb/tower_db_test.go | 7 +++++++ watchtower/wtmock/tower_db.go | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/watchtower/wtdb/tower_db.go b/watchtower/wtdb/tower_db.go index 8610f04d..92a9e55a 100644 --- a/watchtower/wtdb/tower_db.go +++ b/watchtower/wtdb/tower_db.go @@ -193,6 +193,12 @@ func (t *TowerDB) InsertSessionInfo(session *SessionInfo) error { return ErrSessionAlreadyExists } + // Perform a quick sanity check on the session policy before + // accepting. + if err := session.Policy.Validate(); err != nil { + return err + } + err = putSession(sessions, session) if err != nil { return err diff --git a/watchtower/wtdb/tower_db_test.go b/watchtower/wtdb/tower_db_test.go index 2eb03a22..c408e7ef 100644 --- a/watchtower/wtdb/tower_db_test.go +++ b/watchtower/wtdb/tower_db_test.go @@ -150,6 +150,13 @@ func testInsertSession(h *towerDBHarness) { RewardAddress: []byte{0x01, 0x02, 0x03}, } + // Try to insert the session, which should fail since the policy doesn't + // meet the current sanity checks. + h.insertSession(session, wtpolicy.ErrSweepFeeRateTooLow) + + // Now assign a sane sweep fee rate to the policy, inserting should + // succeed. + session.Policy.SweepFeeRate = wtpolicy.DefaultSweepFeeRate h.insertSession(session, nil) session2 := h.getSession(&id, nil) diff --git a/watchtower/wtmock/tower_db.go b/watchtower/wtmock/tower_db.go index 1c7b94c9..35c01ab1 100644 --- a/watchtower/wtmock/tower_db.go +++ b/watchtower/wtmock/tower_db.go @@ -81,6 +81,11 @@ func (db *TowerDB) InsertSessionInfo(info *wtdb.SessionInfo) error { return wtdb.ErrSessionAlreadyExists } + // Perform a quick sanity check on the session policy before accepting. + if err := info.Policy.Validate(); err != nil { + return err + } + db.sessions[info.ID] = info return nil