about summary refs log tree commit diff stats
path: root/classes/CsrfMiddleware.php
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-24 17:22:36 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-24 17:22:36 +0300
commitf8cd8e746f9d02b8fb009ed25ba87656f71d093a (patch)
tree744c6391f72224960b615e0ee72fe0cace68b89d /classes/CsrfMiddleware.php
parent6f660eedb640eaa31e75cfd2543692d7f494ddd6 (diff)
downloadthe-epube-f8cd8e746f9d02b8fb009ed25ba87656f71d093a.tar.gz
the-epube-f8cd8e746f9d02b8fb009ed25ba87656f71d093a.tar.xz
give AuthMiddleware more appropriate name, update phpstan config
Diffstat (limited to 'classes/CsrfMiddleware.php')
-rw-r--r--classes/CsrfMiddleware.php30
1 files changed, 30 insertions, 0 deletions
diff --git a/classes/CsrfMiddleware.php b/classes/CsrfMiddleware.php
new file mode 100644
index 0000000..8510766
--- /dev/null
+++ b/classes/CsrfMiddleware.php
@@ -0,0 +1,30 @@
+<?php
+
+class CsrfMiddleware implements MiddlewareInterface
+{
+    /** @var array<string> */
+    private array $csrfIgnore = [
+        "cover", "download", "getpagination", "getlastread"
+    ];
+
+    public function process(Request $request, callable $next): Response
+    {
+        if (empty($_SESSION['owner'])) {
+            return $next($request);
+        }
+
+        $op = $request->getAttribute('op');
+
+        $csrfToken = $request->getAttribute('csrf_token', '');
+
+        if (!in_array($op, $this->csrfIgnore) && !validate_csrf($csrfToken)) {
+            $response = (new Response())
+                ->withStatus(401)
+                ->withBody("Unauthorized (CSRF)");
+            $response->send();
+            exit;
+        }
+
+        return $next($request);
+    }
+}