Skip to content

Commit f80a3be

Browse files
authored
Trim whitespace in X-Matrix header (#436)
Fixes #434
1 parent d531860 commit f80a3be

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

fclient/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ func ParseAuthorization(header string) (scheme string, origin, destination spec.
332332
if len(pair) != 2 {
333333
continue
334334
}
335-
name := pair[0]
336-
value := strings.Trim(pair[1], "\"")
335+
name := strings.TrimSpace(pair[0])
336+
value := strings.Trim(strings.TrimSpace(pair[1]), "\"")
337337
if name == "origin" {
338338
origin = spec.ServerName(value)
339339
}

fclient/request_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,58 @@ func mustLoadPrivateKey(seed string) ed25519.PrivateKey {
186186
}
187187
return privateKey
188188
}
189+
190+
func TestParseAuthorization(t *testing.T) {
191+
wantScheme := "X-Matrix"
192+
wantOrigin := spec.ServerName("foo")
193+
wantKey := gomatrixserverlib.KeyID("ed25519:1")
194+
wantSig := "sig"
195+
wantDestination := spec.ServerName("bar")
196+
197+
tests := []struct {
198+
name string
199+
header string
200+
}{
201+
{
202+
name: "parse with whitespace",
203+
header: `X-Matrix origin=foo , key="ed25519:1", sig="sig", destination="bar"`,
204+
},
205+
{
206+
name: "parse without spaces",
207+
header: `X-Matrix origin=foo,key="ed25519:1",sig="sig",destination="bar"`,
208+
},
209+
{
210+
name: "parse with tabs spaces",
211+
header: `X-Matrix origin=foo , key="ed25519:1", sig="sig" ,destination ="bar"`,
212+
},
213+
{
214+
name: "parse with different ordering and tabs",
215+
header: `X-Matrix origin=foo , ,destination ="bar", sig="sig", key="ed25519:1"`,
216+
},
217+
{
218+
name: "parse with different ordering and whitespace around values",
219+
header: `X-Matrix origin=foo , ,destination = "bar" , sig= "sig" , key="ed25519:1"`,
220+
},
221+
}
222+
for _, tt := range tests {
223+
t.Run(tt.name, func(t *testing.T) {
224+
gotScheme, gotOrigin, gotDestination, gotKey, gotSig := ParseAuthorization(tt.header)
225+
226+
if gotScheme != wantScheme {
227+
t.Errorf("ParseAuthorization() gotScheme = %v, want %v", gotScheme, wantScheme)
228+
}
229+
if gotOrigin != wantOrigin {
230+
t.Errorf("ParseAuthorization() gotOrigin = %v, want %v", gotOrigin, wantOrigin)
231+
}
232+
if gotDestination != wantDestination {
233+
t.Errorf("ParseAuthorization() gotDestination = %v, want %v", gotDestination, wantDestination)
234+
}
235+
if gotKey != wantKey {
236+
t.Errorf("ParseAuthorization() gotKey = %v, want %v", gotKey, wantKey)
237+
}
238+
if gotSig != wantSig {
239+
t.Errorf("ParseAuthorization() gotSig = %v, want %v", gotSig, wantSig)
240+
}
241+
})
242+
}
243+
}

0 commit comments

Comments
 (0)