Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional whitespace support in Authorization (#1350) #17145

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/17145.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for optional whitespace around the Federation API's `Authorization` header's parameter commas.
6 changes: 5 additions & 1 deletion synapse/federation/transport/server/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ def _parse_auth_header(header_bytes: bytes) -> Tuple[str, str, str, Optional[str
"""
try:
header_str = header_bytes.decode("utf-8")
params = re.split(" +", header_str)[1].split(",")
space_or_tab = "[ \t]"
params = re.split(
rf"{space_or_tab}*,{space_or_tab}*",
re.split(r"^X-Matrix +", header_str, maxsplit=1)[1],
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
)
param_dict: Dict[str, str] = {
k.lower(): v for k, v in [param.split("=", maxsplit=1) for param in params]
}
Expand Down
7 changes: 7 additions & 0 deletions tests/federation/transport/server/test__base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,10 @@ def test_authorization_header(self) -> None:
),
("foo", "ed25519:1", "sig", "bar"),
)
# test that "optional whitespace(s)" (space and tabulation) are allowed between comma-separated auth-param components
self.assertEqual(
_parse_auth_header(
b'X-Matrix origin=foo , key="ed25519:1", sig="sig", destination="bar", extra_field=ignored'
),
("foo", "ed25519:1", "sig", "bar"),
)
Loading