AshReports.Security.AtomValidator (ash_reports v0.1.0)
Validates and safely converts strings to atoms using whitelists.
This module prevents atom table exhaustion attacks by only allowing conversion of whitelisted string values to atoms. All other values are kept as strings or rejected with clear error messages.
Security Rationale
Elixir atoms are not garbage collected. Creating atoms dynamically from user input can lead to atom table exhaustion (default limit: ~1M atoms), causing the VM to crash (DoS vulnerability).
Usage
# Safe - uses whitelist
AtomValidator.to_chart_type("bar")
#=> {:ok, :bar}
# Safe - rejects unknown
AtomValidator.to_chart_type("malicious")
#=> {:error, :invalid_chart_type}
# Safe - keeps as string
AtomValidator.to_field_name("user_input")
#=> {:ok, "user_input"}
Summary
Functions
Returns the list of allowed aggregation functions.
Returns the list of allowed chart providers.
Returns the list of allowed chart types.
Returns the list of allowed export formats.
Returns the list of allowed sort directions.
Converts an aggregation function string to an atom if allowed.
Converts a string to a chart provider atom if it's in the allowed list.
Converts a string to a chart type atom if it's in the allowed list.
Converts a string to an export format atom if it's in the allowed list.
Safely handles field names by keeping them as strings.
Converts a string to a sort direction atom if it's in the allowed list.
Functions
Returns the list of allowed aggregation functions.
Returns the list of allowed chart providers.
Returns the list of allowed chart types.
Returns the list of allowed export formats.
Returns the list of allowed sort directions.
@spec to_aggregation_function(String.t() | atom()) :: {:ok, atom()} | {:error, :invalid_aggregation_function}
Converts an aggregation function string to an atom if allowed.
Converts a string to a chart provider atom if it's in the allowed list.
Converts a string to a chart type atom if it's in the allowed list.
Examples
iex> AtomValidator.to_chart_type("bar")
{:ok, :bar}
iex> AtomValidator.to_chart_type("invalid")
{:error, :invalid_chart_type}
iex> AtomValidator.to_chart_type(:bar)
{:ok, :bar}
Converts a string to an export format atom if it's in the allowed list.
Safely handles field names by keeping them as strings.
Field names come from user data/schema and should never be converted to atoms to prevent atom table exhaustion.
Examples
iex> AtomValidator.to_field_name("user_field")
{:ok, "user_field"}
iex> AtomValidator.to_field_name(:existing_atom)
{:ok, :existing_atom}
Converts a string to a sort direction atom if it's in the allowed list.