Skip to content

Commit 3eb46a1

Browse files
authored
Merge pull request #12 from Pudge601/v2.x
Throw InvalidArgumentException when decoding nonsense data
2 parents c0a06e4 + 47d00d0 commit 3eb46a1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Base64Url.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ public static function encode(string $data, bool $usePadding = false): string
3434
/**
3535
* @param string $data The data to decode
3636
*
37+
* @throws \InvalidArgumentException
38+
*
3739
* @return string The data decoded
3840
*/
3941
public static function decode(string $data): string
4042
{
41-
return \base64_decode(\strtr($data, '-_', '+/'), true);
43+
$decoded = \base64_decode(\strtr($data, '-_', '+/'), true);
44+
if ($decoded === false) {
45+
throw new \InvalidArgumentException('Invalid data provided');
46+
}
47+
48+
return $decoded;
4249
}
4350
}

tests/Base64UrlTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,24 @@ public function getTestBadVectors(): array
109109
],
110110
];
111111
}
112+
113+
/**
114+
* @dataProvider getTestNonsenseVectors
115+
*
116+
* @test
117+
*/
118+
public function nonsenseInput(string $input): void
119+
{
120+
static::expectException(\InvalidArgumentException::class);
121+
Base64Url::decode($input);
122+
}
123+
124+
public function getTestNonsenseVectors(): array
125+
{
126+
return [
127+
[
128+
'cxr0fdsezrewklerewxoz423ocfsa3bw432yjydsa9lhdsalw',
129+
],
130+
];
131+
}
112132
}

0 commit comments

Comments
 (0)