hex.pm breach: fake structs chained to RCE

hex.pm breach: fake structs chained to RCE

The first exploit request reached hex.pm at 18:16:00 UTC on November 23. Sentry logged an error for that request and for each of the 69 that arrived over the next thirteen minutes. By 18:29 the attacker had run commands on six web servers. The response took about 20 minutes, and the postmortem published the next day is unusually honest about what its own logs cannot answer. That line is why the hex.pm breach deserves more attention than the fix: database reads are not logged.

Hex.pm can prove what the attacker did not do. What they read is unknowable, and unknowable means you assume it, plan for it, and tell users before they hear it elsewhere.

Fake structs slipped past the safe decoder

Every Hex client speaks a binary Erlang term format to the registry. The endpoint decodes unauthenticated request bodies in that format with Plug.Crypto.non_executable_binary_to_term/2 called with [:safe], before routing and before authentication, so any unauthenticated POST carrying the application/vnd.hex+erlang content type reached it on every route. Safe mode is not decoration: it rejects functions, because they execute, and it rejects new atoms, because they can exhaust the atom table. What it never checked was maps.

An Elixir struct is just a map with a __struct__ key naming a module. The decoder returned attacker-built structs as the request’s body parameters, with every field set to whatever the request chose, and nothing downstream knew the difference. The runtime believed them.

The five hops from decode to command execution

None of what follows is a vulnerability on its own. Each hop is a legitimate piece of code doing exactly its documented job, the kind a reviewer signs off without reading twice. Strung together, they run arbitrary commands.

HopWhat runsWhy the attacker reaches it
DecodePlug.Crypto.non_executable_binary_to_term/2runs before routing and authentication
Method overridePlug.MethodOverride calls fetch/2 on the struct’s moduleraises UndefinedFunctionError with the struct in the stack trace
Error reportinginspect on request data and stack framesSentry, Bandit logging and the JSON formatter all call it
Inspect dispatchEcto.Query.DynamicExpr implementationthe attacker picked the module named in __struct__
Stream log callbackdb_connection applies the stream’s :log optiona {module, function, args} tuple taken from the request

The chain works because Plug.MethodOverride reads body_params[“_method”], which on a struct calls fetch/2 on the struct’s module. That function does not exist, so the request raises an UndefinedFunctionError with the attacker’s struct sitting in the stack trace. Error reporting is where the struct finally executes: Sentry, Bandit’s error logging, and the JSON log formatter all call inspect on request data and stack-frame arguments. inspect is a protocol, so for a struct it runs the implementation of the module named in __struct__. The attacker chose that module: Ecto.Query.DynamicExpr.

From there the hops accelerate. The Inspect implementation for Ecto.Query.DynamicExpr enumerates its binding field without checking that it is a list, and enumeration is itself a protocol call, so a struct placed in that field dispatches to another implementation. The struct used there was a DBConnection.Stream, whose Enumerable implementation calls DBConnection.reduce/3 and then declare/4. Encoding the query fails before any connection is checked out, but the error path still runs the stream’s :log option, and that option is a {module, function, args} tuple taken straight from the request. db_connection applied the attacker’s tuple with the log entry prepended, and that is command execution as the BEAM’s user.

non_executable_binary_to_term guards the things that execute directly, functions and new atoms. A struct executes the first protocol implementation it meets, and in Elixir almost everything is a protocol, which means almost every library exposes one.

What the hex.pm breach proves, and what it can’t

The verification work is the strongest part of the writeup. They restored a database copy from 18:13:59, before the attacker’s first request of any kind, and compared every row of every table against the live database. Every difference traces to an audit log entry from another address, background jobs, or their own response actions. All 23 releases published during the incident match publish or stage entries there. All 62 package files and all 186 registry files written since 18:00 match the checksums stored in the database. No shells were found on the replaced machines, and cloud logs show no new keys or API calls from the attacker’s addresses.

  • Writes: every row, function, role and permission change since 18:13:59 is accounted for.
  • Packages: 23 releases, 62 package files and 186 registry files, all matching stored checksums.
  • Persistence: no shells on the replaced servers, no new cloud keys, no anomalous API calls.
  • Database reads: not logged, so exposure of email addresses, password hashes, API key hashes, 2FA secrets, sessions and audit logs cannot be ruled out.
  • Private documentation reads: also not logged, and a read made from a compromised server looks identical to one of hex.pm’s own.

That gap decides the risk. Sessions, password hashes and 2FA secrets live in that database, so the team has to assume the attacker saw them, rotate everything, and report the event to IMY, CISA and ENISA as a personal data breach. Read accounting is not glamorous, but it is the difference between saying we believe nothing was taken and saying we know what was available to take. I would rather see read logging on credential tables than another perimeter rule. The same asymmetry shows up when Claude Code secrets leak through an allowlist: the surface nobody logs is the one that bites.

Twenty minutes, minute by minute

The timeline rewards a close read. The first response action came at 18:23:55, under eight minutes after Sentry logged the first exploit error at 18:16:00 and one for each request that followed. A firewall rule was added at 18:27, and it took full effect between 18:30 and 18:33. All six servers were replaced with a patched build between 18:30 and 18:31, files created on the affected servers appear 3 ms after a request at 18:16:46, and nothing succeeded after 18:29:33.

Two details in the timeline are easy to miss. The restart at 18:28 brought up three new servers still running the vulnerable build, which is why six servers were hit; replacing infrastructure mid-incident can mint new victims. And at 19:07, forty minutes after the block, an email arrived describing the same vulnerability. The attackers may have reported it themselves, or a researcher may have been watching; the postmortem does not say. Either way, by 10:42 the next morning every credential the attacker could have read was rotated, the database password included, and the CVE went public at 13:48.

One decoder fix does not close this class

Commit 379b0a2 makes the decoder reject any term containing a map with a __struct__ key and return a 400. Because the check lives in the single decoder used for this format, it covers every route that accepts it. That closes the entry point. The class survives it.

The hardening list reads like an admission that five libraries each held one link of the chain. plug_crypto will document that the decoder is only safe for authenticated payloads, and it will gain an option to reject structs; Plug will refuse struct values as body params.

Ecto will check that the binding is a list before enumerating it inside Inspect. db_connection will stop applying :log callbacks taken from stream structs; that one is tracked as CVE-2026-133555. And within a day of the postmortem the same shape turned up in Ash: its keyset pagination accepted crafted structs in client-supplied cursors, which could lead to command execution or SQL injection. Ash 3.38.0 shipped the fix on November 24 as CVE-2026-124283. Finding the same pattern in a second library within 24 hours is the strongest argument that the decoder patch is triage, not a cure.

Trust in a registry is a chain too. Codeberg lost its name, not its servers, and a mirror of a compromised registry inherits the compromise together with every package mirrored from it. Until the new signing system ships, repo.hex.pm is the only origin to fetch from.

If you publish packages to hex.pm

The immediate steps are short. Change your password. Then reset 2FA at hex.pm/dashboard/security: under Authenticator App, choose Setup New App and verify a code from the new entry, which gets you a new secret and fresh recovery codes even where an organization requires 2FA and refuses to disable it. When the new Hex client with the registry signing system lands in the coming days, update it:

mix local.hex --force

Until then, fetch packages only from repo.hex.pm. Treat any email claiming to come from Hex as hostile until you have checked its links, and forward the suspicious ones to security@hex.pm. I would also keep an eye on how a secure element can be fine while the box around it is not, because the same logic applies: each component in the signing chain will be solid, and the next compromise, if there is one, lands in the seam between two solid parts.

Press ⌘K to search & switch themes