Skip to content

Commit 4738de2

Browse files
committed
fix: salt parameter splicing
1 parent 725e604 commit 4738de2

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/Altcha.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function verifySolution(string|array $data, bool $checkExpires = true): b
154154
*/
155155
private function extractParams(Payload $payload): array
156156
{
157-
$saltParts = explode('?', $payload->salt);
157+
$saltParts = explode('?', rtrim($payload->salt, ';'));
158158
if (\count($saltParts) > 1) {
159159
parse_str($saltParts[1], $params);
160160

src/BaseChallengeOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function __construct(
3838
$salt .= '?' . http_build_query($params);
3939
}
4040

41+
// Add a delimiter to prevent parameter splicing
42+
if (!str_ends_with($salt, ';')) {
43+
$salt = $salt . ';';
44+
}
45+
4146
$this->salt = $salt;
4247
}
4348
}

tests/AltchaTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ public function testVerifySolution(): void
9898
self::assertTrue($isValid);
9999
}
100100

101+
public function testVerifySolutionSaltSplicing(): void
102+
{
103+
$challenge = self::$altcha->createChallenge(new BaseChallengeOptions(
104+
algorithm: Algorithm::SHA256,
105+
maxNumber: 50000,
106+
number: 123,
107+
expires: (new \DateTimeImmutable())->add(new \DateInterval('PT10S')),
108+
params: [],
109+
salt: bin2hex(random_bytes(12)),
110+
));
111+
112+
$payload = [
113+
'algorithm' => $challenge->algorithm,
114+
'challenge' => $challenge->challenge,
115+
'salt' => $challenge->salt . "1",
116+
'signature' => $challenge->signature,
117+
'number' => 23,
118+
];
119+
120+
$isValid = self::$altcha->verifySolution(base64_encode(json_encode($payload) ?: ''));
121+
122+
self::assertFalse($isValid);
123+
}
124+
101125
public function testVerifyServerSignature(): void
102126
{
103127
$algorithm = Algorithm::SHA256;

0 commit comments

Comments
 (0)