Skip to content

🛡️ Sentinel: [CRITICAL] Fix command injection in PDF generation#225

Open
anchapin wants to merge 1 commit intomainfrom
sentinel-fix-pdf-command-injection-9354257226934992843
Open

🛡️ Sentinel: [CRITICAL] Fix command injection in PDF generation#225
anchapin wants to merge 1 commit intomainfrom
sentinel-fix-pdf-command-injection-9354257226934992843

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented Apr 2, 2026

Severity: CRITICAL
Vulnerability: The _compile_pdf method in CoverLetterGenerator executed pdflatex and pandoc without the -no-shell-escape flag. LaTeX's shell escape feature could be abused to execute arbitrary commands if compiling malicious, unescaped, or AI-generated input (Remote Code Execution). Additionally, the subprocess call lacked a timeout, making it vulnerable to infinite compilation loops leading to Denial of Service (DoS) and zombie processes.
Impact: A malicious or improperly sanitized input to the cover letter generator could result in arbitrary code execution on the host machine or resource exhaustion causing an outage.
Fix:

  • Added -no-shell-escape to pdflatex arguments.
  • Added --pdf-engine-opt=-no-shell-escape to pandoc fallback arguments.
  • Implemented timeout=30 in process.communicate() with proper process termination (process.kill()) and stream cleanup to prevent zombie processes on timeout.
    Verification: Ran full test suite via pytest tests/ which completed successfully. The tests/test_cover_letter_generator.py tests executed successfully, ensuring no regressions.

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

Summary by Sourcery

Harden PDF compilation in the cover letter generator to mitigate command injection and denial-of-service risks.

Bug Fixes:

  • Disable LaTeX shell escape when invoking pdflatex and the pandoc xelatex engine to prevent command injection during PDF generation.
  • Add timeouts and proper process termination for pdflatex and pandoc invocations to avoid hangs and zombie processes during PDF compilation.

…eration

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
Copy link
Copy Markdown

sourcery-ai bot commented Apr 2, 2026

Reviewer's Guide

Secures LaTeX/PDF generation in CoverLetterGenerator by disabling shell escape for pdflatex/pandoc and adding timeouts with explicit process cleanup to prevent RCE/DoS and zombie processes.

Sequence diagram for secured PDF compilation with timeout and fallback

sequenceDiagram
    actor User
    participant CoverLetterGenerator
    participant pdflatex
    participant pandoc

    User->>CoverLetterGenerator: generate_cover_letter
    CoverLetterGenerator->>CoverLetterGenerator: _compile_pdf(output_path, tex_content)

    Note over CoverLetterGenerator: Primary attempt using pdflatex with -no-shell-escape
    CoverLetterGenerator->>pdflatex: Popen([pdflatex, -interaction=nonstopmode, -no-shell-escape, tex_path.name])
    pdflatex-->>CoverLetterGenerator: process handle

    CoverLetterGenerator->>pdflatex: communicate(timeout=30)
    alt pdflatex completes in time
        pdflatex-->>CoverLetterGenerator: stdout, stderr, returncode
        alt returncode == 0 or output_path exists
            CoverLetterGenerator-->>User: PDF compiled successfully
        else returncode != 0 and output_path missing
            Note over CoverLetterGenerator: Try pandoc fallback
            CoverLetterGenerator->>pandoc: Popen([pandoc, tex_path, -o, output_path, --pdf-engine=xelatex, --pdf-engine-opt=-no-shell-escape])
            pandoc-->>CoverLetterGenerator: process handle
            CoverLetterGenerator->>pandoc: communicate(timeout=30)
            alt pandoc completes in time
                pandoc-->>CoverLetterGenerator: stdout, stderr, returncode
                alt returncode == 0 or output_path exists
                    CoverLetterGenerator-->>User: PDF compiled via pandoc fallback
                else
                    CoverLetterGenerator-->>User: PDF compilation failed
                end
            else pandoc timeout
                CoverLetterGenerator->>pandoc: kill()
                pandoc-->>CoverLetterGenerator: stdout, stderr after kill
                CoverLetterGenerator-->>User: PDF compilation failed
            end
        end
    else pdflatex timeout
        CoverLetterGenerator->>pdflatex: kill()
        pdflatex-->>CoverLetterGenerator: stdout, stderr after kill
        Note over CoverLetterGenerator: No pandoc fallback on timeout
        CoverLetterGenerator-->>User: PDF compilation failed
    end
