Skip to content

Commit 6e7185b

Browse files
committed
Fix wildcard check_origin vulnerability.
Previously, our documentation points to a wilcard example of: check_origin: [ "//*.other.com" ] Which should allow any subdomain of "other.com", but our comparison for `"//*.other.com"` would allow `api.any-other.com`, which would allow an attacker to register a domain with a custom prefix of a target domain and pass origin checks. This patch ensures the `String.ends_with?` check includes the subdomain dot prefix. Who is affected? Only those using a wildcard check origin are affected, and potential exploits are limited to allowing unauthenticated channel connections from a bad host. Because LiveView adds its own csrf token to the connection by default, LiveView applications with wildcard check origin would refuse connection under this scenario. Additionally, channel applications utilizing token based authentication would require the attacker to also have a valid token to connection from a bad host. Phoenix channels does not allow access to cookies, so an attacker would also not be able to pass their own cookies from a bad host.
1 parent f103409 commit 6e7185b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/phoenix/socket/transport.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ defmodule Phoenix.Socket.Transport do
624624
defp compare_host?(_request_host, nil),
625625
do: true
626626
defp compare_host?(request_host, "*." <> allowed_host),
627-
do: String.ends_with?(request_host, allowed_host)
627+
do: request_host == allowed_host or String.ends_with?(request_host, "." <> allowed_host)
628628
defp compare_host?(request_host, allowed_host),
629629
do: request_host == allowed_host
630630

test/phoenix/socket/transport_test.exs

+15
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ defmodule Phoenix.Socket.TransportTest do
8585
refute conn.halted
8686
conn = check_origin("https://org1.ex.com", check_origin: origins)
8787
refute conn.halted
88+
89+
conn = check_origin("https://ex.com", check_origin: origins)
90+
refute conn.halted
91+
92+
conn = check_origin("https://org1.prefix-ex.com", check_origin: origins)
93+
assert conn.halted
8894
end
8995

9096
test "nested wildcard subdomains" do
@@ -93,6 +99,15 @@ defmodule Phoenix.Socket.TransportTest do
9399
conn = check_origin("http://org1.foo.example.com", check_origin: origins)
94100
refute conn.halted
95101

102+
conn = check_origin("http://foo.example.com", check_origin: origins)
103+
refute conn.halted
104+
105+
conn = check_origin("http://bad.example.com", check_origin: origins)
106+
assert conn.halted
107+
108+
conn = check_origin("http://org1.prefix-foo.example.com", check_origin: origins)
109+
assert conn.halted
110+
96111
conn = check_origin("http://org1.bar.example.com", check_origin: origins)
97112
assert conn.halted
98113
assert conn.status == 403

0 commit comments

Comments
 (0)