Skip to content

Commit a5435a6

Browse files
authored
Reject invalid config shapes in Config.validate!/2 (#15349)
Previous code used `Enum.all?` and silently skipped invalid pairs
1 parent 3549775 commit a5435a6

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

lib/elixir/lib/config.ex

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,27 @@ defmodule Config do
377377
end
378378
end
379379

380-
defp validate!(config, file) do
381-
Enum.all?(config, fn
380+
defp validate!(config, file) when is_list(config) do
381+
Enum.each(config, fn
382382
{app, value} when is_atom(app) ->
383-
if Keyword.keyword?(value) do
384-
true
385-
else
383+
if not Keyword.keyword?(value) do
386384
raise ArgumentError,
387385
"expected config for app #{inspect(app)} in #{Path.relative_to_cwd(file)} " <>
388386
"to return keyword list, got: #{inspect(value)}"
389387
end
390388

391-
_ ->
392-
false
389+
other ->
390+
raise ArgumentError,
391+
"expected config in #{Path.relative_to_cwd(file)} to be a keyword list " <>
392+
"of {atom, keyword} pairs, got entry: #{inspect(other)}"
393393
end)
394394

395395
config
396396
end
397+
398+
defp validate!(config, file) do
399+
raise ArgumentError,
400+
"expected config in #{Path.relative_to_cwd(file)} to be a keyword list " <>
401+
"of {atom, keyword} pairs, got: #{inspect(config)}"
402+
end
397403
end

lib/elixir/test/elixir/config/reader_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ defmodule Config.ReaderTest do
5252
~r"expected config for app :sample in .*/bad_app.exs to return keyword list",
5353
fn -> Config.Reader.read!(fixture_path("configs/bad_app.exs")) end
5454

55+
assert_raise ArgumentError,
56+
~r"expected config .* to be a keyword list of {atom, keyword} pairs",
57+
fn ->
58+
Config.Reader.eval!(
59+
"nofile",
60+
~s([{"not_atom", [key: :val]}, {:valid, [k: :v]}])
61+
)
62+
end
63+
64+
assert_raise ArgumentError,
65+
~r"expected config .* to be a keyword list of {atom, keyword} pairs",
66+
fn -> Config.Reader.eval!("nofile", "[:not_a_pair]") end
67+
5568
assert_raise RuntimeError, "no :env key was given to this configuration file", fn ->
5669
Config.Reader.read!(fixture_path("configs/env.exs"))
5770
end

0 commit comments

Comments
 (0)