Am I affected?

type your phoenix version to check

Description

Improper Input Validation vulnerability in phoenixframework phoenix (Phoenix.Presence) allows an attacker who can join a channel to cause a denial of service via a non-string presence key.

track/3,4 accepts any term as the presence key, such as a JSON object taken from a join payload. The key is converted with to_string/1 only later, in Phoenix.Presence.group/1, which raises Protocol.UndefinedError on a map. group/1 runs whenever presences are listed and when each presence diff is broadcast, together with the application's fetch/2, which can likewise raise on an unexpected key. Once such a key is tracked, every process that lists presences on the topic crashes, typically each channel that joins it, and presence diffs for the topic are lost.

From 1.7.0 the diff runs in a Task.Supervisor.async task linked to the tracker shard, and Phoenix.Presence.handle_info/2 has no clause for its failure. The shard then terminates, taking down every channel and LiveView process tracked on it and erasing presence state for all its topics. Repeated joins exhaust the restart intensity and take down the application supervisor.

This issue affects phoenix: from 1.2.0 before 1.5.16, from 1.6.0-rc.0 before 1.6.18, from 1.7.0-rc.0 before 1.7.25, and from 1.8.0-rc.0 before 1.8.17.

Technical analysis

1. Key accepted. The track/3,4 injected by use Phoenix.Presence passes any term to Phoenix.Tracker.track, which returns {:ok, ref}. The documented pattern Presence.track(socket, name, meta) with name from the join payload lets a client choose a map as the key.

2. Deferred conversion. Phoenix.Presence.group/1 converts each key with to_string/1. It runs in Phoenix.Presence.list/2, in the calling process, and in handle_diff/2 before the application's fetch/2. A map key raises Protocol.UndefinedError there, as does a fetch/2 such as the moduledoc's String.to_integer(key) on a non-numeric key.

3. Topic crash. While the key stays tracked, every list on the topic raises. The documented after_join that pushes Presence.list(socket) therefore crashes each later joiner's channel. Before 1.7.0, handle_diff/2 runs in an unlinked Task.Supervisor.start_child task, so a failing diff only loses that batch of joins and leaves.

4. Shard crash (1.7.0 and later). handle_diff/2 runs the diff in a Task.Supervisor.async task linked to the tracker shard, and Phoenix.Presence.handle_info/2 matches only the task's success reply. The failure terminates the shard (FunctionClauseError), every tracked process linked to it dies, and presence state is wiped. Repeated joins exhaust the supervisor's restart intensity.

Proof of concept

  1. Run a Phoenix endpoint with a channel whose join reads name from the join payload and then calls MyPresence.track(socket, name, %{online_at: 1}).
  2. Join the topic with 15 clients using string names, and confirm MyPresence.list(topic) returns 15 entries.
  3. Join once more with the payload {"name": {"x": 1}}.
  4. The log shows Protocol.UndefinedError from Phoenix.Presence.group/1, then the shard terminating. All 15 clients receive phx_error, and MyPresence.list(topic) returns 0 entries.

Weaknesses & attack patterns

Weakness

CWE-20 · Improper Input Validation in catalog → MITRE ↗

Attack patterns

CAPEC-153 · Input Data Manipulation MITRE ↗

With one channel join an attacker makes the topic's presence unusable: every later join that lists presences crashes, and presence updates stop. From 1.7.0 the same join terminates every channel and LiveView process tracked on the Presence shard and erases presence state for all its topics, and repeating it brings down the application.

Affected — Hex / phoenix Hex.pm ↗ Repository ↗

≥ 1.2.0 < 1.5.16 affected
1.6 series ≥ 1.6.0-rc.0 < 1.6.18 affected
1.7 series ≥ 1.7.0-rc.0 < 1.7.25 affected
1.8 series ≥ 1.8.0-rc.0 < 1.8.17 affected
every other version: unaffected
cpe cpe:2.3:a:phoenixframework:phoenix:*:*:*:*:*:*:*:*
version type semver
modules · source files · routines
modules 'Elixir.Phoenix.Presence'
source files lib/phoenix/presence.ex
routines 'Elixir.Phoenix.Presence':handle_info/2 · 'Elixir.Phoenix.Presence':handle_diff/2 · 'Elixir.Phoenix.Presence':group/1

Affected — GitHub / phoenixframework/phoenix Repository ↗

≥ 44e7618 and up affected
→ 39c1c6b not affected
→ 6609595 not affected
→ 81bc027 not affected
→ edb7028 not affected
every other version: unaffected
cpe cpe:2.3:a:phoenixframework:phoenix:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules 'Elixir.Phoenix.Presence'
source files lib/phoenix/presence.ex
routines 'Elixir.Phoenix.Presence':handle_info/2 · 'Elixir.Phoenix.Presence':handle_diff/2 · 'Elixir.Phoenix.Presence':group/1

Workarounds

Validate every presence key before passing it to track/3,4, untrack/2,3 or update/3,4, so that only strings reach the tracker. For a key taken from client input, reject a join whose value is not a binary, or convert it explicitly:

def join("room:" <> _, %{"name" => name}, socket) when is_binary(name) do
  send(self(), {:after_join, name})
  {:ok, socket}
end

A fetch/2 callback must also not raise on any tracked key.

Configurations

The application must use Phoenix.Presence and either track presences under a key influenced by client input (such as a name taken from the channel join payload), or define a fetch/2 callback that can raise on a tracked key. Applications that track only server-controlled string keys and whose fetch/2 cannot raise are not affected.

References

Credits

Peter Ullrich Finder Reporter
Steffen Deusch Remediation developer
José Valim Remediation reviewer

CVSS breakdown

CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N

Exploitability

Attack Vector
Network
Attack Complexity
Low
Attack Requirements
Present
Privileges Required
None
User Interaction
None

Vulnerable System

Confidentiality
None
Integrity
None
Availability
High

Subsequent Systems

Confidentiality
None
Integrity
None
Availability
None
« All CVEs