1010
1111use APCUIterator ;
1212use RobiNN \Pca \Config ;
13+ use RobiNN \Pca \Csrf ;
1314use RobiNN \Pca \Format ;
1415use RobiNN \Pca \Helpers ;
1516use RobiNN \Pca \Http ;
@@ -112,9 +113,13 @@ private function viewKey(): string {
112113 );
113114 }
114115
115- if (isset ($ _GET ['delete ' ])) {
116- apcu_delete ($ key );
117- Http::redirect ();
116+ if (isset ($ _POST ['delete ' ])) {
117+ if (!Csrf::validateToken (Http::post ('csrf_token ' , '' ))) {
118+ Helpers::alert ($ this ->template , 'Invalid CSRF token. ' , 'error ' );
119+ } else {
120+ apcu_delete ($ key );
121+ Http::redirect ();
122+ }
118123 }
119124
120125 [$ formatted_value , $ encode_fn , $ is_formatted ] = Value::format ($ value );
@@ -128,7 +133,6 @@ private function viewKey(): string {
128133 'formatted ' => $ is_formatted ,
129134 'edit_url ' => Http::queryString (['ttl ' ], ['form ' => 'edit ' , 'key ' => $ key ]),
130135 'export_url ' => Http::queryString (['ttl ' , 'view ' , 'p ' , 'key ' ], ['export ' => 'key ' ]),
131- 'delete_url ' => Http::queryString (['view ' ], ['delete ' => 'key ' , 'key ' => $ key ]),
132136 ]);
133137 }
134138
@@ -164,7 +168,11 @@ private function form(): string {
164168 }
165169
166170 if (isset ($ _POST ['submit ' ])) {
167- $ this ->saveKey ();
171+ if (Csrf::validateToken (Http::post ('csrf_token ' , '' ))) {
172+ $ this ->saveKey ();
173+ } else {
174+ Helpers::alert ($ this ->template , 'Invalid CSRF token. ' , 'error ' );
175+ }
168176 }
169177
170178 $ value = Value::converter ($ value , $ encoder , 'view ' );
@@ -297,12 +305,16 @@ public function keysTreeView(array $keys): array {
297305
298306 private function mainDashboard (): string {
299307 if (isset ($ _POST ['submit_import_key ' ])) {
300- Helpers::import (
301- static fn (string $ key ): bool => apcu_exists ($ key ),
302- static function (string $ key , string $ value , int $ ttl ): bool {
303- return apcu_store ($ key , unserialize (base64_decode ($ value ), ['allowed_classes ' => false ]), $ ttl );
304- }
305- );
308+ if (Csrf::validateToken (Http::post ('csrf_token ' , '' ))) {
309+ Helpers::import (
310+ static fn (string $ key ): bool => apcu_exists ($ key ),
311+ static function (string $ key , string $ value , int $ ttl ): bool {
312+ return apcu_store ($ key , unserialize (base64_decode ($ value ), ['allowed_classes ' => false ]), $ ttl );
313+ }
314+ );
315+ } else {
316+ echo Helpers::alert ($ this ->template , 'Invalid CSRF token. ' , 'error ' );
317+ }
306318 }
307319
308320 $ keys = $ this ->getAllKeys ();
0 commit comments