Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

## 2026-05-20 - [Critical] LaTeX RCE and DoS in PDF Generation
**Vulnerability:** The `PDFConverter` (`cli/pdf/converter.py`) and `CoverLetterGenerator` (`cli/generators/cover_letter_generator.py`) modules omitted the `-no-shell-escape` flag when invoking `pdflatex` and `pandoc`, allowing LaTeX templates to execute arbitrary shell commands via the `\write18` primitive (RCE). Additionally, the `subprocess.communicate()` calls lacked a strict timeout, making the application vulnerable to Denial of Service (DoS) attacks via infinite loops in malicious or buggy LaTeX code.
**Learning:** Security parameters and protective mechanisms (like `-no-shell-escape` and timeouts) must be applied uniformly across all code paths executing external tools. Even if one module (`TemplateGenerator`) is secure, duplicated or refactored logic elsewhere can easily drop these critical safeguards, introducing severe vulnerabilities.
**Prevention:** Always enforce the `-no-shell-escape` flag for `pdflatex` (directly) and `pandoc` (via `--pdf-engine-opt=-no-shell-escape`). Implement strict subprocess timeouts with explicit process cleanup (`process.kill()` and a second `communicate()`) to prevent zombie processes and DoS. Ensure these protections are centralized or rigidly duplicated across all PDF generation implementations.
27 changes: 23 additions & 4 deletions cli/generators/cover_letter_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
return False

if process.returncode == 0 or output_path.exists():
pdf_created = True
except (subprocess.CalledProcessError, FileNotFoundError):
Expand All @@ -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()
return False

if process.returncode == 0 or output_path.exists():
pdf_created = True
except (subprocess.CalledProcessError, FileNotFoundError):
Expand Down
25 changes: 21 additions & 4 deletions cli/pdf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
return False

if process.returncode == 0 or output_path.exists():
return True
Expand Down Expand Up @@ -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()
return False

if process.returncode == 0 or output_path.exists():
return True
Expand Down
Loading