Skip to content

πŸ›‘οΈ Sentinel: [CRITICAL] Fix missing -no-shell-escape flag and timeout in PDF generation#289

Open
anchapin wants to merge 1 commit intomainfrom
sentinel-pdf-rce-fix-5092411136245107424
Open

πŸ›‘οΈ Sentinel: [CRITICAL] Fix missing -no-shell-escape flag and timeout in PDF generation#289
anchapin wants to merge 1 commit intomainfrom
sentinel-pdf-rce-fix-5092411136245107424

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented May 7, 2026

🚨 Severity: CRITICAL
πŸ’‘ Vulnerability: Missing -no-shell-escape flag in pdflatex and --pdf-engine-opt=-no-shell-escape in pandoc fallback commands inside cli/generators/cover_letter_generator.py and cli/pdf/converter.py allowed potential Remote Code Execution (RCE) from untrusted inputs embedded into the .tex files. Additionally, the lack of timeout arguments could cause process hangs (DoS).
🎯 Impact: An attacker or AI hallucination could inject malicious LaTeX commands (like \immediate\write18{...}) into the generated cover letter or PDF conversion that would be executed by the host system. Furthermore, an infinite loop in the TeX compilation could hang the server indefinitely.
πŸ”§ Fix: Added -no-shell-escape flags and 30-second timeouts with proper subprocess.TimeoutExpired handling and process termination.
βœ… Verification: Ran bandit and pytest tests/test_pdf_security.py successfully. Checked pytest test suite to ensure no breakage.


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

Summary by Sourcery

Harden PDF generation against unsafe LaTeX execution and hangs in both cover letter and generic PDF conversion flows.

Bug Fixes:

  • Prevent remote code execution by disabling shell escape in pdflatex and pandoc-based PDF generation commands.
  • Avoid potential denial-of-service hangs in LaTeX and pandoc compilation by enforcing a 30-second timeout with proper process termination.

Enhancements:

  • Align error handling for PDF compilation timeouts to propagate clear failures from both primary and fallback PDF generation paths.

… in PDF generation

Added `-no-shell-escape` flag to pdflatex and pandoc commands in `cli/generators/cover_letter_generator.py` and `cli/pdf/converter.py` to prevent Remote Code Execution (RCE) via malicious LaTeX input.
Added 30-second timeouts to the subprocess.communicate() calls to prevent infinite loops causing Denial of Service (DoS), matching the existing implementation in `cli/generators/template.py`.

Co-authored-by: anchapin <[email protected]>
@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
Copy link
Copy Markdown

sourcery-ai Bot commented May 7, 2026

Reviewer's Guide

Adds hardened PDF generation by enforcing no-shell-escape in LaTeX/pandoc commands and introducing 30s timeouts with explicit timeout handling for both the cover letter generator and generic PDF converter.

Sequence diagram for hardened cover letter PDF compilation

sequenceDiagram
    actor User
    participant CoverLetterGenerator
    participant Filesystem
    participant Subprocess

    User->>CoverLetterGenerator: request_cover_letter_pdf(tex_content, output_path)
    CoverLetterGenerator->>Filesystem: write_tex_file(tex_content)
    Filesystem-->>CoverLetterGenerator: tex_path

    CoverLetterGenerator->>Subprocess: Popen(pdflatex -interaction=nonstopmode -no-shell-escape tex_path.name)
    alt within_30_seconds
        Subprocess-->>CoverLetterGenerator: communicate(timeout=30)
        alt pdflatex_success_or_output_exists
            CoverLetterGenerator-->>User: return True (pdf_created)
        else pdflatex_failure
            CoverLetterGenerator->>Subprocess: Popen(pandoc --pdf-engine=xelatex --pdf-engine-opt=-no-shell-escape)
            alt within_30_seconds
                Subprocess-->>CoverLetterGenerator: communicate(timeout=30)
                alt pandoc_success_or_output_exists
                    CoverLetterGenerator-->>User: return True (pdf_created)
                else pandoc_failure
                    CoverLetterGenerator-->>User: return False
                end
            else pandoc_timeout
                Subprocess->>Subprocess: kill()
                Subprocess-->>CoverLetterGenerator: communicate()
                CoverLetterGenerator-->>User: raise RuntimeError(PDF compilation timed out)
            end
        end
    else pdflatex_timeout
        Subprocess->>Subprocess: kill()
        Subprocess-->>CoverLetterGenerator: communicate()
        CoverLetterGenerator-->>User: raise RuntimeError(PDF compilation timed out)
    end
