Skip to content

πŸ›‘οΈ Sentinel: [CRITICAL] Fix command injection in PDF compilation#270

Open
anchapin wants to merge 1 commit intomainfrom
fix/rce-pdf-compilation-10565898894670016090
Open

πŸ›‘οΈ Sentinel: [CRITICAL] Fix command injection in PDF compilation#270
anchapin wants to merge 1 commit intomainfrom
fix/rce-pdf-compilation-10565898894670016090

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented Apr 27, 2026

🚨 Severity: CRITICAL
πŸ’‘ Vulnerability: Command injection/Remote Code Execution (RCE) via pdflatex and pandoc during PDF compilation in PDFConverter and CoverLetterGenerator.
🎯 Impact: Malicious user input or hallucinated LaTeX control characters could inject arbitrary shell commands.
πŸ”§ Fix: Add -no-shell-escape flag to pdflatex and --pdf-engine-opt=-no-shell-escape to pandoc. Added timeout=30 to subprocess.communicate() calls to prevent DoS.
βœ… Verification: Ran tests/test_pdf_security.py to ensure flags and timeout behavior function safely. All tests pass and linter passes.


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

Summary by Sourcery

Harden PDF compilation against command injection and long-running processes by enforcing shell-escape disabling and adding timeouts to external LaTeX/Pandoc calls.

Bug Fixes:

  • Disable shell escape in pdflatex and pandoc-based PDF generation to prevent command injection and remote code execution.
  • Add 30-second timeouts and process kill handling to PDF compilation subprocesses to avoid denial-of-service from hanging converters.

@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 27, 2026

Reviewer's Guide

Harden PDF compilation against command injection and DoS by disabling shell escape in pdflatex/pandoc and enforcing a 30s timeout on all LaTeX compilation subprocesses.

Sequence diagram for hardened PDF compilation with timeout and no shell escape

sequenceDiagram
    title Hardened PDF compilation flow with timeout and no shell escape
    actor User
    participant CLIApp
    participant PDFCompiler
    participant Subprocess
    participant PdfEngine

    User->>CLIApp: request PDF generation
    CLIApp->>PDFCompiler: compile(tex_content, output_path)

    rect rgb(230,230,255)
        PDFCompiler->>Subprocess: Popen([pdflatex|pandoc, -interaction=nonstopmode, -no-shell-escape,...])
        Subprocess->>PdfEngine: start process with args
        PdfEngine-->>Subprocess: running

        alt completes within 30s
            PDFCompiler->>Subprocess: communicate(timeout=30)
            Subprocess-->>PDFCompiler: stdout, stderr
            PDFCompiler->>PDFCompiler: check returncode or output_path.exists()
            alt success
                PDFCompiler-->>CLIApp: True (PDF created)
                CLIApp-->>User: return generated PDF
            else failure
                PDFCompiler-->>CLIApp: False (compilation failed)
                CLIApp-->>User: report compilation error
            end
        else exceeds 30s
            PDFCompiler->>Subprocess: communicate(timeout=30)
            Subprocess-->>PDFCompiler: TimeoutExpired exception
            PDFCompiler->>Subprocess: kill()
            PDFCompiler->>Subprocess: communicate()
            Subprocess-->>PDFCompiler: stdout, stderr
            PDFCompiler-->>CLIApp: raise RuntimeError
            CLIApp-->>User: report PDF compilation timed out
        end
    end
Loading

Updated class diagram for PDF compilation helpers with security hardening

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

    class PDFConverter {
        +_compile_pdflatex(tex_path, output_path, working_dir) bool
        +_compile_pandoc(tex_path, output_path, working_dir) bool
    }

    class SubprocessModule {
        +Popen(args, stdout, stderr, cwd) Process
    }

    class Process {
        +communicate(timeout) stdout_stderr
        +kill() void
        +returncode int
    }

    class PdfLatexEngine {
        +exec(-interaction=nonstopmode, -no-shell-escape, tex_name) void
    }

    class PandocEngine {
        +exec(tex_path, output_path, --pdf-engine=xelatex, --pdf-engine-opt=-no-shell-escape) void
    }

    CoverLetterGenerator ..> SubprocessModule : uses
    PDFConverter ..> SubprocessModule : uses

    SubprocessModule ..> Process : returns
    Process ..> PdfLatexEngine : runs
    Process ..> PandocEngine : runs

    CoverLetterGenerator ..> PdfLatexEngine : compiles
    CoverLetterGenerator ..> PandocEngine : fallback

    PDFConverter ..> PdfLatexEngine : _compile_pdflatex
    PDFConverter ..> PandocEngine : _compile_pandoc
Loading

File-Level Changes

Change Details Files
Harden pdflatex invocation in cover letter PDF generation against RCE and DoS.
  • Add -no-shell-escape flag to the pdflatex command to prevent shell escapes in LaTeX documents.
  • Wrap subprocess.communicate() with a 30-second timeout and kill the process on timeout to avoid hanging compilations, then raise a RuntimeError.
  • Preserve existing success criteria by checking both process return code and output PDF existence.
cli/generators/cover_letter_generator.py
Harden both pdflatex and pandoc-based PDF compilation in the core PDF converter against RCE and DoS.
  • Add -no-shell-escape flag to the pdflatex command to disable LaTeX shell escapes.
  • Extend pandoc invocation with --pdf-engine-opt=-no-shell-escape when using xelatex as the PDF engine.
  • Wrap all subprocess.communicate() calls with a 30-second timeout, killing the process and raising RuntimeError on timeout, while keeping existing success checks based on return code and output file existence.
cli/pdf/converter.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 new timeout handling logic for pdflatex and pandoc is duplicated in multiple places; consider extracting a small helper (e.g., run_with_timeout(cmd, cwd, timeout=30)) to centralize process execution, timeout, and error behavior.
  • On RuntimeError("PDF compilation timed out"), stdout/stderr are currently discarded; capturing and attaching stderr (or at least logging it) would make diagnosing misconfigurations or unexpected hangs much easier while keeping the call sites unchanged.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new timeout handling logic for `pdflatex` and `pandoc` is duplicated in multiple places; consider extracting a small helper (e.g., `run_with_timeout(cmd, cwd, timeout=30)`) to centralize process execution, timeout, and error behavior.
- On `RuntimeError("PDF compilation timed out")`, stdout/stderr are currently discarded; capturing and attaching stderr (or at least logging it) would make diagnosing misconfigurations or unexpected hangs much easier while keeping the call sites unchanged.

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