about summary refs log tree commit diff stats
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-10-14 21:40:28 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-10-14 21:40:28 +0300
commit48d7b08a87adec0ce1c114bd5c70c0f6e108a954 (patch)
tree906546542b5df2689ee6a0c16b889df82f947580 /classes
parenta5652f406c6a1fdadb40f65d56e6141800103f61 (diff)
downloadthe-epube-48d7b08a87adec0ce1c114bd5c70c0f6e108a954.tar.gz
the-epube-48d7b08a87adec0ce1c114bd5c70c0f6e108a954.tar.xz
prevent storing pagination for nonexistant books
Diffstat (limited to 'classes')
-rw-r--r--classes/Handler/PaginationHandler.php18
1 files changed, 17 insertions, 1 deletions
diff --git a/classes/Handler/PaginationHandler.php b/classes/Handler/PaginationHandler.php
index 03388c9..de22037 100644
--- a/classes/Handler/PaginationHandler.php
+++ b/classes/Handler/PaginationHandler.php
@@ -3,7 +3,9 @@
 namespace Handler;
 
 use AuthRequired;
+use Config;
 use ORM;
+use PDO;
 use Request;
 use RequestHandlerInterface;
 use Response;
@@ -16,6 +18,20 @@ class PaginationHandler implements RequestHandlerInterface, AuthRequired
      * 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) {
@@ -51,7 +67,7 @@ class PaginationHandler implements RequestHandlerInterface, AuthRequired
             $payload = null;
         }
 
-        if ($bookid && $payload) {
+        if ($bookid && $payload && $this->bookExists($bookid)) {
             $pag = ORM::for_table('epube_pagination')
                 ->where('bookid', $bookid)
                 ->find_one();