Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Validate client_secret parameter (#6767)
Browse files Browse the repository at this point in the history
* commit '9f7aaf90b':
  Validate client_secret parameter (#6767)
  • Loading branch information
anoadragon453 committed Mar 23, 2020
2 parents 5a32d29 + 9f7aaf9 commit 8632f34
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog.d/6767.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Validate `client_secret` parameter using the regex provided by the Client-Server API, temporarily allowing `:` characters for older clients. The `:` character will be removed in a future release.
4 changes: 3 additions & 1 deletion synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.http.client import SimpleHttpClient
from synapse.util.hash import sha256_and_url_safe_base64
from synapse.util.stringutils import random_string
from synapse.util.stringutils import assert_valid_client_secret, random_string

from ._base import BaseHandler

Expand Down Expand Up @@ -92,6 +92,8 @@ def threepid_from_creds(self, id_server, creds):
raise SynapseError(
400, "Missing param client_secret in creds", errcode=Codes.MISSING_PARAM
)
assert_valid_client_secret(client_secret)

session_id = creds.get("sid")
if not session_id:
raise SynapseError(
Expand Down
21 changes: 14 additions & 7 deletions synapse/rest/client/v2_alpha/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ async def on_GET(self, request, medium):
)

sid = parse_string(request, "sid", required=True)
token = parse_string(request, "token", required=True)
client_secret = parse_string(request, "client_secret", required=True)

assert_valid_client_secret(client_secret)

token = parse_string(request, "token", required=True)

# Attempt to validate a 3PID session
try:
# Mark the session as valid
Expand Down Expand Up @@ -386,6 +384,8 @@ async def on_POST(self, request):
body = parse_json_object_from_request(request)
assert_params_in_dict(body, ["client_secret", "email", "send_attempt"])
client_secret = body["client_secret"]
assert_valid_client_secret(client_secret)

email = body["email"]
send_attempt = body["send_attempt"]
next_link = body.get("next_link") # Optional param
Expand Down Expand Up @@ -448,6 +448,8 @@ async def on_POST(self, request):
body, ["client_secret", "country", "phone_number", "send_attempt"]
)
client_secret = body["client_secret"]
assert_valid_client_secret(client_secret)

country = body["country"]
phone_number = body["phone_number"]
send_attempt = body["send_attempt"]
Expand Down Expand Up @@ -530,8 +532,9 @@ async def on_GET(self, request):
)

sid = parse_string(request, "sid", required=True)
client_secret = parse_string(request, "client_secret", required=True)
token = parse_string(request, "token", required=True)
client_secret = parse_string(request, "client_secret", required=True)
assert_valid_client_secret(client_secret)

# Attempt to validate a 3PID session
try:
Expand Down Expand Up @@ -596,6 +599,7 @@ async def on_POST(self, request):

body = parse_json_object_from_request(request)
assert_params_in_dict(body, ["client_secret", "sid", "token"])
assert_valid_client_secret(body["client_secret"])

# Proxy submit_token request to msisdn threepid delegate
response = await self.identity_handler.proxy_msisdn_submit_token(
Expand Down Expand Up @@ -661,8 +665,9 @@ async def on_POST(self, request):
)
assert_params_in_dict(threepid_creds, ["client_secret", "sid"])

client_secret = threepid_creds["client_secret"]
sid = threepid_creds["sid"]
client_secret = threepid_creds["client_secret"]
assert_valid_client_secret(client_secret)

validation_session = await self.identity_handler.validate_threepid_session(
client_secret, sid
Expand Down Expand Up @@ -722,8 +727,9 @@ async def on_POST(self, request):
body = parse_json_object_from_request(request)

assert_params_in_dict(body, ["client_secret", "sid"])
client_secret = body["client_secret"]
sid = body["sid"]
client_secret = body["client_secret"]
assert_valid_client_secret(client_secret)

await self.auth_handler.validate_user_via_ui_auth(
requester, body, self.hs.get_ip_from_request(request)
Expand Down Expand Up @@ -771,8 +777,9 @@ async def on_POST(self, request):
assert_params_in_dict(body, ["id_server", "sid", "client_secret"])
id_server = body["id_server"]
sid = body["sid"]
client_secret = body["client_secret"]
id_access_token = body.get("id_access_token") # optional
client_secret = body["client_secret"]
assert_valid_client_secret(client_secret)

requester = await self.auth.get_user_by_req(request)
user_id = requester.user.to_string()
Expand Down
2 changes: 2 additions & 0 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ async def on_POST(self, request):

# Extract params from body
client_secret = body["client_secret"]
assert_valid_client_secret(client_secret)

email = body["email"]
send_attempt = body["send_attempt"]
next_link = body.get("next_link") # Optional param
Expand Down
7 changes: 5 additions & 2 deletions synapse/util/stringutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@

_string_with_symbols = string.digits + string.ascii_letters + ".,;:^&*-_+=#~@"

# https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-register-email-requesttoken
# Note: The : character is allowed here for older clients, but will be removed in a
# future release. Context: https://github.com/matrix-org/synapse/issues/6766
client_secret_regex = re.compile(r"^[0-9a-zA-Z\.\=\_\-\:]+$")

# random_string and random_string_with_symbols are used for a range of things,
# some cryptographically important, some less so. We use SystemRandom to make sure
# we get cryptographically-secure randoms.
rand = random.SystemRandom()

client_secret_regex = re.compile(r"^[0-9a-zA-Z.=_-]+$")


def random_string(length):
return "".join(rand.choice(string.ascii_letters) for _ in range(length))
Expand Down
51 changes: 51 additions & 0 deletions tests/util/test_stringutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
# Copyright 2020 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.api.errors import SynapseError
from synapse.util.stringutils import assert_valid_client_secret

from .. import unittest


class StringUtilsTestCase(unittest.TestCase):
def test_client_secret_regex(self):
"""Ensure that client_secret does not contain illegal characters"""
good = [
"abcde12345",
"ABCabc123",
"_--something==_",
"...--==-18913",
"8Dj2odd-e9asd.cd==_--ddas-secret-",
# We temporarily allow : characters: https://github.com/matrix-org/synapse/issues/6766
# To be removed in a future release
"SECRET:1234567890",
]

bad = [
"--+-/secret",
"\\dx--dsa288",
"",
"AAS//",
"asdj**",
">X><Z<!!-)))",
"a@b.com",
]

for client_secret in good:
assert_valid_client_secret(client_secret)

for client_secret in bad:
with self.assertRaises(SynapseError):
assert_valid_client_secret(client_secret)

0 comments on commit 8632f34

Please sign in to comment.