Repository navigation
Fix client_secret param validator - #20104
Merged
Merged
Conversation
The Matrix spec requires `client_secret` request parameters to consist entirely of the characters `[0-9a-zA-Z.=_-]` (non-empty, at most 255 characters), as enforced by `assert_valid_client_secret` / `CLIENT_SECRET_REGEX = r"^[0-9a-zA-Z\.=_\-]+$"` in synapse/util/stringutils.py. That validation was originally added for matrix-org/synapse#6766. The Pydantic `ClientSecretStr` type in synapse/types/rest/client/__init__.py lost the regex anchors in the Pydantic port (element-hq#13188) and was further weakened in the Pydantic v2 migration (element-hq#19071): its pattern is now the unanchored, unquantified character class `[0-9a-zA-Z.=_-]`. Pydantic applies patterns with *search* semantics, so any string containing at least one allowed character passes — e.g. "foo bar", "secret!", "café" and "a'; DROP TABLE users;--" are all accepted. This is a regression of the fix for matrix-org/synapse#6766. This commit adds tests only: - test_rejects_client_secrets_with_invalid_characters FAILS at this commit, demonstrating the regression. - The empty-string and over-255-character rejection tests already pass, since min_length/max_length constraints were unaffected.
…ment Anchor and quantify the pattern (`^[0-9a-zA-Z.=_-]+$`) so that `client_secret` values must consist entirely of the spec-mandated character set, matching `assert_valid_client_secret`. Pydantic applies patterns with search semantics, so the previous unanchored single character class accepted any string containing at least one valid character. Regression of the fix for matrix-org/synapse#6766.
barodeur
marked this pull request as ready for review
August 14, 2026 13:24
sandhose
reviewed
Aug 14, 2026
Update test example with a nice reference to https://xkcd.com/327/ Co-authored-by: Quentin Gliech <quenting@element.io>
sandhose
approved these changes
Aug 14, 2026
82 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The validation of
client_secretparams accepts invalid values: any value that includes a char in[0-9a-zA-Z.=_-]is accepted, for example"café"is accepted.Instead synapse should only accept if all chars are within
[0-9a-zA-Z.=_-], not just one.This is the current validator:
Unfortunately, Pydantic only defines the
patternargument as:Which is extremely imprecise.
Here is a little script to verify the behavior:
which would output
History
This is a regression of a previously reported and fixed bug:
client_secretregex at all — with real-world fallout: FluffyChat had started sending secrets containing:because nothing rejected them. Fixed by introducingassert_valid_client_secret(Validate client_secret parameter matrix-org/synapse#6767).synapse.rest.client.account. matrix-org/synapse#13188 (Synapse 1.66.0) ported the account endpoints to Pydantic and transcribed the regex without anchors/quantifier; Pydantic v1'sre.matchsemantics meant only the first character was validated.Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.