[tor-commits] [sbws/master] stem: parse torrc options that are only a key

juga at torproject.org juga at torproject.org
Wed Jan 9 17:34:46 UTC 2019


commit fcca7fafc3503b1a0796dd74d412658a89f94653
Author: juga0 <juga at riseup.net>
Date:   Fri Dec 7 09:37:20 2018 +0000

    stem: parse torrc options that are only a key
    
    User torrc options in `extra_lines` that are only one key and not
    a key value pair were not being parsed correctly.
    Add a test.
    
    Fixes bug #28715. Bugfix v0.1.1
---
 sbws/util/stem.py            | 16 ++++++++--------
 tests/unit/util/test_stem.py |  8 ++++++++
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/sbws/util/stem.py b/sbws/util/stem.py
index aa4fa69..d8e5cf8 100644
--- a/sbws/util/stem.py
+++ b/sbws/util/stem.py
@@ -134,15 +134,13 @@ def parse_user_torrc_config(torrc, torrc_text):
         # Ignore blank lines
         if len(line) < 1:
             continue
-        # The way stem handles configuring Tor with a dictionary is the first
-        # word is a key and the remaining words are the value.
+        # Some torrc options are only a key, some are a key value pair.
         kv = line.split(None, 1)
-        if len(kv) < 2:
-            fail_hard('All torrc lines must have 2 or more words. "%s" has '
-                      'fewer', line)
-        key, value = kv
-        log.debug('Adding "%s %s" to torrc with which we are launching Tor',
-                  key, value)
+        if len(kv) > 1:
+            key, value = kv
+        else:
+            key = kv[0]
+            value = None
         # It's really easy to add to the torrc if the key doesn't exist
         if key not in torrc:
             torrc_dict.update({key: value})
@@ -160,6 +158,8 @@ def parse_user_torrc_config(torrc, torrc_text):
                 assert isinstance(existing_val, list)
                 existing_val.append(value)
                 torrc_dict.update({key: existing_val})
+        log.debug('Adding "%s %s" to torrc with which we are launching Tor',
+                  key, value)
     return torrc_dict
 
 
diff --git a/tests/unit/util/test_stem.py b/tests/unit/util/test_stem.py
index 8edf789..c2aafe9 100644
--- a/tests/unit/util/test_stem.py
+++ b/tests/unit/util/test_stem.py
@@ -24,3 +24,11 @@ def test_parse_user_torrc_config_existing_keyvalue_options_fail(caplog):
     # the existing value and the new value
     assert torrc_dict_new != torrc_dict
     assert torrc_dict_new == {'SocksPort': ['auto', '9050']}
+
+
+def test_parse_user_torrc_config_new_key_option_success():
+    config_torrc_extra_lines = """
+    LongLivedPorts
+    """
+    torrc_dict = parse_user_torrc_config({}, config_torrc_extra_lines)
+    assert torrc_dict == {'LongLivedPorts': None}



More information about the tor-commits mailing list