Skip to content
Open
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
17 changes: 17 additions & 0 deletions lib/mpp/parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,29 @@ def unescape_quoted(str)
sig { params(params_str: T.untyped).returns(T::Hash[T.untyped, T.untyped]) }
def parse_auth_params(params_str)
params = {}
pos = 0
first = true

params_str.scan(AUTH_PARAM_RE) do |key, quoted_val, token_val|
match = T.must(Regexp.last_match)
separator = T.must(params_str[pos...match.begin(0)])
if first
Kernel.raise Mpp::ParseError, "Malformed authentication parameters" unless separator.strip.empty?
else
Kernel.raise Mpp::ParseError, "Malformed authentication parameters" unless separator.match?(/\A\s*,\s*\z/)
end

Kernel.raise Mpp::ParseError, "Duplicate parameter: #{key}" if params.key?(key)

value = quoted_val.nil? ? token_val : unescape_quoted(quoted_val)
params[key] = value
pos = match.end(0)
first = false
end

tail = T.must(params_str[pos..])
Kernel.raise Mpp::ParseError, "Malformed authentication parameters" unless tail.strip.empty?

params
end

Expand Down
17 changes: 17 additions & 0 deletions test/mpp/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ def test_parse_www_authenticate_rejects_missing_fields
assert_raises(Mpp::ParseError) { Mpp::Challenge.from_www_authenticate('Payment id="abc"') }
end

def test_parse_www_authenticate_rejects_malformed_auth_params
challenge = Mpp::Challenge.create(
secret_key: "test-secret",
realm: "api.example.com",
method: "tempo",
intent: "charge",
request: {"amount" => "1000000"}
)
header = challenge.to_www_authenticate("api.example.com")

missing_separator = header.sub(", realm=", " realm=")
trailing_junk = "#{header} not-a-param"

assert_raises(Mpp::ParseError) { Mpp::Challenge.from_www_authenticate(missing_separator) }
assert_raises(Mpp::ParseError) { Mpp::Challenge.from_www_authenticate(trailing_junk) }
end

def test_credential_roundtrip
echo = Mpp::ChallengeEcho.new(
id: "test-id",
Expand Down