π‘οΈ Sentinel: [CRITICAL] Fix command injection and timeout in PDF generation - #368
π‘οΈ Sentinel: [CRITICAL] Fix command injection and timeout in PDF generation#368anchapin wants to merge 2 commits into
Conversation
Added `-no-shell-escape` flag and 30-second timeouts to `pdflatex` and `pandoc` compilation commands in `cli/pdf/converter.py` and `cli/generators/cover_letter_generator.py` to prevent Remote Code Execution (RCE) and Denial of Service (DoS) attacks. Added tests to verify the fixes. Co-authored-by: anchapin <[email protected]>
|
π 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's GuideEnforces secure and resilient PDF generation by adding no-shell-escape flags and timeouts around LaTeX/Pandoc subprocesses, plus regression tests and sentinel documentation for the security fixes. Sequence diagram for secure PDF generation with no-shell-escape and timeoutsequenceDiagram
actor User
participant CoverLetterGenerator
participant PdfConverter as PdfConverter
participant Pdflatex as subprocess_Pdflatex
participant Pandoc as subprocess_Pandoc
User->>CoverLetterGenerator: generate_cover_letter_pdf
CoverLetterGenerator->>PdfConverter: _compile_pdflatex(tex_path, output_path, working_dir)
PdfConverter->>Pdflatex: subprocess.Popen(["pdflatex", "-interaction=nonstopmode", "-no-shell-escape", tex_path.name])
Pdflatex-->>PdfConverter: process
PdfConverter->>Pdflatex: process.communicate(timeout=30)
alt pdflatex_timeout
Pdflatex-->>PdfConverter: subprocess.TimeoutExpired
PdfConverter->>Pdflatex: process.kill()
PdfConverter->>Pdflatex: process.communicate()
PdfConverter-->>CoverLetterGenerator: False
else pdflatex_success
Pdflatex-->>PdfConverter: stdout, stderr
PdfConverter-->>CoverLetterGenerator: True
else pdflatex_failure
PdfConverter->>PdfConverter: _compile_pandoc(tex_path, output_path, working_dir)
PdfConverter->>Pandoc: subprocess.Popen(["pandoc", tex_path, "-o", output_path, "--pdf-engine=xelatex", "--pdf-engine-opt=-no-shell-escape"])
Pandoc-->>PdfConverter: process
PdfConverter->>Pandoc: process.communicate(timeout=30)
alt pandoc_timeout
Pandoc-->>PdfConverter: subprocess.TimeoutExpired
PdfConverter->>Pandoc: process.kill()
PdfConverter->>Pandoc: process.communicate()
PdfConverter-->>CoverLetterGenerator: False
else pandoc_complete
Pandoc-->>PdfConverter: stdout, stderr
PdfConverter-->>CoverLetterGenerator: True/False
end
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The timeout/kill/communicate pattern for
pdflatexandpandocis duplicated in multiple places; consider extracting a small helper (e.g.,run_with_timeout(cmd, cwd=None, timeout=30)) to centralize this behavior and reduce the risk of inconsistent future changes. - In
_compile_pdfyou now importsubprocessinside the method whilecli/pdf/converter.pyuses a module-level import; aligning these to a single import style (preferably at the top of the file) will keep the codebase more consistent and avoid subtle mocking differences in tests.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The timeout/kill/communicate pattern for `pdflatex` and `pandoc` is duplicated in multiple places; consider extracting a small helper (e.g., `run_with_timeout(cmd, cwd=None, timeout=30)`) to centralize this behavior and reduce the risk of inconsistent future changes.
- In `_compile_pdf` you now import `subprocess` inside the method while `cli/pdf/converter.py` uses a module-level import; aligning these to a single import style (preferably at the top of the file) will keep the codebase more consistent and avoid subtle mocking differences in tests.Help me be more useful! Please click π or π on each comment and I'll use the feedback to improve your reviews.
Added `-no-shell-escape` flag and 30-second timeouts to `pdflatex` and `pandoc` compilation commands in `cli/pdf/converter.py` and `cli/generators/cover_letter_generator.py` to prevent Remote Code Execution (RCE) and Denial of Service (DoS) attacks. Added tests to verify the fixes. Co-authored-by: anchapin <[email protected]>
π¨ Severity: CRITICAL
π‘ Vulnerability: The
pdflatexandpandoccommands were missing the-no-shell-escapeflag, allowing potential Remote Code Execution (RCE) if malicious LaTeX was compiled. Furthermore, the subprocess calls lacked a timeout, making the application vulnerable to Denial of Service (DoS) attacks via infinite compilation loops.π― Impact: A malicious user could execute arbitrary shell commands on the host server by injecting
\write18or\input{|...}into the LaTeX template inputs. Additionally, malformed LaTeX could cause the process to hang indefinitely, starving the server of resources.π§ Fix: Enforced
-no-shell-escapeforpdflatexand--pdf-engine-opt=-no-shell-escapeforpandoc. Added a 30-second timeout to allsubprocess.communicate()calls, catchingTimeoutExpired, explicitly killing the process, and returningFalsegracefully. Added test cases totests/test_pdf_security.pyto ensure these arguments and timeouts are enforced.β Verification: Ran the full test suite (
python -m pytest), achieving 100% pass rate. Verified new security tests validate the correct flags and timeout handling. Code was formatted withblack.PR created automatically by Jules for task 3257995907454688652 started by @anchapin
Summary by Sourcery
Harden PDF generation against command injection and hangs by tightening LaTeX/pandoc invocation and adding timeouts.
Bug Fixes:
Documentation:
Tests: