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

Trim whitespace in X-Matrix header #436

Merged
merged 2 commits into from
Sep 10, 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
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)
}
})
}
}
Loading