From c9ecc8a76d25088998ab9287acbf57ea64c00e99 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 23 May 2026 23:57:24 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITICAL]?= =?UTF-8?q?=20Fix=20Remote=20Code=20Execution=20in=20PDF=20Compilers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses a critical RCE vulnerability where `pdflatex` and `pandoc` were invoked without strictly restricting shell execution or applying timeout bounds. A maliciously crafted input could inject commands using `\write18` to execute arbitrary shell commands on the host machine. The fix enforces the `-no-shell-escape` flag for `pdflatex` and `--pdf-engine-opt=-no-shell-escape` for `pandoc`, and implements a 30-second subprocess timeout with proper process cleanup (`process.kill()`) to prevent infinite compilation loops (DoS). Co-authored-by: anchapin <6326294+anchapin@users.noreply.github.com> --- .jules/sentinel.md | 5 +++++ cli/generators/cover_letter_generator.py | 27 ++++++++++++++++++++---- cli/pdf/converter.py | 25 ++++++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 3a1d237..0bbde00 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -7,3 +7,8 @@ **Vulnerability:** The `CoverLetterGenerator` used a standard Jinja2 environment (intended for HTML/XML or plain text) to render LaTeX templates. This allowed malicious user input (or AI hallucinations) containing LaTeX control characters (e.g., `\input{...}`) to be injected directly into the LaTeX source, leading to potential Local File Inclusion (LFI) or other exploits. **Learning:** Jinja2's default `autoescape` is context-aware based on file extensions, but usually only for HTML/XML. It does NOT automatically escape LaTeX special characters. Relying on manual filters (like `| latex_escape`) in templates is error-prone and brittle, as developers might forget to apply them to every variable. **Prevention:** Always use a dedicated Jinja2 environment for LaTeX generation that enforces auto-escaping via a `finalize` hook (e.g., `tex_env.finalize = latex_escape`). This ensures *all* variable output is sanitized by default, providing defense-in-depth even if the template author forgets explicit filters. + +## 2025-02-23 - [Critical] Remote Code Execution (RCE) via LaTeX Compilation +**Vulnerability:** The application was invoking `pdflatex` (and `pandoc` with `--pdf-engine=xelatex`) without strictly restricting shell execution or applying timeout bounds. A maliciously crafted (or AI-hallucinated) job description or resume element could inject `\write18{command}` or `\immediate\write18{command}` to execute arbitrary shell commands on the host machine. The lack of a timeout also exposed the system to Denial of Service (DoS) attacks via infinite compilation loops. +**Learning:** Tools like `pdflatex` can execute arbitrary commands on the system by default unless specifically constrained. You cannot rely on input sanitization or regex filters alone to prevent this. +**Prevention:** Always enforce the `-no-shell-escape` flag for `pdflatex` (and `--pdf-engine-opt=-no-shell-escape` for `pandoc`) to disable `\write18` execution. Additionally, implement explicit subprocess timeouts (`process.communicate(timeout=30)`) with proper cleanup (`process.kill()`) to prevent unbounded execution and resource starvation. diff --git a/cli/generators/cover_letter_generator.py b/cli/generators/cover_letter_generator.py index aaf0b61..6b93e9e 100644 --- a/cli/generators/cover_letter_generator.py +++ b/cli/generators/cover_letter_generator.py @@ -771,12 +771,18 @@ def _compile_pdf(self, output_path: Path, tex_content: str) -> bool: try: # Use Popen with explicit cleanup to avoid double-free issues process = subprocess.Popen( - ["pdflatex", "-interaction=nonstopmode", tex_path.name], + ["pdflatex", "-interaction=nonstopmode", "-no-shell-escape", tex_path.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=tex_path.parent, ) - stdout, stderr = process.communicate() + try: + stdout, stderr = process.communicate(timeout=30) + except subprocess.TimeoutExpired: + process.kill() + stdout, stderr = process.communicate() + raise RuntimeError("PDF compilation timed out") + if process.returncode == 0 or output_path.exists(): pdf_created = True except (subprocess.CalledProcessError, FileNotFoundError): @@ -787,11 +793,24 @@ def _compile_pdf(self, output_path: Path, tex_content: str) -> bool: # Fallback to pandoc try: process = subprocess.Popen( - ["pandoc", str(tex_path), "-o", str(output_path), "--pdf-engine=xelatex"], + [ + "pandoc", + str(tex_path), + "-o", + str(output_path), + "--pdf-engine=xelatex", + "--pdf-engine-opt=-no-shell-escape", + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) - stdout, stderr = process.communicate() + try: + stdout, stderr = process.communicate(timeout=30) + except subprocess.TimeoutExpired: + process.kill() + stdout, stderr = process.communicate() + raise RuntimeError("PDF compilation timed out") + if process.returncode == 0 or output_path.exists(): pdf_created = True except (subprocess.CalledProcessError, FileNotFoundError): diff --git a/cli/pdf/converter.py b/cli/pdf/converter.py index 0b0a200..d78222e 100644 --- a/cli/pdf/converter.py +++ b/cli/pdf/converter.py @@ -86,12 +86,17 @@ def _compile_pdflatex( """ try: process = subprocess.Popen( - ["pdflatex", "-interaction=nonstopmode", tex_path.name], + ["pdflatex", "-interaction=nonstopmode", "-no-shell-escape", tex_path.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=working_dir, ) - stdout, stderr = process.communicate() + try: + stdout, stderr = process.communicate(timeout=30) + except subprocess.TimeoutExpired: + process.kill() + stdout, stderr = process.communicate() + raise RuntimeError("PDF compilation timed out") if process.returncode == 0 or output_path.exists(): return True @@ -121,12 +126,24 @@ def _compile_pandoc( """ try: process = subprocess.Popen( - ["pandoc", str(tex_path), "-o", str(output_path), "--pdf-engine=xelatex"], + [ + "pandoc", + str(tex_path), + "-o", + str(output_path), + "--pdf-engine=xelatex", + "--pdf-engine-opt=-no-shell-escape", + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=working_dir, ) - stdout, stderr = process.communicate() + try: + stdout, stderr = process.communicate(timeout=30) + except subprocess.TimeoutExpired: + process.kill() + stdout, stderr = process.communicate() + raise RuntimeError("PDF compilation timed out") if process.returncode == 0 or output_path.exists(): return True