[tor-commits] [stem/master] Resume size check for circuit responses

atagar at torproject.org atagar at torproject.org
Sun Aug 26 20:49:21 UTC 2018


commit 2e51a01c1fa96f24f44b744d37915ef2ffd7762d
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Aug 25 12:39:44 2018 -0700

    Resume size check for circuit responses
    
    Maybe this check is redundant with the cell module additions? Not sure.
    Regardless, unlike the hamhanded 'relay_cell_size' calculation I did prior
    to Dave's branch our LinkProtocol's 'fixed_cell_length' now makes this
    check trivial to do upfront.
---
 stem/client/__init__.py | 10 +++++++---
 stem/client/datatype.py |  5 ++++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/stem/client/__init__.py b/stem/client/__init__.py
index 46a82428..a216696a 100644
--- a/stem/client/__init__.py
+++ b/stem/client/__init__.py
@@ -243,16 +243,20 @@ class Circuit(object):
       self.forward_digest = forward_digest
       self.forward_key = forward_key
 
+      # Decrypt relay cells received in response. Again, our digest/key only
+      # updates when handled successfully.
+
       reply = self.relay._orport.recv()
       reply_cells = []
 
-      relay_cell_cmd = stem.client.cell.RelayCell.VALUE
+      if len(reply) % self.relay.link_protocol.fixed_cell_length != 0:
+        raise stem.ProtocolError('Circuit response should be a series of RELAY cells, but received an unexpected size for a response: %i' % len(reply))
 
       while reply:
         raw_cell, reply = stem.client.cell.Cell.pop(reply, self.relay.link_protocol)
 
-        if raw_cell.VALUE != relay_cell_cmd:
-          raise stem.ProtocolError('RELAY cell responses should be %i but was %i' % (relay_cell_cmd, raw_cell.VALUE))
+        if raw_cell.VALUE != stem.client.cell.RelayCell.VALUE:
+          raise stem.ProtocolError('RELAY cell responses should be %i but was %i' % (stem.client.cell.RelayCell.VALUE, raw_cell.VALUE))
         elif raw_cell.circ_id != self.id:
           raise stem.ProtocolError('Response should be for circuit id %i, not %i' % (self.id, raw_cell.circ_id))
 
diff --git a/stem/client/datatype.py b/stem/client/datatype.py
index 43bdfe9d..644d4993 100644
--- a/stem/client/datatype.py
+++ b/stem/client/datatype.py
@@ -116,6 +116,7 @@ import collections
 import hashlib
 import struct
 
+import stem.client.cell
 import stem.prereq
 import stem.util
 import stem.util.connection
@@ -246,9 +247,11 @@ class LinkProtocol(int):
     protocol = int.__new__(cls, version)
     protocol.version = version
     protocol.circ_id_size = Size.LONG if version > 3 else Size.SHORT
-    protocol.fixed_cell_length = 514 if version > 3 else 512
     protocol.first_circ_id = 0x80000000 if version > 3 else 0x01
 
+    cell_header_size = protocol.circ_id_size.size + 1  # circuit id (2 or 4 bytes) + command (1 byte)
+    protocol.fixed_cell_length = cell_header_size + stem.client.cell.FIXED_PAYLOAD_LEN
+
     return protocol
 
   def __hash__(self):





More information about the tor-commits mailing list