Skip to content

Commit

Permalink
Trim whitespace in X-Matrix header (#436)
Browse files Browse the repository at this point in the history
Fixes #434
  • Loading branch information
S7evinK authored Sep 10, 2024
1 parent d531860 commit f80a3be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
55 changes: 55 additions & 0 deletions fclient/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit f80a3be

Please sign in to comment.