Skip to content

Commit 4fd7b64

Browse files
committed
fix: salt parameter splicing
1 parent 8501412 commit 4fd7b64

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/altcha.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def self.create_challenge(options)
254254

255255
salt = options.salt || random_bytes(salt_length).unpack1('H*')
256256
salt += "?#{URI.encode_www_form(params)}" unless params.empty?
257+
salt += salt.end_with?('&') ? '' : '&'
257258

258259
number = options.number || random_int(max_number)
259260

spec/altcha_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@
8787
expect(Altcha.verify_solution(payload, hmac_key, true)).to be true
8888
end
8989

90+
it 'fails to verify an incorrect solution with salt splicing' do
91+
challenge_options_with_expires = Altcha::ChallengeOptions.new(
92+
algorithm: algorithm,
93+
expires: Time.now.to_i + 3600,
94+
hmac_key: hmac_key,
95+
salt: salt,
96+
number: 123
97+
)
98+
challenge = Altcha.create_challenge(challenge_options_with_expires)
99+
payload = {
100+
algorithm: algorithm,
101+
challenge: challenge.challenge,
102+
number: 23,
103+
salt: challenge.salt + '1',
104+
signature: challenge.signature
105+
}
106+
expect(Altcha.verify_solution(payload, hmac_key, true)).to be false
107+
end
108+
90109
it 'fails to verify an incorrect solution' do
91110
payload = { algorithm: algorithm, challenge: 'wrong_challenge', number: number, salt: salt, signature: 'wrong_signature' }
92111
expect(Altcha.verify_solution(payload, hmac_key, false)).to be false
@@ -131,7 +150,7 @@
131150
describe '.solve_challenge' do
132151
it 'solves a challenge correctly' do
133152
challenge = Altcha.create_challenge(challenge_options)
134-
solution = Altcha.solve_challenge(challenge.challenge, salt, algorithm, 10_000, 0)
153+
solution = Altcha.solve_challenge(challenge.challenge, challenge.salt, algorithm, 10_000, 0)
135154
expect(solution).not_to be_nil
136155
expect(solution.number).to eq(number)
137156
end

0 commit comments

Comments
 (0)