Loading

Class diagram for CoverLetterGenerator PDF compilation changes

classDiagram
    class CoverLetterGenerator {
        +bool _compile_pdf(output_path, tex_content)
    }

    class SubprocessUsage {
        +Popen(args, stdout, stderr, cwd)
        +communicate(timeout)
        +kill()
    }

    CoverLetterGenerator ..> SubprocessUsage : uses

    class PdfLatexInvocation {
        -command pdflatex
        -option interaction_nonstopmode
        -option no_shell_escape
        -arg tex_path_name
        +build_args()
    }

    class PandocInvocation {
        -command pandoc
        -arg tex_path
        -option output_path
        -option pdf_engine_xelatex
        -option pdf_engine_opt_no_shell_escape
        +build_args()
    }

    CoverLetterGenerator ..> PdfLatexInvocation : primary
    CoverLetterGenerator ..> PandocInvocation : fallback

    class TimeoutHandling {
        +int timeout_seconds
        +handle_timeout(process)
    }

    TimeoutHandling : timeout_seconds = 30
    CoverLetterGenerator ..> TimeoutHandling : enforces timeout
Loading

Flow diagram for timeout and process cleanup in PDF compilation

flowchart TD
    A[Start _compile_pdf] --> B[Run pdflatex with -no-shell-escape via Popen]
    B --> C[communicate timeout 30]

    C -->|Completes| D{pdflatex success or output_path exists}
    C -->|TimeoutExpired| E[Kill pdflatex]

    E --> F[communicate after kill]
    F --> G[Return False]

    D -->|Yes| H[Set pdf_created True]
    H --> Z[End]
    D -->|No| I[Run pandoc fallback with --pdf-engine-opt=-no-shell-escape]

    I --> J[communicate timeout 30]
    J -->|Completes| K{pandoc success or output_path exists}
    J -->|TimeoutExpired| L[Kill pandoc]

    L --> M[communicate after kill]
    M --> N[Return False]

    K -->|Yes| O[Set pdf_created True]
    O --> Z
    K -->|No| P[Return False]

    Z[End _compile_pdf]
Loading

File-Level Changes

Change Details Files
Hardened pdflatex invocation against command injection and hangs.
  • Added -no-shell-escape flag to the pdflatex command to disable LaTeX shell escape.
  • Wrapped process.communicate in a 30-second timeout to avoid infinite compilation.
  • On timeout, kill the pdflatex process and perform a final communicate call, then return False to signal failure.
  • Kept existing success condition based on process return code or PDF existence.
cli/generators/cover_letter_generator.py
Hardened pandoc fallback invocation against command injection and hangs.
  • Extended pandoc arguments with --pdf-engine-opt=-no-shell-escape to propagate shell-escape disabling to the xelatex engine.
  • Wrapped process.communicate in a 30-second timeout similar to the primary path.
  • On timeout, kill the pandoc process, perform a final communicate call, and return False to signal failure.
  • Preserved existing logic that treats either a zero return code or existing PDF as success.
cli/generators/cover_letter_generator.py

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 both the pdflatex and pandoc subprocesses is duplicated; consider extracting it into a small helper function to centralize the communicate(timeout=...) / TimeoutExpired / kill() pattern and keep this method easier to read and maintain.
  • The 30-second timeout is currently hardcoded in two places; consider defining a single module-level or class-level constant (e.g., PDF_COMPILE_TIMEOUT_SECONDS) so future tuning doesn’t require touching multiple call sites.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The timeout handling logic for both the `pdflatex` and `pandoc` subprocesses is duplicated; consider extracting it into a small helper function to centralize the `communicate(timeout=...)` / `TimeoutExpired` / `kill()` pattern and keep this method easier to read and maintain.
- The 30-second timeout is currently hardcoded in two places; consider defining a single module-level or class-level constant (e.g., `PDF_COMPILE_TIMEOUT_SECONDS`) so future tuning doesn’t require touching multiple call sites.

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