Skip to content

Commit 9391a71

Browse files
committed
Reject watch request with -1 revision and make rangeEvents safe against negative revision
Signed-off-by: Marek Siarkowicz <[email protected]>
1 parent b7420c5 commit 9391a71

File tree

4 files changed

+150
-54
lines changed

4 files changed

+150
-54
lines changed

server/etcdserver/api/v3rpc/watch.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ func (sws *serverWatchStream) recvLoop() error {
272272
// support >= key queries
273273
creq.RangeEnd = []byte{}
274274
}
275+
if creq.StartRevision < 0 {
276+
wr := &pb.WatchResponse{
277+
Header: sws.newResponseHeader(sws.watchStream.Rev()),
278+
WatchId: clientv3.InvalidWatchID,
279+
Canceled: true,
280+
Created: true,
281+
CancelReason: rpctypes.ErrCompacted.Error(),
282+
}
283+
284+
select {
285+
case sws.ctrlStream <- wr:
286+
continue
287+
case <-sws.closec:
288+
return nil
289+
}
290+
}
275291

276292
err := sws.isWatchPermitted(creq)
277293
if err != nil {

server/storage/mvcc/watchable_store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ func rangeEventsWithReuse(lg *zap.Logger, b backend.Backend, evs []mvccpb.Event,
447447

448448
// rangeEvents returns events in range [minRev, maxRev).
449449
func rangeEvents(lg *zap.Logger, b backend.Backend, minRev, maxRev int64) []mvccpb.Event {
450+
if minRev < 0 {
451+
minRev = 0
452+
}
450453
minBytes, maxBytes := NewRevBytes(), NewRevBytes()
451454
minBytes = RevToBytes(Revision{Main: minRev}, minBytes)
452455
maxBytes = RevToBytes(Revision{Main: maxRev}, maxBytes)

server/storage/mvcc/watchable_store_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,14 @@ func TestRangeEvents(t *testing.T) {
405405
expectEvents []mvccpb.Event
406406
}{
407407
// maxRev, top to bottom
408-
{minRev: 2, maxRev: 6, expectEvents: expectEvents[0:5]},
409-
{minRev: 2, maxRev: 5, expectEvents: expectEvents[0:3]},
410-
{minRev: 2, maxRev: 4, expectEvents: expectEvents[0:2]},
411-
{minRev: 2, maxRev: 3, expectEvents: expectEvents[0:1]},
412-
{minRev: 2, maxRev: 2, expectEvents: expectEvents[0:0]},
408+
{minRev: -1, maxRev: 6, expectEvents: expectEvents[0:5]},
409+
{minRev: -1, maxRev: 5, expectEvents: expectEvents[0:3]},
410+
{minRev: -1, maxRev: 4, expectEvents: expectEvents[0:2]},
411+
{minRev: -1, maxRev: 3, expectEvents: expectEvents[0:1]},
412+
{minRev: -1, maxRev: 2, expectEvents: expectEvents[0:0]},
413413

414414
// minRev, bottom to top
415+
{minRev: -1, maxRev: 6, expectEvents: expectEvents[0:5]},
415416
{minRev: 2, maxRev: 6, expectEvents: expectEvents[0:5]},
416417
{minRev: 3, maxRev: 6, expectEvents: expectEvents[1:5]},
417418
{minRev: 4, maxRev: 6, expectEvents: expectEvents[2:5]},

0 commit comments

Comments
 (0)