Skip to content

🛡️ Sentinel: [CRITICAL] Fix LaTeX Subprocess RCE and DoS#350

Open
anchapin wants to merge 1 commit into
mainfrom
sentinel/fix-pdflatex-rce-and-dos-mitigation-2493027765909998490
Open

🛡️ Sentinel: [CRITICAL] Fix LaTeX Subprocess RCE and DoS#350
anchapin wants to merge 1 commit into
mainfrom
sentinel/fix-pdflatex-rce-and-dos-mitigation-2493027765909998490

Conversation

@anchapin

@anchapin anchapin commented Jun 10, 2026

Copy link
Copy Markdown
Owner

🚨 Severity: CRITICAL
💡 Vulnerability: The PDFConverter class executed pdflatex and pandoc without the -no-shell-escape flags, allowing potential remote code execution (RCE) via \write18{} macros if an attacker controlled the LaTeX input. Additionally, the compilation subprocess did not have a timeout, allowing malicious LaTeX input (e.g. infinite loops via \def) to cause Denial of Service (DoS) by hanging the application thread indefinitely.
🎯 Impact: An attacker who can influence the LaTeX template could execute arbitrary commands on the server running the application (RCE). They could also hang the application indefinitely by providing inputs that cause the LaTeX compiler to spin in an infinite loop (DoS).
🔧 Fix: Enforced the -no-shell-escape flag for pdflatex directly and for pandoc via --pdf-engine-opt=-no-shell-escape. Also enforced a strict 30-second timeout on the subprocess.communicate() calls, accompanied by proper zombie process reaping (process.kill() followed by communicate()).
✅ Verification: Ensure tests pass locally by running python -m pytest tests/test_pdf_security.py. Review the cli/pdf/converter.py file to verify the subprocess parameters and exception handling blocks.


PR created automatically by Jules for task 2493027765909998490 started by @anchapin

Summary by Sourcery

Harden LaTeX-based PDF generation to prevent RCE and DoS via unsafe subprocess execution.

Bug Fixes:

  • Disable LaTeX shell command execution by adding no-shell-escape flags to pdflatex and pandoc PDF compilation commands in PDFConverter.
  • Prevent PDF compilation subprocesses from hanging indefinitely by enforcing a 30-second timeout, killing timed-out processes, and handling failures.
  • Document the LaTeX subprocess RCE/DoS vulnerability, its root cause, and mitigations in the Sentinel security notes.

Tests:

  • Add unit tests validating that PDFConverter invokes pdflatex and pandoc with no-shell-escape and other expected arguments and that timeouts correctly kill the subprocess and return failure.

This commit updates the `PDFConverter` class to enforce secure subprocess execution for `pdflatex` and `pandoc`.

Changes:
- Added `-no-shell-escape` to `pdflatex` calls to prevent remote code execution via `\write18{}` macros.
- Added `--pdf-engine-opt=-no-shell-escape` to `pandoc` fallback calls.
- Implemented a strict 30-second timeout on the `subprocess.communicate()` calls.
- Added proper process cleanup logic (catch `TimeoutExpired`, `process.kill()`, and reap zombie process via a second `communicate()`).
- Added tests to `tests/test_pdf_security.py` to verify the subprocess arguments and timeout handling.

Co-authored-by: anchapin <6326294+anchapin@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

Hardened the PDF conversion pipeline against LaTeX-based RCE and DoS by enforcing no-shell-escape flags, adding subprocess timeouts with proper cleanup, and backstopping the behavior with targeted unit tests and security documentation updates.

Sequence diagram for secured LaTeX PDF compilation with timeout and no-shell-escape

sequenceDiagram
    participant PDFConverter as _compile_pdflatex
    participant Subprocess as subprocess
    participant Proc as pdflatex_process

    PDFConverter->>Subprocess: Popen(["pdflatex", "-interaction=nonstopmode", "-no-shell-escape", tex_path.name])
    Subprocess-->>PDFConverter: Proc

    PDFConverter->>Proc: communicate(timeout=30)
    alt subprocess completes in time
        Proc-->>PDFConverter: stdout, stderr (returncode == 0)
        PDFConverter-->>PDFConverter: return True
    else TimeoutExpired
        Proc-->>PDFConverter: subprocess.TimeoutExpired
        PDFConverter->>Proc: kill()
        PDFConverter->>Proc: communicate()
        Proc-->>PDFConverter: stdout, stderr
        PDFConverter-->>PDFConverter: return False
    end
Loading

File-Level Changes

Change Details Files
Harden pdflatex invocation to disable shell escapes and enforce bounded execution time.
  • Add -no-shell-escape to the pdflatex command arguments alongside -interaction=nonstopmode.
  • Wrap subprocess.Popen().communicate() in a 30-second timeout for pdflatex compilation.
  • On TimeoutExpired, kill the pdflatex process, reap it via a second communicate() call, and return False to signal failure.
  • Preserve existing success criteria that accept either zero return code or presence of the expected output file while leaving other error handling intact.
cli/pdf/converter.py
Harden pandoc-based PDF compilation with secure engine options and time-bounded execution.
  • Extend the pandoc command to include --pdf-engine=xelatex and --pdf-engine-opt=-no-shell-escape for secure LaTeX execution.
  • Introduce a 30-second timeout around pandoc’s subprocess communicate() call.
  • On TimeoutExpired, kill the pandoc process, reap it via communicate(), and return False to indicate compilation failure.
  • Keep existing logic that treats either a zero return code or an existing output file as success, while reusing the generic exception handling path for other failures.
cli/pdf/converter.py
Add unit tests to validate subprocess arguments and timeout/kill behavior for PDFConverter.
  • Introduce tests that simulate TimeoutExpired for pdflatex and pandoc, asserting that the converter returns False, calls kill(), and uses communicate(timeout=30).
  • Add tests that verify pdflatex arguments include -no-shell-escape, -interaction=nonstopmode, and the pdflatex executable name.
  • Add tests that verify pandoc arguments include --pdf-engine=xelatex, --pdf-engine-opt=-no-shell-escape, and the pandoc executable name.
  • Import PDFConverter into the security test module to drive the new tests.
tests/test_pdf_security.py
Update Sentinel security documentation to capture the LaTeX subprocess RCE/DoS incident and required mitigations.
  • Append a dated entry describing the PDFConverter LaTeX subprocess vulnerability, emphasizing shell-escape and infinite-loop risks.
  • Document the defense-in-depth requirements: mandatory -no-shell-escape flags and strict communicate() timeouts with kill-and-reap patterns for compiler subprocesses.
  • Highlight that LaTeX’s Turing completeness and shell execution capabilities require extra care at execution boundaries.
.jules/sentinel.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The timeout and kill/communicate logic is duplicated in both _compile_pdflatex and _compile_pandoc; consider extracting this into a small helper to centralize the pattern and avoid divergence in future changes.
  • The 30-second timeout value is hard-coded in multiple places (implementation and tests); promoting it to a named constant in the converter and referencing that in tests would make future tuning easier and less error-prone.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The timeout and kill/communicate logic is duplicated in both `_compile_pdflatex` and `_compile_pandoc`; consider extracting this into a small helper to centralize the pattern and avoid divergence in future changes.
- The 30-second timeout value is hard-coded in multiple places (implementation and tests); promoting it to a named constant in the converter and referencing that in tests would make future tuning easier and less error-prone.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant