Skip to content

Commit 810925e

Browse files
committed
xff lua
1 parent dc2343d commit 810925e

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

charts/synapse/scripts/synapse.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,22 @@ local function lookup_whoami(request_handle, token, options)
160160

161161
log(request_handle, options, "warn", "performing whoami lookup for token " .. truncate_token(token, options))
162162

163+
local call_headers = {
164+
[":method"] = "GET",
165+
[":path"] = get_option(options, "whoami_path", "/_matrix/client/v3/account/whoami"),
166+
[":authority"] = authority,
167+
["authorization"] = "Bearer " .. token,
168+
["x-forwarded-proto"] = "https"
169+
}
170+
local xff = headers:get("x-forwarded-for")
171+
if xff ~= nil then
172+
call_headers["x-forwarded-for"] = xff
173+
end
174+
163175
local ok, response_headers, response_body = pcall(function()
164176
return request_handle:httpCall(
165177
get_option(options, "whoami_cluster", "httpd"),
166-
{
167-
[":method"] = "GET",
168-
[":path"] = get_option(options, "whoami_path", "/_matrix/client/v3/account/whoami"),
169-
[":authority"] = authority,
170-
["authorization"] = "Bearer " .. token,
171-
["x-forwarded-proto"] = "https"
172-
},
178+
call_headers,
173179
"",
174180
get_option(options, "timeout_ms", 5000)
175181
)

charts/synapse/scripts/synapse_test.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,37 @@ local function test_whoami_lookup_returns_localpart()
101101
assert_equal(calls, 1, "whoami should be called once")
102102
end
103103

104+
local function test_whoami_lookup_forwards_xff()
105+
local captured_xff
106+
local request_handle = new_request_handle({
107+
[":path"] = "/_matrix/client/v3/sync",
108+
[":authority"] = "matrix.example.org",
109+
["authorization"] = "Bearer xff-token",
110+
["x-forwarded-for"] = "203.0.113.1"
111+
}, function(_, cluster, headers, body, timeout_ms)
112+
captured_xff = headers["x-forwarded-for"]
113+
return { [":status"] = "200" }, '{"user_id":"@carol:example.org"}'
114+
end)
115+
116+
synapse.get_user_identifier_from_request(request_handle, {})
117+
assert_equal(captured_xff, "203.0.113.1", "whoami httpCall should forward x-forwarded-for")
118+
end
119+
120+
local function test_whoami_lookup_without_xff()
121+
local captured_xff = "sentinel"
122+
local request_handle = new_request_handle({
123+
[":path"] = "/_matrix/client/v3/sync",
124+
[":authority"] = "matrix.example.org",
125+
["authorization"] = "Bearer no-xff-token"
126+
}, function(_, cluster, headers, body, timeout_ms)
127+
captured_xff = headers["x-forwarded-for"]
128+
return { [":status"] = "200" }, '{"user_id":"@dave:example.org"}'
129+
end)
130+
131+
synapse.get_user_identifier_from_request(request_handle, {})
132+
assert_equal(captured_xff, nil, "whoami httpCall should not set x-forwarded-for when absent")
133+
end
134+
104135
local function test_whoami_lookup_is_cached()
105136
local request_handle = new_request_handle({
106137
[":path"] = "/_matrix/client/v3/sync",
@@ -184,6 +215,8 @@ local tests = {
184215
test_access_token_from_authorization_header,
185216
test_access_token_from_query,
186217
test_whoami_lookup_returns_localpart,
218+
test_whoami_lookup_forwards_xff,
219+
test_whoami_lookup_without_xff,
187220
test_whoami_lookup_is_cached,
188221
test_whoami_failure_falls_back_to_token,
189222
test_missing_token_returns_nil,

0 commit comments

Comments
 (0)