From f80a3be1daaeafdb3b96849332d43a6b9adfd30c Mon Sep 17 00:00:00 2001 From: Till <2353100+S7evinK@users.noreply.github.com> Date: Tue, 10 Sep 2024 21:04:01 +0200 Subject: [PATCH] Trim whitespace in X-Matrix header (#436) Fixes #434 --- fclient/request.go | 4 +-- fclient/request_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/fclient/request.go b/fclient/request.go index 982a87dc..188a47bb 100644 --- a/fclient/request.go +++ b/fclient/request.go @@ -332,8 +332,8 @@ func ParseAuthorization(header string) (scheme string, origin, destination spec. if len(pair) != 2 { continue } - name := pair[0] - value := strings.Trim(pair[1], "\"") + name := strings.TrimSpace(pair[0]) + value := strings.Trim(strings.TrimSpace(pair[1]), "\"") if name == "origin" { origin = spec.ServerName(value) } diff --git a/fclient/request_test.go b/fclient/request_test.go index 6411545b..571eac5d 100644 --- a/fclient/request_test.go +++ b/fclient/request_test.go @@ -186,3 +186,58 @@ func mustLoadPrivateKey(seed string) ed25519.PrivateKey { } return privateKey } + +func TestParseAuthorization(t *testing.T) { + wantScheme := "X-Matrix" + wantOrigin := spec.ServerName("foo") + wantKey := gomatrixserverlib.KeyID("ed25519:1") + wantSig := "sig" + wantDestination := spec.ServerName("bar") + + tests := []struct { + name string + header string + }{ + { + name: "parse with whitespace", + header: `X-Matrix origin=foo , key="ed25519:1", sig="sig", destination="bar"`, + }, + { + name: "parse without spaces", + header: `X-Matrix origin=foo,key="ed25519:1",sig="sig",destination="bar"`, + }, + { + name: "parse with tabs spaces", + header: `X-Matrix origin=foo , key="ed25519:1", sig="sig" ,destination ="bar"`, + }, + { + name: "parse with different ordering and tabs", + header: `X-Matrix origin=foo , ,destination ="bar", sig="sig", key="ed25519:1"`, + }, + { + name: "parse with different ordering and whitespace around values", + header: `X-Matrix origin=foo , ,destination = "bar" , sig= "sig" , key="ed25519:1"`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotScheme, gotOrigin, gotDestination, gotKey, gotSig := ParseAuthorization(tt.header) + + if gotScheme != wantScheme { + t.Errorf("ParseAuthorization() gotScheme = %v, want %v", gotScheme, wantScheme) + } + if gotOrigin != wantOrigin { + t.Errorf("ParseAuthorization() gotOrigin = %v, want %v", gotOrigin, wantOrigin) + } + if gotDestination != wantDestination { + t.Errorf("ParseAuthorization() gotDestination = %v, want %v", gotDestination, wantDestination) + } + if gotKey != wantKey { + t.Errorf("ParseAuthorization() gotKey = %v, want %v", gotKey, wantKey) + } + if gotSig != wantSig { + t.Errorf("ParseAuthorization() gotSig = %v, want %v", gotSig, wantSig) + } + }) + } +}