Skip to content

Commit

Permalink
Ignore HSTS check in Runtime Config (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
btmo authored May 19, 2024
1 parent 1c8388f commit b47ad2f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
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: []

0 comments on commit b47ad2f

Please sign in to comment.