|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\Assert; |
|
6
|
|
|
|
|
7
|
|
|
use BadMethodCallException; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Throwable; |
|
10
|
|
|
use Webmozart\Assert\Assert as Webmozart; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Webmozart\Assert wrapper class |
|
14
|
|
|
* |
|
15
|
|
|
* @package simplesamlphp/assert |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Assert extends Webmozart |
|
18
|
|
|
{ |
|
19
|
|
|
private static string $base64_regex = '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $name |
|
23
|
|
|
* @param array $arguments |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function __callStatic($name, $arguments): void |
|
26
|
|
|
{ |
|
27
|
|
|
// Handle Exception-parameter |
|
28
|
|
|
$exception = AssertionFailedException::class; |
|
29
|
|
|
$last = end($arguments); |
|
30
|
|
|
if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) { |
|
31
|
|
|
$exception = $last; |
|
32
|
|
|
|
|
33
|
|
|
array_pop($arguments); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
|
|
call_user_func_array([Webmozart::class, $name], $arguments); |
|
38
|
|
|
return; |
|
39
|
|
|
} catch (InvalidArgumentException $e) { |
|
40
|
|
|
throw new $exception($e->getMessage()); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Note: This test is not bullet-proof but prevents a string containing illegal characters |
|
47
|
|
|
* from being passed and ensures the string roughly follows the correct format for a Base64 encoded string |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $value |
|
50
|
|
|
* @param string $message |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function stringPlausibleBase64(string $value, $message = ''): void |
|
53
|
|
|
{ |
|
54
|
|
|
$result = true; |
|
55
|
|
|
|
|
56
|
|
|
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$base64_regex]]) === false) { |
|
57
|
|
|
$result = false; |
|
58
|
|
|
} else { |
|
59
|
|
|
$decoded = base64_decode($value, true); |
|
60
|
|
|
if ($decoded === false) { |
|
61
|
|
|
$result = false; |
|
62
|
|
|
} elseif (base64_encode($decoded) !== $value) { |
|
63
|
|
|
$result = false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($result === false) { |
|
68
|
|
|
throw new AssertionFailedException( |
|
69
|
|
|
sprintf( |
|
70
|
|
|
$message ?: '%s is not a valid Base64 encoded string', |
|
71
|
|
|
call_user_func_array( |
|
72
|
|
|
[Webmozart::class, 'typeToString'], |
|
73
|
|
|
[is_object($value) ? get_class($value) : gettype($value)] |
|
|
|
|
|
|
74
|
|
|
) |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|