From 981ea06d85f53ef092f3a0c46305774471ea9a4c Mon Sep 17 00:00:00 2001 From: marquis-wkhtmltopdf <358259913+marquis-wkhtmltopdf@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:57:50 +0530 Subject: [PATCH] Add a security warning for from_string() from_string() reads tags as wkhtmltopdf options (CVE-2025-26240). Add a Security section to the README and warnings to the docstrings. Closes #267 --- README.rst | 22 ++++++++++++++++++++++ pdfkit/api.py | 8 ++++++++ pdfkit/pdfkit.py | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 2822f0c..549d3e7 100644 --- a/README.rst +++ b/README.rst @@ -17,6 +17,28 @@ Deprecation Warning This library has been deprecated to match the `wkhtmltopdf project status `_. +Security +-------- + +**Warning!** Never pass untrusted HTML, such as HTML from users, to ``from_string()``. It reads ```` tags in the HTML as ``wkhtmltopdf`` options, so whoever writes the HTML can set any option, including ones that read files or run JavaScript (`CVE-2025-26240 `_). + +To convert untrusted HTML: + +* Use ``from_file()``, which ignores meta tags, and turn off JavaScript and local file access, as below. +* Never add ``--enable-local-file-access`` or ``--allow``, and never put user input in ``options``, ``toc``, ``cover`` or ``css``. +* Run ``wkhtmltopdf`` where it cannot reach your secrets or internal network. The HTML can still load images and pages from any address. + +.. code-block:: python + + import io + + options = { + 'disable-javascript': None, + 'disable-local-file-access': None + } + + pdfkit.from_file(io.StringIO(untrusted_html), 'out.pdf', options=options) + Installation ------------ diff --git a/pdfkit/api.py b/pdfkit/api.py index 86d9656..3c5b677 100644 --- a/pdfkit/api.py +++ b/pdfkit/api.py @@ -9,6 +9,9 @@ def from_url(url, output_path=None, options=None, toc=None, cover=None, """ Convert file of files from URLs to PDF document + Warning: never pass untrusted URLs. wkhtmltopdf also opens file:// URLs and addresses + on your internal network. See Security in the README. + :param url: URL or list of URLs to be saved :param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable. :param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--' @@ -32,6 +35,8 @@ def from_file(input, output_path=None, options=None, toc=None, cover=None, css=N """ Convert HTML file or files to PDF document + Unlike from_string(), this ignores tags. See Security in the README. + :param input: path to HTML file or list with paths or file-like object :param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable. :param options: (optional) dict with wkhtmltopdf options, with or w/o '--' @@ -56,6 +61,9 @@ def from_string(input, output_path=None, options=None, toc=None, cover=None, css """ Convert given string or strings to PDF document + Warning: never pass untrusted HTML. Its tags become wkhtmltopdf + options. Use from_file() instead. See Security in the README. + :param input: string with a desired text. Could be a raw text or a html file :param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable. :param options: (optional) dict with wkhtmltopdf options, with or w/o '--' diff --git a/pdfkit/pdfkit.py b/pdfkit/pdfkit.py index 88b3c92..ce018e8 100644 --- a/pdfkit/pdfkit.py +++ b/pdfkit/pdfkit.py @@ -15,7 +15,8 @@ class PDFKit(object): :param url_or_file: str - either a URL, a path to a file or a string containing HTML to convert - :param type_: str - either 'url', 'file' or 'string' + :param type_: str - either 'url', 'file' or 'string'. Never use 'string' with untrusted + HTML (see from_string) :param options: dict (optional) with wkhtmltopdf options, with or w/o '--' :param toc: dict (optional) - toc-specific wkhtmltopdf options, with or w/o '--' :param cover: str (optional) - url/filename with a cover html page