Skip to content

Commit e30811f

Browse files
Merge pull request #71 from paragonie/v2.x-sodium-fix
Backport fix from v3
2 parents e6352b9 + 9cf7450 commit e30811f

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Base64.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static function decode(
230230
}
231231
if ($variant > 0) {
232232
try {
233-
return sodium_base642bin($encodedString, $variant);
233+
return sodium_base642bin(Binary::safeSubstr($encodedString, 0, $srcLen), $variant);
234234
} catch (SodiumException $ex) {
235235
throw new RangeException($ex->getMessage(), $ex->getCode(), $ex);
236236
}

tests/Base64Test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,14 @@ protected function getNextChar(string $c): string
131131
'BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/A'
132132
);
133133
}
134+
135+
public function testStrictPaddingSodium()
136+
{
137+
for ($i = 1; $i < 32; ++$i) {
138+
$random = random_bytes($i);
139+
$encoded = Base64::encode($random);
140+
$decoded = Base64::decode($encoded, true);
141+
$this->assertSame($decoded, $random);
142+
}
143+
}
134144
}

tests/Base64UrlSafeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,14 @@ protected function getNextChar(string $c): string
101101
'BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_A'
102102
);
103103
}
104+
105+
public function testStrictPaddingSodium()
106+
{
107+
for ($i = 1; $i < 32; ++$i) {
108+
$random = random_bytes($i);
109+
$encoded = Base64UrlSafe::encode($random);
110+
$decoded = Base64UrlSafe::decode($encoded, true);
111+
$this->assertSame($decoded, $random);
112+
}
113+
}
104114
}

0 commit comments

Comments
 (0)