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

fix: handle invalid secret formats in parse_secret #326

Merged
merged 6 commits into from
May 3, 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.50
1.1.51
4 changes: 4 additions & 0 deletions lib/supavisor/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ defmodule Supavisor.Helpers do
{:ok, %{digest: :md5, secret: secret, user: user}}
end

def parse_secret(_secret, _user) do
{:error, "Unsupported or invalid secret format"}
end

def parse_postgres_secret(_), do: {:error, "Digest not supported"}

## Internal functions
Expand Down
38 changes: 36 additions & 2 deletions test/supavisor/helpers_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
defmodule Supavisor.HelpersTest do
use ExUnit.Case
doctest Supavisor.Helpers
use ExUnit.Case, async: true
alias Supavisor.Helpers

describe "parse_secret/2" do
test "parses SCRAM-SHA-256 secrets correctly" do
encoded_stored_key = Base.encode64("storedKey")
encoded_server_key = Base.encode64("serverKey")
secret = "SCRAM-SHA-256$4000:salt$#{encoded_stored_key}:#{encoded_server_key}"
user = "user@example.com"

expected =
{:ok,
%{
digest: "SCRAM-SHA-256",
iterations: 4000,
salt: "salt",
stored_key: "storedKey",
server_key: "serverKey",
user: user
}}

assert Helpers.parse_secret(secret, user) == expected
end

test "parses md5 secrets correctly" do
secret = "supersecret"
user = "user@example.com"
expected = {:ok, %{digest: :md5, secret: secret, user: user}}
assert Helpers.parse_secret("md5supersecret", user) == expected
end

test "returns error for unsupported or invalid secret formats" do
assert Helpers.parse_secret("unsupported_secret", "user@example.com") ==
{:error, "Unsupported or invalid secret format"}
end
end
end
Loading