Skip to content

Commit

Permalink
Replace insecure String.to_atom with String.to_existing_atom (#2)
Browse files Browse the repository at this point in the history
* Replace insecure String.to_atom with String.to_existing_atom

* String to_atom is safe here

Co-authored-by: Dung Nguyen <dung.nguyen@onpoint.vn>
  • Loading branch information
bluzky and bluzky authored Oct 28, 2020
1 parent b88eb13 commit ce37d3c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
9 changes: 5 additions & 4 deletions lib/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ defmodule Querie.Parser do
[field, operator] ->
case operator do
"ref" ->
{String.to_atom(operator), {String.to_atom(field), split_key_and_operator(value)}}
{:ref, {field, split_key_and_operator(value)}}

op when op in @supported_ops ->
{String.to_atom(op), {String.to_atom(field), value}}
{String.to_atom(op), {field, value}}

_ ->
nil
end

[field] ->
{:is, {String.to_atom(field), value}}
{:is, {field, value}}

_ ->
nil
Expand Down Expand Up @@ -99,7 +99,8 @@ defmodule Querie.Parser do
with field_def <- SchemaHelpers.get_field(schema, column),
{:field_def_nil, false} <- {:field_def_nil, is_nil(field_def)},
{:ok, casted_value} <- cast_field(field, field_def) do
{:ok, {operator, {column, casted_value}}}
# use String.to_existing_atom, here we make sure the column atom existed
{:ok, {operator, {String.to_existing_atom(column), casted_value}}}
else
{:field_def_nil, true} -> nil
:skip -> nil
Expand Down
20 changes: 11 additions & 9 deletions lib/schema_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
defmodule Querie.SchemaHelpers do
def get_field(schema, field) do
field_def = Map.get(schema, field)
case Enum.find(schema, fn {k, _} -> to_string(k) == field end) do
{_, field_def} ->
{type, opts} =
if is_atom(field_def) or is_tuple(field_def) do
{field_def, []}
else
Keyword.pop(field_def, :type)
end

if field_def do
{type, opts} =
if is_atom(field_def) or is_tuple(field_def) do
{field_def, []}
else
Keyword.pop(field_def, :type)
end
{field, type, opts}

{field, type, opts}
_ ->
nil
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/querex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule QuerieTest do
params = %{"name" => 123}
{code, data} = Querie.Parser.parse(schema, params)
assert code == :error
assert Enum.find_value(data, false, fn {field, _} -> field == :name end)
assert Enum.find_value(data, false, fn {field, _} -> field == "name" end)
end

test "parse integer range by map" do
Expand Down

0 comments on commit ce37d3c

Please sign in to comment.