Loading

Flow diagram for hardened generic PDF converter compilation

flowchart TD
    A[start_compile_pdf] --> B[run_pdflatex_with_no_shell_escape]
    B --> C[communicate timeout 30s]
    C --> D{pdflatex_timed_out}
    D -- yes --> E[kill process]
    E --> F[communicate drain_pipes]
    F --> G[return False]
    D -- no --> H{pdflatex_success_or_output_exists}
    H -- yes --> I[return True]
    H -- no --> J[run_pandoc_with_no_shell_escape]

    J --> K[communicate timeout 30s]
    K --> L{pandoc_timed_out}
    L -- yes --> M[kill process]
    M --> N[communicate drain_pipes]
    N --> O[return False]
    L -- no --> P{pandoc_success_or_output_exists}
    P -- yes --> Q[return True]
    P -- no --> R[return False]
Loading

File-Level Changes

Change Details Files
Harden pdflatex invocation in cover letter PDF compilation with no-shell-escape and timeout handling.
  • Extend pdflatex command arguments to include -no-shell-escape to disable shell escapes during LaTeX compilation.
  • Wrap process.communicate with a 30-second timeout to prevent hangs during compilation.
  • On subprocess.TimeoutExpired, kill the process, drain stdout/stderr, and raise a RuntimeError to surface a compilation timeout error.
  • Preserve existing success condition that treats either zero return code or presence of the output PDF as successful compilation.
cli/generators/cover_letter_generator.py
Harden pandoc fallback for cover letter PDF compilation with no-shell-escape and timeout handling.
  • Extend pandoc command arguments to pass --pdf-engine-opt=-no-shell-escape alongside --pdf-engine=xelatex, disabling shell escapes in the underlying LaTeX engine.
  • Wrap the pandoc subprocess.communicate call in a 30-second timeout to guard against hangs.
  • On subprocess.TimeoutExpired, kill the process, drain stdout/stderr, and raise a RuntimeError to propagate the timeout condition.
  • Retain existing logic that considers either zero return code or presence of the output PDF as a successful fallback conversion.
cli/generators/cover_letter_generator.py
Secure pdflatex-based PDF conversion utility with no-shell-escape and timeout handling.
  • Add -no-shell-escape to the pdflatex invocation to prevent LaTeX from executing shell commands.
  • Protect the communicate call with a 30-second timeout to avoid unbounded compilation time.
  • On timeout, kill the process, drain pipes, and immediately return False to signal compilation failure instead of raising.
  • Keep existing success criteria based on subprocess return code or output file existence.
cli/pdf/converter.py
Secure pandoc-based PDF conversion utility with no-shell-escape and timeout handling.
  • Update pandoc invocation to include --pdf-engine-opt=-no-shell-escape in addition to --pdf-engine=xelatex so the underlying LaTeX engine runs without shell escapes.
  • Wrap communicate in a 30-second timeout to mitigate potential hangs during conversion.
  • On timeout, kill the process, read remaining output, and return False to indicate failure.
  • Maintain prior success condition: either a zero return code or an existing output PDF is treated as success.
cli/pdf/converter.py

Possibly linked issues

  • #N/A: Bandit high findings are likely these unsafe subprocess calls; PR hardens them and makes bandit pass again.

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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

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 handling logic for pdflatex/pandoc is duplicated in multiple places; consider extracting a shared helper that encapsulates the 30s timeout, process.kill(), and communicate() behavior so it stays consistent and easier to adjust later.
  • On timeout, cover_letter_generator raises RuntimeError while converter returns False, which creates different error semantics for similar failures; it may be worth aligning these behaviors or clearly documenting the difference so callers can handle them consistently.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The timeout handling logic for pdflatex/pandoc is duplicated in multiple places; consider extracting a shared helper that encapsulates the 30s timeout, process.kill(), and communicate() behavior so it stays consistent and easier to adjust later.
- On timeout, cover_letter_generator raises RuntimeError while converter returns False, which creates different error semantics for similar failures; it may be worth aligning these behaviors or clearly documenting the difference so callers can handle them consistently.

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