about summary refs log tree commit diff stats
path: root/classes/Handler/PaginationHandler.php
diff options
context:
space:
mode:
authorfox <fox@fakecake.org>2026-10-14 21:41:11 +0300
committerGitLab <noreply@fakecake.org>2026-10-14 21:41:11 +0300
commitfa0e418228ec4ff79ef4b2024beb2491e15d538e (patch)
tree906546542b5df2689ee6a0c16b889df82f947580 /classes/Handler/PaginationHandler.php
parentc0135cc03916ad22707064e8957dce3f8e067a5d (diff)
parent48d7b08a87adec0ce1c114bd5c70c0f6e108a954 (diff)
downloadthe-epube-fa0e418228ec4ff79ef4b2024beb2491e15d538e.tar.gz
the-epube-fa0e418228ec4ff79ef4b2024beb2491e15d538e.tar.xz
Merge branch 'dev' into 'master'
Dev

See merge request git/the-epube!77
Diffstat (limited to 'classes/Handler/PaginationHandler.php')
-rw-r--r--classes/Handler/PaginationHandler.php40
1 files changed, 39 insertions, 1 deletions
diff --git a/classes/Handler/PaginationHandler.php b/classes/Handler/PaginationHandler.php
index 3c0555b..de22037 100644
--- a/classes/Handler/PaginationHandler.php
+++ b/classes/Handler/PaginationHandler.php
@@ -3,13 +3,35 @@
 namespace Handler;
 
 use AuthRequired;
+use Config;
 use ORM;
+use PDO;
 use Request;
 use RequestHandlerInterface;
 use Response;
 
 class PaginationHandler implements RequestHandlerInterface, AuthRequired
 {
+    /**
+     * Upper bound for stored location payloads. Observed real-world max in
+     * production is ~3 MB for large books; 16 MiB keeps generous headroom
+     * while still bounding unbounded storage growth.
+     */
+    private const MAX_PAYLOAD_SIZE = 16 * 1024 * 1024;
+
+    /**
+     * Verify the id refers to an actual library entry in the Calibre DB.
+     * The frontend keys pagination by the EPUB 'data' row id, but accept
+     * a 'books' row id as well for robustness/backwards compatibility.
+     */
+    private function bookExists(int $bookid): bool
+    {
+        $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB));
+        $sth = $caldb->prepare("SELECT 1 FROM data WHERE id = ? UNION SELECT 1 FROM books WHERE id = ? LIMIT 1");
+        $sth->execute([$bookid, $bookid]);
+        return (bool)$sth->fetch();
+    }
+
     private function handleGet(int $bookid): Response
     {
         if ($bookid) {
@@ -29,7 +51,23 @@ class PaginationHandler implements RequestHandlerInterface, AuthRequired
 
     private function handleStore(int $bookid, ?string $payload): Response
     {
-        if ($bookid && $payload) {
+        // Payload is later served back verbatim with a JSON content type,
+        // so only accept well-formed JSON arrays (epub.js location format)
+        // instead of storing arbitrary attacker-controlled bytes.
+        if ($payload !== null && strlen($payload) <= self::MAX_PAYLOAD_SIZE) {
+            $decoded = json_decode($payload, true);
+            if (is_array($decoded)) {
+                // Normalize (strips surrounding whitespace etc.) while keeping
+                // the round-trip as byte-faithful as possible.
+                $payload = json_encode($decoded, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
+            } else {
+                $payload = null;
+            }
+        } else {
+            $payload = null;
+        }
+
+        if ($bookid && $payload && $this->bookExists($bookid)) {
             $pag = ORM::for_table('epube_pagination')
                 ->where('bookid', $bookid)
                 ->find_one();