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

Enable https check in runtime #162

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 24 additions & 12 deletions lib/sobelow/config/https.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,40 @@ defmodule Sobelow.Config.HTTPS do

@uid 9
@finding_type "Config.HTTPS: HTTPS Not Enabled"
@files_to_check ["prod.exs", "runtime.exs"]

use Sobelow.Finding

def run(dir_path, configs) do
path = dir_path <> "prod.exs"
def run(dir_path, configs, files_to_check \\ @files_to_check) do
configs_in_files = configs_in_files(dir_path, configs, files_to_check)

if File.exists?(path) && Enum.member?(configs, "prod.exs") do
https = Config.get_configs_by_file(:https, path)

(Config.get_configs_by_file(:force_ssl, path) ++ https)
|> handle_https(path)
if !Enum.empty?(configs_in_files) && Enum.all?(configs_in_files, &https_config_missing?/1) do
Enum.each(configs_in_files, fn {path, _} ->
add_finding(path)
end)
end
end

defp handle_https(opts, path) do
if Enum.empty?(opts) do
add_finding(path)
end
defp configs_in_files(dir_path, configs, files) do
files
|> Enum.map(fn file_path ->
path = dir_path <> file_path
exists = File.exists?(path) && Enum.member?(configs, file_path)
{path, exists}
end)
|> Enum.filter(fn {_path, exists} -> exists end)
|> Enum.map(fn {path, _exists} ->
https = Config.get_configs_by_file(:https, path)
{path, Config.get_configs_by_file(:force_ssl, path) ++ https}
end)
end

defp https_config_missing?({_path, opts}) do
Enum.empty?(opts)
end

defp add_finding(file) do
reason = "HTTPS configuration details could not be found in `prod.exs`."
reason = "HTTPS configuration details could not be found in `prod.exs` nor `runtime.exs`."

finding =
%Finding{
Expand Down
36 changes: 36 additions & 0 deletions test/config/https_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule SobelowTest.Config.HttpsTest do
use ExUnit.Case
alias Sobelow.Config.HTTPS

setup do
Application.put_env(:sobelow, :format, "json")
Sobelow.Fingerprint.start_link()
Sobelow.FindingLog.start_link()

:ok
end

test "does not log when force_ssl is present in prod.exs" do
refute HTTPS.run("./test/fixtures/https/", ["prod.exs"])
end

test "does not log when force_ssl is present in runtime.exs" do
refute HTTPS.run("./test/fixtures/https/", ["runtime.exs"])
end

test "does not log when files don't exist" do
refute HTTPS.run("./test/fixtures/https/", ["prod.exs"], ["prod_does_not_exist.exs"])
end

test "does not log when one of the files has the https enabled" do
refute HTTPS.run("./test/fixtures/https/", ["prod.exs"], [
"prod_without_https.exs",
"prod.exs"
])
end

test "logs when config files exist but https in not found any of them" do
HTTPS.run("./test/fixtures/https/", ["prod_without_https.exs"], ["prod_without_https.exs"])
assert Sobelow.FindingLog.json("1") =~ "Config.HTTPS: HTTPS Not Enabled"
end
end
4 changes: 4 additions & 0 deletions test/fixtures/https/prod.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Config

config :phoenix_app,
force_ssl: [hsts: true]
4 changes: 4 additions & 0 deletions test/fixtures/https/prod_without_https.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Config

config :phoenix_app,
logger: :info
4 changes: 4 additions & 0 deletions test/fixtures/https/runtime.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Config

config :phoenix_app,
force_ssl: [hsts: true]
Loading