Repository navigation
Expand file tree
/
Copy pathclass-wp-secrets-keyring-conformance.php
More file actions
117 lines (96 loc) · 4.73 KB
/
Copy pathclass-wp-secrets-keyring-conformance.php
File metadata and controls
117 lines (96 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Conformance suite for WP_Secrets_Keyring implementations.
*
* Extend this, return a keyring from keyring(), and the contract every keyring is
* held to gets checked against it. "extends WP_Secrets_Keyring" is a claim PHP
* can verify about method names and nothing else – it cannot tell you that
* wrap() is non-deterministic, that unwrap() rejects tampering instead of
* silently returning garbage bytes, or that failure is reported as WP_Error
* rather than an exception or a falsy string. Those are the properties that
* actually matter to WP_Secrets_Key_Manager, the only caller of a keyring.
*
* @package WordPress
* @subpackage Secrets
*/
abstract class WP_Secrets_Keyring_Conformance extends WP_UnitTestCase {
/**
* The keyring under test. Fresh per test.
*
* @return WP_Secrets_Keyring
*/
abstract protected function keyring();
/**
* Wrap() followed by unwrap() has to return exactly the bytes that went in –
* WP_Secrets_Key_Manager derives the root key from this round trip, so any
* corruption here is a corrupted root key for every secret on the site.
*/
public function test_wrap_returns_a_non_empty_string_that_unwraps_to_the_same_bytes(): void {
$keyring = $this->keyring();
$key_material = random_bytes( 32 );
$wrapped = $keyring->wrap( $key_material );
$this->assertIsString( $wrapped, 'wrap() should return a string.' );
$this->assertNotSame( '', $wrapped, 'wrap() should not return an empty string.' );
$unwrapped = $keyring->unwrap( $wrapped );
$this->assertSame( $key_material, $unwrapped, 'unwrap() should return the original key material.' );
}
/**
* A deterministic wrap() leaks, via ciphertext comparison, whether two wrapped
* values protect the same key material – something nothing outside the
* keyring is entitled to learn. A fresh nonce (or equivalent) per call is what
* WP_Secrets_Key_Manager relies on to keep that comparison unavailable.
*/
public function test_two_wraps_of_the_same_bytes_return_different_strings(): void {
$keyring = $this->keyring();
$key_material = random_bytes( 32 );
$first = $keyring->wrap( $key_material );
$second = $keyring->wrap( $key_material );
$this->assertNotSame( $first, $second, 'Two wraps of the same key material should return different strings.' );
}
/**
* Unwrap() never throws and never returns a plausible-looking string for input
* it did not produce – WP_Secrets_Key_Manager treats anything other than
* WP_Error as usable key material, so a keyring that returns garbage bytes on
* garbage input hands a wrong root key downstream instead of failing.
*/
public function test_unwrap_of_garbage_is_a_wp_error(): void {
$keyring = $this->keyring();
$garbage = 'garbage-' . bin2hex( random_bytes( 16 ) );
$this->assertWPError( $keyring->unwrap( $garbage ), 'Unwrapping garbage should return a WP_Error.' );
}
/**
* A truncated wrapped value must fail closed rather than decode to a short,
* wrong key – WP_Secrets_Key_Manager has no way to tell a merely-short key
* from a correctly-derived one except by trusting unwrap()'s success.
*/
public function test_unwrap_of_a_truncated_value_is_a_wp_error(): void {
$keyring = $this->keyring();
$wrapped = $keyring->wrap( random_bytes( 32 ) );
$this->assertIsString( $wrapped, 'wrap() should return a string.' );
$truncated = substr( $wrapped, 0, intdiv( strlen( $wrapped ), 2 ) );
$this->assertWPError( $keyring->unwrap( $truncated ), 'Unwrapping a truncated value should return a WP_Error.' );
}
/**
* A single flipped bit anywhere in the wrapped value has to be caught –
* that is the entire point of authenticated wrapping. Silently accepting it
* would let a corrupted or tampered wrapped root key through as genuine.
*/
public function test_unwrap_of_a_value_with_one_flipped_byte_is_a_wp_error(): void {
$keyring = $this->keyring();
$wrapped = $keyring->wrap( random_bytes( 32 ) );
$this->assertIsString( $wrapped, 'wrap() should return a string.' );
$flip_at = intdiv( strlen( $wrapped ), 2 );
$flipped = $wrapped;
$flipped[ $flip_at ] = chr( ord( $wrapped[ $flip_at ] ) ^ 0x01 );
$this->assertWPError( $keyring->unwrap( $flipped ), 'Unwrapping a value with one flipped byte should return a WP_Error.' );
}
/**
* Site Health renders get_key_source() directly; an empty or non-string value
* there is a blank line on a diagnostics page an operator is depending on.
*/
public function test_get_key_source_returns_a_non_empty_string(): void {
$source = $this->keyring()->get_key_source();
$this->assertIsString( $source, 'get_key_source() should return a string.' );
$this->assertNotSame( '', trim( $source ), 'get_key_source() should not return an empty or whitespace-only string.' );
}
}