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
|
<?php
use PHPUnit\Framework\TestCase;
final class CsrfMiddlewareTest extends ApiTestCase
{
// ── Ignored operations — no CSRF check needed ────────────────────────────
public function test_ignored_op_list_without_csrf(): void
{
// Login to establish session (list is ignored, so no CSRF check)
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
// list is in csrfIgnore — should work without csrf_token
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'list',
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('books', $body);
}
// ── Non-ignored operations — CSRF required ───────────────────────────────
public function test_non_ignored_op_requires_csrf(): void
{
// Login to establish session
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
// togglefav is NOT in csrfIgnore — should require csrf_token
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'togglefav',
'id' => 1,
'csrf_token' => '',
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
$this->assertStringContainsString('CSRF', $body['error'] ?? '');
}
public function test_non_ignored_op_with_valid_csrf(): void
{
// Login to establish session and get csrf_token
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$csrfToken = $body['csrf_token'] ?? '';
$this->assertNotEmpty($csrfToken);
// togglefav with valid csrf_token should succeed
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'togglefav',
'id' => 1,
'csrf_token' => $csrfToken,
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('id', $body);
}
public function test_non_ignored_op_with_invalid_csrf(): void
{
// Login to establish session
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
// togglefav with wrong csrf_token should fail
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'togglefav',
'id' => 1,
'csrf_token' => 'invalid_token_12345',
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
$this->assertStringContainsString('CSRF', $body['error'] ?? '');
}
// ── Unauthenticated — no CSRF check ─────────────────────────────────────
public function test_unauthenticated_no_csrf_check(): void
{
// Without a session, CSRF middleware should skip its check
// and let the handler deal with auth
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'togglefav',
'id' => 1,
]);
$this->assertEquals(401, $status);
// Error should be about authentication, not CSRF
$this->assertArrayHasKey('error', $body);
$this->assertStringNotContainsString('CSRF', $body['error'] ?? '');
}
}
|