diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-10-14 21:30:09 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-10-14 21:30:09 +0300 |
| commit | c39abf75db7a40514ac3f17900e106ad40a29451 (patch) | |
| tree | 2126470890c6537604f92b6582edc58816f13b09 /classes | |
| parent | 8501985a97a6775f8a88fdf91e93c3382d7ae9ac (diff) | |
| download | the-epube-c39abf75db7a40514ac3f17900e106ad40a29451.tar.gz the-epube-c39abf75db7a40514ac3f17900e106ad40a29451.tar.xz | |
fix content-type of getpagination and prevent sniffing; only accept valid JSON data when saving
Diffstat (limited to 'classes')
| -rw-r--r-- | classes/Handler/PaginationHandler.php | 18 | ||||
| -rw-r--r-- | classes/Response.php | 4 |
2 files changed, 21 insertions, 1 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) diff --git a/classes/Response.php b/classes/Response.php index ea5e93d..e45814b 100644 --- a/classes/Response.php +++ b/classes/Response.php @@ -6,7 +6,7 @@ class Response /** @var array<string, string> */ private array $headers = []; private string $body = ''; - private string $contentType = "text/json"; + private string $contentType = "application/json"; public function withStatus(int $code, string $reasonPhrase = ''): self { @@ -50,6 +50,8 @@ class Response if (!$contentTypeFound) header("Content-type: {$this->contentType}"); + header("X-Content-Type-Options: nosniff"); + http_response_code($this->status); echo $this->body; } |
