about summary refs log tree commit diff stats
path: root/classes/Handler/PaginationHandler.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/Handler/PaginationHandler.php')
-rw-r--r--classes/Handler/PaginationHandler.php18
1 files changed, 18 insertions, 0 deletions
diff --git a/classes/Handler/PaginationHandler.php b/classes/Handler/PaginationHandler.php
index 3c0555b..3265440 100644
--- a/classes/Handler/PaginationHandler.php
+++ b/classes/Handler/PaginationHandler.php
@@ -10,6 +10,8 @@ use Response;
 
 class PaginationHandler implements RequestHandlerInterface, AuthRequired
 {
+    /** Upper bound for stored location payloads (epub.js location arrays can be large). */
+    private const MAX_PAYLOAD_SIZE = 2 * 1024 * 1024;
     private function handleGet(int $bookid): Response
     {
         if ($bookid) {
@@ -29,6 +31,22 @@ class PaginationHandler implements RequestHandlerInterface, AuthRequired
 
     private function handleStore(int $bookid, ?string $payload): Response
     {
+        // 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) {
             $pag = ORM::for_table('epube_pagination')
                 ->where('bookid', $bookid)