Error if a .furl config entry contains an unescaped '#'. fixes #2128
authorDaira Hopwood <daira@jacaranda.org>
Mon, 5 May 2014 21:55:50 +0000 (22:55 +0100)
committerDaira Hopwood <daira@jacaranda.org>
Mon, 5 May 2014 21:55:50 +0000 (22:55 +0100)
Author: Andrew Miller <amiller@dappervision.com>
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
src/allmydata/node.py
src/allmydata/test/test_client.py
src/allmydata/test/test_node.py

index 8873e5c798232ab1d009de76e05b5436b3803d25..47c8ac3bac4dd07af92a054447396f8a0ea8131e 100644 (file)
@@ -55,6 +55,11 @@ class OldConfigError(Exception):
 class OldConfigOptionError(Exception):
     pass
 
+class UnescapedHashError(Exception):
+    def __str__(self):
+        return ("The configuration entry %s contained an unescaped '#' character."
+                % quote_output(self.args[0]))
+
 
 class Node(service.MultiService):
     # this implements common functionality of both Client nodes and Introducer
@@ -101,11 +106,27 @@ class Node(service.MultiService):
         test_name = tempfile.mktemp()
         _assert(os.path.dirname(test_name) == tempdir, test_name, tempdir)
 
+    @staticmethod
+    def _contains_unescaped_hash(item):
+        characters = iter(item)
+        for c in characters:
+            if c == '\\':
+                characters.next()
+            elif c == '#':
+                return True
+
+        return False
+
     def get_config(self, section, option, default=_None, boolean=False):
         try:
             if boolean:
                 return self.config.getboolean(section, option)
-            return self.config.get(section, option)
+
+            item = self.config.get(section, option)
+            if option.endswith(".furl") and self._contains_unescaped_hash(item):
+                raise UnescapedHashError(item)
+
+            return item
         except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
             if default is _None:
                 fn = os.path.join(self.basedir, u"tahoe.cfg")
index 796971b236140ed73e3872212ce90a72eb8c8e22..df3691f33cbf09105c6c05ae6efbb7c528be5172 100644 (file)
@@ -3,7 +3,7 @@ from twisted.trial import unittest
 from twisted.application import service
 
 import allmydata
-from allmydata.node import OldConfigError, OldConfigOptionError, MissingConfigEntry
+from allmydata.node import Node, OldConfigError, OldConfigOptionError, MissingConfigEntry, UnescapedHashError
 from allmydata import client
 from allmydata.storage_client import StorageFarmBroker
 from allmydata.util import base32, fileutil
@@ -30,6 +30,31 @@ class Basic(testutil.ReallyEqualMixin, unittest.TestCase):
                            BASECONFIG)
         client.Client(basedir)
 
+    def test_comment(self):
+        dummy = "pb://wl74cyahejagspqgy4x5ukrvfnevlknt@127.0.0.1:58889/bogus"
+
+        should_fail = [r"test#test", r"#testtest", r"test\\#test"]
+        should_not_fail = [r"test\#test", r"test\\\#test", r"testtest"]
+
+        basedir = "test_client.Basic.test_comment"
+        os.mkdir(basedir)
+
+        def write_config(shouldfail, s):
+            config = ("[client]\n"
+                      "introducer.furl = %s\n" % s)
+            fileutil.write(os.path.join(basedir, "tahoe.cfg"), config)
+
+        for s in should_fail:
+            self.failUnless(Node._contains_unescaped_hash(s))
+            write_config(s)
+            self.failUnlessRaises(UnescapedHashError, client.Client, basedir)
+
+        for s in should_not_fail:
+            self.failIf(Node._contains_unescaped_hash(s))
+            write_config(s)
+            client.Client(basedir)
+
+
     @mock.patch('twisted.python.log.msg')
     def test_error_on_old_config_files(self, mock_log_msg):
         basedir = "test_client.Basic.test_error_on_old_config_files"
index 72d6ef8ce00c6956c8127a98f1cdcb18565a8455..88303f2a63c319c70c97fc8a353c880d1f6a085a 100644 (file)
@@ -87,6 +87,17 @@ class TestCase(testutil.SignalMixin, unittest.TestCase):
                                                        u"\u2621"))
         return d
 
+    def test_tahoe_cfg_hash_in_name(self):
+        basedir = "test_node/test_cfg_hash_in_name"
+        nickname = "Hash#Bang!" # a clever nickname containing a hash
+        fileutil.make_dirs(basedir)
+        f = open(os.path.join(basedir, 'tahoe.cfg'), 'wt')
+        f.write("[node]\n")
+        f.write("nickname = %s\n" % (nickname,))
+        f.close()
+        n = TestNode(basedir)
+        self.failUnless(n.nickname == nickname)
+
     def test_private_config(self):
         basedir = "test_node/test_private_config"
         privdir = os.path.join(basedir, "private")