Skip to content

Commit b47ad2f

Browse files
authored
Ignore HSTS check in Runtime Config (#166)
1 parent 1c8388f commit b47ad2f

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

lib/sobelow/config/hsts.ex

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ defmodule Sobelow.Config.HSTS do
1414

1515
@uid 8
1616
@finding_type "Config.HSTS: HSTS Not Enabled"
17+
@ignored_files ["runtime.exs"]
1718

1819
use Sobelow.Finding
1920

2021
def run(dir_path, configs) do
2122
Enum.each(configs, fn conf ->
22-
path = dir_path <> conf
23+
unless Enum.member?(@ignored_files, conf) do
24+
path = dir_path <> conf
2325

24-
Config.get_configs_by_file(:https, path)
25-
|> handle_https(path)
26+
Config.get_configs_by_file(:https, path)
27+
|> handle_https(path)
28+
end
2629
end)
2730
end
2831

2932
defp handle_https(opts, file) do
30-
# If HTTPS configs were found in any config file and there
33+
# If HTTPS configs were found in any compile-time config file and there
3134
# are no accompanying HSTS configs, add an HSTS finding.
3235
if length(opts) > 0 && Enum.empty?(Config.get_configs(:force_ssl, file)) do
3336
add_finding(file)

test/config/hsts_test.exs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule SobelowTest.Config.HstsTest do
2+
use ExUnit.Case
3+
alias Sobelow.Config.HSTS
4+
5+
setup do
6+
Application.put_env(:sobelow, :format, "json")
7+
Sobelow.Fingerprint.start_link()
8+
Sobelow.FindingLog.start_link()
9+
10+
:ok
11+
end
12+
13+
test "complains when force_ssl is missing in prod.exs" do
14+
HSTS.run("./test/fixtures/hsts/", ["missing_prod.exs"])
15+
assert Sobelow.FindingLog.json("1") =~ "Config.HSTS: HSTS Not Enabled"
16+
end
17+
18+
test "does not complain when force_ssl is present in prod.exs" do
19+
HSTS.run("./test/fixtures/hsts/", ["present_prod.exs"])
20+
refute Sobelow.FindingLog.json("1") =~ "Config.HSTS: HSTS Not Enabled"
21+
end
22+
23+
test "does not complain when force_ssl is missing in runtime.exs" do
24+
HSTS.run("./test/fixtures/hsts/", ["runtime.exs"])
25+
refute Sobelow.FindingLog.json("1") =~ "Config.HSTS: HSTS Not Enabled"
26+
end
27+
end

test/fixtures/hsts/missing_prod.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use Config
2+
3+
config :phoenix_app,
4+
https: []

test/fixtures/hsts/present_prod.exs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use Config
2+
3+
config :phoenix_app,
4+
https: [],
5+
force_ssl: [hsts: true]

test/fixtures/hsts/runtime.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use Config
2+
3+
config :phoenix_app,
4+
https: []

0 commit comments

Comments
 (0)