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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
**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-03-09 - [CRITICAL] Remote Code Execution (RCE) via LaTeX Compilation
**Vulnerability:** Untrusted LaTeX compilation commands (`pdflatex` and `pandoc` using `xelatex`) were executed via `subprocess.Popen` without the `-no-shell-escape` flag. This allows malicious LaTeX documents to execute arbitrary system commands via `\write18{}` or similar mechanisms during PDF generation.
**Learning:** All PDF compilation tools, whether `pdflatex` directly or `pandoc` using a LaTeX engine, must be explicitly sandboxed to prevent command injection, even when the input source is semi-trusted or dynamically generated (e.g., from an LLM). Process execution should also enforce strict timeouts to prevent Denial of Service (DoS) from infinite compilation loops.
**Prevention:** Always enforce `-no-shell-escape` directly in `pdflatex` or via `--pdf-engine-opt=-no-shell-escape` in `pandoc`. Always wrap `subprocess.Popen().communicate()` calls with a strict `timeout` argument and implement proper cleanup (`process.kill()`) for `TimeoutExpired` exceptions to avoid zombie processes.
23 changes: 19 additions & 4 deletions cli/generators/cover_letter_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,12 +771,16 @@ 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()
if process.returncode == 0 or output_path.exists():
pdf_created = True
except (subprocess.CalledProcessError, FileNotFoundError):
Expand All @@ -787,11 +791,22 @@ 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()
if process.returncode == 0 or output_path.exists():
pdf_created = True
except (subprocess.CalledProcessError, FileNotFoundError):
Expand Down
35 changes: 29 additions & 6 deletions cli/pdf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ 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()

if process.returncode == 0 or output_path.exists():
return True
Expand Down Expand Up @@ -121,12 +125,23 @@ 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()

if process.returncode == 0 or output_path.exists():
return True
Expand All @@ -148,7 +163,11 @@ def is_pdflatex_available(self) -> bool:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
process.communicate()
try:
process.communicate(timeout=10)
except subprocess.TimeoutExpired:
process.kill()
process.communicate()
return process.returncode == 0
except FileNotFoundError:
return False
Expand All @@ -166,7 +185,11 @@ def is_pandoc_available(self) -> bool:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
process.communicate()
try:
process.communicate(timeout=10)
except subprocess.TimeoutExpired:
process.kill()
process.communicate()
return process.returncode == 0
except FileNotFoundError:
return False
Expand Down
Loading