Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow basic expressions #9

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/safe_code/validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ defmodule SafeCode.Validator do
defp valid_node?({:., _, [{{:., _, [{module, _, _}, func]}, _, _}, _]}, opts), do: safe_module_function?(module, func, opts)
defp valid_node?({:., _, [{module, _, _}, func]}, opts), do: safe_module_function?(module, func, opts)
defp valid_node?({:., _, [module, func]}, opts) when is_atom(module), do: safe_module_function?(module, func, opts)
defp valid_node?({:not, _, _}, _opts), do: true
defp valid_node?({:||, _, _}, _opts), do: true
defp valid_node?({:&&, _, _}, _opts), do: true
defp valid_node?({:==, _, _}, _opts), do: true
defp valid_node?({:===, _, _}, _opts), do: true
defp valid_node?({:!, _, _}, _opts), do: true
defp valid_node?({:!=, _, _}, _opts), do: true
defp valid_node?({:!==, _, _}, _opts), do: true
defp valid_node?({:<, _, _}, _opts), do: true
defp valid_node?({:>, _, _}, _opts), do: true
defp valid_node?({:>=, _, _}, _opts), do: true
defp valid_node?({:<=, _, _}, _opts), do: true
defp valid_node?({:or, _, _}, _opts), do: true
defp valid_node?({:and, _, _}, _opts), do: true
defp valid_node?({:<>, _, _}, _opts), do: true
defp valid_node?({:++, _, _}, _opts), do: true
defp valid_node?({:--, _, _}, _opts), do: true

defp valid_node?({function, _meta, args}, opts) when is_atom(function) and is_list(args) do
FunctionValidators.safe_function?(function, opts)
Expand Down
2 changes: 2 additions & 0 deletions lib/safe_code/validator/function_validators/elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ defmodule SafeCode.Validator.FunctionValidators.Elixir do
def safe_function?(_), do: false

def safe_module_function?(Enum, :map), do: true
def safe_module_function?(Enum, :with_index), do: true
def safe_module_function?(Access, :get), do: true
def safe_module_function?(Map, :put), do: true

def safe_module_function?(Kernel, fun), do: safe_function?(fun)
def safe_module_function?(_, _), do: false
Expand Down
22 changes: 22 additions & 0 deletions lib/safe_code/validator/function_validators/phoenix.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
defmodule SafeCode.Validator.FunctionValidators.Phoenix do
@behaviour SafeCode.Validator.FunctionValidators.Behaviour

def safe_function?(:my_component), do: true
def safe_function?(:rem), do: true
def safe_function?(:&), do: true
def safe_function?(:/), do: true
def safe_function?(:{}), do: true
def safe_function?(_), do: false

def safe_module_function?(DockYardWeb.EmployeeView, :random_avatar_uri), do: true
def safe_module_function?(DockYardWeb.EmployeeView, :display_name), do: true
def safe_module_function?(DockYardWeb.PostView, :illustration), do: true
def safe_module_function?(DockYardWeb.PostView, :post_path), do: true
def safe_module_function?(DockYardWeb.PostView, :author_avatar_url), do: true
def safe_module_function?(DockYard.ClientLeads, :change_client_lead_form), do: true
Comment on lines +11 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to think of a way to remove this, it's trying to validate AST like this one:

{:., [line: 95], [{:__aliases__, [line: 95], [:DockYardWeb, :EmployeeView]}, :display_name]}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexandrexaviersm safe_code allows the application using it to specify its own safe functions. We are doing this in Beacon, but I need to expose a way for users using Beacon to tag safe functions. I am working on a PR for this now. You can remove these lines from this PR.

def safe_module_function?(Phoenix.LiveView.Engine, :live_to_iodata), do: true
def safe_module_function?(Phoenix.LiveView.Engine, :changed_assign?), do: true
def safe_module_function?(Phoenix.LiveView.Engine, :fetch_assign!), do: true
def safe_module_function?(Phoenix.LiveView.Engine, :nested_changed_assign?), do: true
def safe_module_function?(Phoenix.LiveView.Engine, :to_component_static), do: true
def safe_module_function?(Phoenix.LiveView.HTMLEngine, :binary_encode), do: true
def safe_module_function?(Phoenix.LiveView.HTMLEngine, :component), do: true
def safe_module_function?(Phoenix.LiveView.HTMLEngine, :function), do: true
def safe_module_function?(Phoenix.LiveView.HTMLEngine, :file), do: true
def safe_module_function?(Phoenix.LiveView.HTMLEngine, :inner_block), do: true
def safe_module_function?(Phoenix.HTML, :attributes_escape), do: true
def safe_module_function?(:assigns, _), do: true

def safe_module_function?(_, _), do: false

# def safe_module_function?(module, function) do
# IO.inspect("module: #{inspect(module)}")
# IO.inspect("function: #{function}")
# true
# end
Comment on lines +31 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# def safe_module_function?(module, function) do
# IO.inspect("module: #{inspect(module)}")
# IO.inspect("function: #{function}")
# true
# end

end
136 changes: 136 additions & 0 deletions test/safe_code/validator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,142 @@ defmodule SafeCode.ValidatorTest do
assert Validator.validate_heex!(heex)
end

test "accepts the 'not' expression" do
heex = """
hello <%= not false %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '||' expression" do
heex = """
hello <%= nil || true %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '&&' expression" do
heex = """
hello <%= 1 && true %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '!' expression" do
heex = """
hello <%= !false %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '==' expression" do
heex = """
hello <%= 1 == 1 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '===' expression" do
heex = """
hello <%= 1 === 1 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '!==' expression" do
heex = """
hello <%= 1 !== 2 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '!=' expression" do
heex = """
hello <%= 1 != 2 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '<' expression" do
heex = """
hello <%= 1 < 5 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '>' expression" do
heex = """
hello <%= 1 > 5 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '>=' expression" do
heex = """
hello <%= 5 >= 1 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '<=' expression" do
heex = """
hello <%= 1 <= 5 %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the 'or' expression" do
heex = """
hello <%= false or true %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the 'and' expression" do
heex = """
hello <%= true and true %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '<>' expression" do
heex = """
hello <%= "Steve" <> "Rogers" %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '++' expression" do
heex = """
hello <%= [1,2] ++ [3,4] %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "accepts the '--' expression" do
heex = """
hello <%= [1,2,3] -- [2] %> how are you
"""

assert Validator.validate_heex!(heex)
end

test "for loop" do
heex = """
<%= for foo <- bar do %>
Expand Down