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

Commit

Permalink
Pydantic for add_threepid endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Sep 1, 2022
1 parent c4be7f3 commit 5f2e43b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion changelog.d/13188.feature
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Improve validation of request bodies for the following client-server API endpoints: [`/account/3pid/msisdn/requestToken`](https://spec.matrix.org/v1.3/client-server-api/#post_matrixclientv3account3pidmsisdnrequesttoken).
Improve validation of request bodies for the following client-server API endpoints: [`/account/3pid/msisdn/requestToken`](https://spec.matrix.org/v1.3/client-server-api/#post_matrixclientv3account3pidmsisdnrequesttoken). Also improve validation for Synapse-internal `/add_threepid` endpoints.
23 changes: 11 additions & 12 deletions synapse/rest/client/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
assert_params_in_dict,
parse_and_validate_json_object_from_request,
parse_json_object_from_request,
parse_string,
)
from synapse.http.site import SynapseRequest
from synapse.metrics import threepid_send_requests
Expand All @@ -45,6 +44,7 @@
AuthenticationData,
EmailRequestTokenBody,
MsisdnRequestTokenBody,
AddThreepidSubmitTokenBody,
)
from synapse.rest.models import RequestBodyModel
from synapse.types import JsonDict
Expand Down Expand Up @@ -491,16 +491,15 @@ async def on_GET(self, request: Request) -> None:
400, "Adding an email to your account is disabled on this server"
)

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)
body = parse_and_validate_json_object_from_request(
request, AddThreepidSubmitTokenBody
)

# Attempt to validate a 3PID session
try:
# Mark the session as valid
next_link = await self.store.validate_threepid_session(
sid, client_secret, token, self.clock.time_msec()
body.sid, body.client_secret, body.token, self.clock.time_msec()
)

# Perform a 302 redirect if next_link is set
Expand Down Expand Up @@ -547,16 +546,16 @@ async def on_POST(self, request: Request) -> Tuple[int, JsonDict]:
"instead.",
)

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

# Proxy submit_token request to msisdn threepid delegate
response = await self.identity_handler.proxy_msisdn_submit_token(
self.config.registration.account_threepid_delegate_msisdn,
body["client_secret"],
body["sid"],
body["token"],
body.client_secret,
body.sid,
body.token,
)
return 200, response

Expand Down
30 changes: 19 additions & 11 deletions synapse/rest/client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ class Config:
type: Optional[StrictStr] = None


class ThreePidRequestTokenBody(RequestBodyModel):
if TYPE_CHECKING:
client_secret: StrictStr
else:
# See also assert_valid_client_secret()
client_secret: constr(
regex="[0-9a-zA-Z.=_-]", # noqa: F722
min_length=0,
max_length=255,
strict=True,
)
if TYPE_CHECKING:
ClientSecretType = StrictStr
else:
# See also assert_valid_client_secret()
ClientSecretType = constr(
regex="[0-9a-zA-Z.=_-]",
min_length=0,
max_length=255,
strict=True,
)


class ThreePidRequestTokenBody(RequestBodyModel):
client_secret: ClientSecretType
id_server: Optional[StrictStr]
id_access_token: Optional[StrictStr]
next_link: Optional[StrictStr]
Expand Down Expand Up @@ -77,3 +79,9 @@ class MsisdnRequestTokenBody(ThreePidRequestTokenBody):
# Two-letter uppercase ISO-3166-1-alpha-2
country: StrictStr
phone_number: StrictStr


class AddThreepidSubmitTokenBody(RequestBodyModel):
sid: StrictStr
token: StrictStr
client_secret: ClientSecretType

0 comments on commit 5f2e43b

Please sign in to comment.