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

Fix X-Matrix auth parsing #617

Merged
merged 2 commits into from
Sep 4, 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.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

* Return a 404 instead of 500 when clients access media which is frozen.
* Ensure the request parameters are correctly set for authenticated media client requests.
* Fixed parsing of `Authorization` headers for federated servers.

## [1.3.7] - July 30, 2024

Expand Down
47 changes: 12 additions & 35 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,21 @@ func GetXMatrixAuth(headers []string) ([]XMatrixAuth, error) {

paramCsv := h[len("X-Matrix "):]
params := make(map[string]string)
isKey := true
keyName := ""
keyValue := ""
escape := false
for _, c := range paramCsv {
if c == ',' && isKey {
params[strings.TrimSpace(strings.ToLower(keyName))] = keyValue
keyName = ""
keyValue = ""
continue
}
if c == '=' {
isKey = false
continue

pairs := strings.Split(paramCsv, ",")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break if , appears inside a quoted value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably, but also , is illegal inside a value, regardless of quotations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(there's also no possible value which contains ,)

for _, pair := range pairs {
csv := strings.SplitN(pair, "=", 2)
if len(csv) != 2 {
return nil, fmt.Errorf("invalid auth param pair: %s", pair)
}

if isKey {
keyName = fmt.Sprintf("%s%s", keyName, string(c))
} else {
if c == '\\' && !escape {
escape = true
continue
}
if c == '"' && !escape {
escape = false
if len(keyValue) > 0 {
isKey = true
}
continue
}
if escape {
escape = false
}
keyValue = fmt.Sprintf("%s%s", keyValue, string(c))
key := strings.TrimSpace(strings.ToLower(csv[0]))
value := strings.Trim(strings.TrimSpace(csv[1]), "\"")
if _, ok := params[key]; ok {
return nil, fmt.Errorf("duplicate auth param: %s", key)
}
}
if len(keyName) > 0 && isKey {
params[strings.TrimSpace(strings.ToLower(keyName))] = keyValue

params[key] = value
}

sig, err := DecodeUnpaddedBase64String(params["sig"])
Expand Down
Loading