-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathCryptoSessionDataTest.php
More file actions
196 lines (167 loc) · 6.8 KB
/
CryptoSessionDataTest.php
File metadata and controls
196 lines (167 loc) · 6.8 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace Test\Session;
use OC\Session\CryptoSessionData;
use OC\Session\Memory;
use OCP\ISession;
use OCP\Security\ICrypto;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Unit tests for CryptoSessionData, verifying encrypted session storage,
* tamper resistance, passphrase boundaries, and round-trip data integrity.
* Covers edge cases and crypto-specific behaviors beyond the base session contract.
*
* Note: ISession API conformity/contract tests are inherited from the parent
* (Test\Session\Session). Only crypto-specific (and pre-wrapper) additions are
* defined here.
*/
#[CoversClass(CryptoSessionData::class)]
#[UsesClass(Memory::class)]
class CryptoSessionDataTest extends Session {
private const DUMMY_PASSPHRASE = 'dummyPassphrase';
private const TAMPERED_BLOB = 'garbage-data';
private const MALFORMED_JSON_BLOB = '{not:valid:json}';
protected ICrypto&MockObject $crypto;
protected ISession $session;
protected function setUp(): void {
parent::setUp();
$this->crypto = $this->createMock(ICrypto::class);
$this->crypto->method('encrypt')->willReturnCallback(
fn ($input) => '#' . $input . '#'
);
$this->crypto->method('decrypt')->willReturnCallback(
fn ($input) => ($input === '' || strlen($input) < 2) ? '' : substr($input, 1, -1)
);
$this->session = new Memory();
$this->instance = new CryptoSessionData($this->session, $this->crypto, self::DUMMY_PASSPHRASE);
}
/**
* Ensure backend never stores plaintext at-rest.
*/
public function testSessionDataStoredEncrypted(): void {
$keyName = 'secret';
$unencryptedValue = 'superSecretValue123';
$this->instance->set($keyName, $unencryptedValue);
$this->instance->close();
$unencryptedSessionDataJson = json_encode(["$keyName" => "$unencryptedValue"]);
$expectedEncryptedSessionDataBlob = $this->crypto->encrypt($unencryptedSessionDataJson, self::DUMMY_PASSPHRASE);
// Retrieve the CryptoSessionData blob directly from lower level session layer to bypass crypto decryption layer
$encryptedSessionDataBlob = $this->session->get('encrypted_session_data'); // should contain raw encrypted blob not the decrypted data
// Definitely encrypted?
$this->assertStringStartsWith('#', $encryptedSessionDataBlob); // Must match stubbed crypto->encrypt()
$this->assertStringEndsWith('#', $encryptedSessionDataBlob); // ditto
$this->assertNotSame($unencryptedSessionDataJson, $expectedEncryptedSessionDataBlob);
$this->assertSame($expectedEncryptedSessionDataBlob, $encryptedSessionDataBlob);
}
/**
* Ensure various key/value types are storable/retrievable
*/
#[DataProvider('roundTripValuesProvider')]
public function testRoundTripValue($key, $value): void {
$this->instance->set($key, $value);
$this->instance->close();
// Simulate reload
$instance2 = new CryptoSessionData($this->session, $this->crypto, self::DUMMY_PASSPHRASE);
$this->assertSame($value, $instance2->get($key));
}
public static function roundTripValuesProvider(): array {
return [
'simple string' => ['foo', 'bar'],
'unicode value' => ['uni', 'héllo 🌍'],
'large value' => ['big', str_repeat('x', 4096)],
'large array' => ['thousand', json_encode(self::makeLargeArray())],
'empty string' => ['', ''],
];
}
/* Helper */
private static function makeLargeArray(int $size = 1000): array {
$result = [];
for ($i = 0; $i < $size; $i++) {
$result["key$i"] = "val$i";
}
return $result;
}
/**
* Ensure removed values are not accessible after flush/reload.
*/
public function testRemovedValueIsGoneAfterClose(): void {
$this->instance->set('temp', 'gone soon');
$this->instance->remove('temp');
$this->instance->close();
$instance2 = new CryptoSessionData($this->session, $this->crypto, self::DUMMY_PASSPHRASE);
$this->assertNull($instance2->get('temp'));
}
/**
* Ensure tampering is handled robustly.
*/
public function testTamperedBlobReturnsNull(): void {
$this->instance->set('foo', 'bar');
$this->instance->close();
// Bypass crypto layer and tamper the lower level blob
$this->session->set('encrypted_session_data', self::TAMPERED_BLOB);
$instance2 = new CryptoSessionData($this->session, $this->crypto, self::DUMMY_PASSPHRASE);
$this->assertNull($instance2->get('foo'));
$this->assertNull($instance2->get('notfoo'));
}
/**
* Ensure malformed JSON is handled robustly.
*/
public function testMalformedJsonBlobReturnsNull(): void {
$this->instance->set('foo', 'bar');
$this->instance->close();
$this->session->set('encrypted_session_data', '#' . self::MALFORMED_JSON_BLOB . '#');
$instance2 = new CryptoSessionData($this->session, $this->crypto, self::DUMMY_PASSPHRASE);
$this->assertNull($instance2->get('foo'));
}
/**
* Ensure an invalid passphrase is handled appropriately.
*/
public function testWrongPassphraseGivesNoAccess(): void {
// Override ICrypto mock/stubs for this test only
$crypto = $this->createPassphraseAwareCryptoMock();
// Override main instance with local ISession and local ICrypto mock/stubs
$session = new Memory();
$instance = new CryptoSessionData($session, $crypto, self::DUMMY_PASSPHRASE);
$instance->set('secure', 'yes');
$instance->close();
$instance2 = new CryptoSessionData($session, $crypto, 'NOT_THE_DUMMY_PASSPHRASE');
$this->assertNull($instance2->get('secure'));
$this->assertFalse($instance2->exists('secure'));
}
/* Helper */
private function createPassphraseAwareCryptoMock(): ICrypto {
$crypto = $this->createMock(ICrypto::class);
$crypto->method('encrypt')->willReturnCallback(function ($plain, $passphrase = null) {
// Set up: store a value with the passphrase embedded (fake encryption)
return $passphrase . '#' . $plain . '#' . $passphrase;
});
$crypto->method('decrypt')->willReturnCallback(function ($input, $passphrase = null) {
// Only successfully decrypt if the embedded passphrase matches
if (str_starts_with($input, $passphrase . '#') && str_ends_with($input, '#' . $passphrase)) {
// Strip off passphrase markers and return the "decrypted" string
return substr($input, strlen($passphrase . '#'), -strlen('#' . $passphrase));
}
// Fail to decrypt
return '';
});
return $crypto;
}
/**
* Ensure closes are idempotent and safe.
*/
public function testDoubleCloseDoesNotCorrupt(): void {
$this->instance->set('safe', 'value');
$this->instance->close();
$blobBefore = $this->session->get('encrypted_session_data');
$this->instance->close(); // Should do nothing harmful
$blobAfter = $this->session->get('encrypted_session_data');
$this->assertSame($blobBefore, $blobAfter);
}
}