loppers v0.1.1 Loppers
A code validator for the Elixir-AST.
It can operate on both white- and blacklists.
Basic example:
iex> quoted = quote do "hello" |> String.upcase |> String.pad_leading(4, "0") end
iex> whitelist = Loppers.special_forms ++ [{Kernel, :|>}, {String, :upcase}, {String, :pad_leading}]
iex> Loppers.validate(quoted, whitelist: whitelist)
:ok
Link to this section Summary
Functions
All functions and macros needed to define modules, functions and set attributes
Convenience list of commonly used operators
A list of all macros contained in Kernel.SpecialForms
Validates a syntax tree against the given whitelist
Link to this section Types
function_ref() :: {module :: atom, :__all__} | {module :: atom, function :: atom} | function :: atom
validate_option :: {:whitelist, [function_ref]} | {:blacklist, [function_ref]}
Link to this section Functions
All functions and macros needed to define modules, functions and set attributes
Convenience list of commonly used operators
A list of all macros contained in Kernel.SpecialForms
.
Without those it’s going to be hard to write any elixir code.
validate(quoted :: term, opts :: [validate_option]) :: :ok | {:error, [error]}
Validates a syntax tree against the given whitelist.
Use Code.string_to_quoted/2
to get the syntax tree out of source code.
When no whitelist is defined, it is assumed that all function calls are ok, except when they exist in the blacklist.
Supplying both a white- and a blacklist can be useful, for example when you want to allow all functions of a module, except a few that you don’t want:
iex> whitelist = Loppers.special_forms ++ [{Enum, :__all__}]
iex> blacklist = [{Enum, :map_reduce}]
iex> quoted = quote do Enum.map_reduce([], nil, &({&1, nil})) end
iex> Loppers.validate(quoted, [whitelist: whitelist, blacklist: blacklist])
{:error, [not_allowed: {{:., [], [{:__aliases__, [alias: false], [:Enum]}, :map_reduce]}, [], [[], nil, {:&, [], [{{:&, [], [1]}, nil}]}]}]}
Options
:whitelist
- a list offunction_ref
s that are allowed in the code:blacklist
- a list offunction_ref
s that are forbidden in the code