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

ignore runtime config in hsts check #166

Merged
merged 1 commit into from
May 19, 2024
Merged
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
11 changes: 7 additions & 4 deletions lib/sobelow/config/hsts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ defmodule Sobelow.Config.HSTS do

@uid 8
@finding_type "Config.HSTS: HSTS Not Enabled"
@ignored_files ["runtime.exs"]

use Sobelow.Finding

def run(dir_path, configs) do
Enum.each(configs, fn conf ->
path = dir_path <> conf
unless Enum.member?(@ignored_files, conf) do
path = dir_path <> conf

Config.get_configs_by_file(:https, path)
|> handle_https(path)
Config.get_configs_by_file(:https, path)
|> handle_https(path)
end
end)
end

defp handle_https(opts, file) do
# If HTTPS configs were found in any config file and there
# If HTTPS configs were found in any compile-time config file and there
# are no accompanying HSTS configs, add an HSTS finding.
if length(opts) > 0 && Enum.empty?(Config.get_configs(:force_ssl, file)) do
add_finding(file)
Expand Down
27 changes: 27 additions & 0 deletions test/config/hsts_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule SobelowTest.Config.HstsTest do
use ExUnit.Case
alias Sobelow.Config.HSTS

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

:ok
end

test "complains when force_ssl is missing in prod.exs" do
HSTS.run("./test/fixtures/hsts/", ["missing_prod.exs"])
assert Sobelow.FindingLog.json("1") =~ "Config.HSTS: HSTS Not Enabled"
end

test "does not complain when force_ssl is present in prod.exs" do
HSTS.run("./test/fixtures/hsts/", ["present_prod.exs"])
refute Sobelow.FindingLog.json("1") =~ "Config.HSTS: HSTS Not Enabled"
end

test "does not complain when force_ssl is missing in runtime.exs" do
HSTS.run("./test/fixtures/hsts/", ["runtime.exs"])
refute Sobelow.FindingLog.json("1") =~ "Config.HSTS: HSTS Not Enabled"
end
end
4 changes: 4 additions & 0 deletions test/fixtures/hsts/missing_prod.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Config

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

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

config :phoenix_app,
https: []
Loading