logo

Insecure Deserialization - Elixir


Need

To protect against unauthorized control of application execution flow


Context

  1. Usage of Elixir for building scalable and fault-tolerant applications
  2. Usage of Elixir Phoenix for building web applications
  3. Usage of serialization/deserialization libraries for handling serialized objects from untrusted sources

Description

Insecure Code Example

defmodule VulnerableApp.WebController do
  use VulnerableApp.Web, :controller

  def deserialize(conn, _params) do
    {:ok, params} = Poison.decode(conn.params["payload"])
    process_params(params)
  end

  defp process_params(params) do
    # Handle params...
  end
end

The following Elixir code deserializes an incoming object from an untrusted source without validating or casting it. This leaves the application open to manipulation from an attacker.

Steps

  1. Use a library such as `jason` to validate the structure of the incoming serialized object.
  2. Only deserialize the object if it meets the expected properties.

Secure Code Example

defmodule SecureApp.WebController do
  use SecureApp.Web, :controller

  def deserialize(conn, _params) do
    case Jason.decode(conn.params["payload"]) do
      {:ok, params} when is_map(params) ->
        process_params(params)
      _ ->
        {:error, "Invalid payload"}
    end
  end

  defp process_params(params) do
    # Handle params...
  end
end

The following Elixir code deserializes an incoming object from an untrusted source but first validates it. This prevents an attacker from manipulating the execution flow of the application.


References

  • 096 - Insecure Deserialization

  • Last updated

    2023/09/18