More server_name validation

We need to do a bit more validation when we get a server name, but don't want
to be re-doing it all over the shop, so factor out a separate
parse_and_validate_server_name, and do the extra validation.

Also, use it to verify the server name in the config file.
This commit is contained in:
Richard van der Hoff 2018-07-04 18:15:03 +01:00
parent 13f7adf84b
commit 546bc9e28b
5 changed files with 68 additions and 13 deletions

View file

@ -12,7 +12,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from synapse.http.endpoint import parse_server_name
from synapse.http.endpoint import (
parse_server_name,
parse_and_validate_server_name,
)
from tests import unittest
@ -30,17 +33,23 @@ class ServerNameTestCase(unittest.TestCase):
for i, o in test_data.items():
self.assertEqual(parse_server_name(i), o)
def test_parse_bad_server_names(self):
def test_validate_bad_server_names(self):
test_data = [
"", # empty
"localhost:http", # non-numeric port
"1234]", # smells like ipv6 literal but isn't
"[1234",
"underscore_.com",
"percent%65.com",
"1234:5678:80", # too many colons
]
for i in test_data:
try:
parse_server_name(i)
parse_and_validate_server_name(i)
self.fail(
"Expected parse_server_name(\"%s\") to throw" % i,
"Expected parse_and_validate_server_name('%s') to throw" % (
i,
),
)
except ValueError:
pass