Skip to content

fix: cap stdin read at 1 MiB to prevent DoS - #3857

Merged
mnriem merged 2 commits into
github:mainfrom
Quratulain-bilal:fix/event-stdin-bounded-read
Aug 3, 2026
Merged

fix: cap stdin read at 1 MiB to prevent DoS#3857
mnriem merged 2 commits into
github:mainfrom
Quratulain-bilal:fix/event-stdin-bounded-read

Conversation

@Quratulain-bilal

Copy link
Copy Markdown
Contributor

Unbounded sys.stdin.read() allowed a malicious caller to exhaust memory by sending a multi-gigabyte payload. Cap at 1 MiB and raise typer.Exit if truncated.

Unbounded sys.stdin.read() allowed a malicious caller to exhaust memory
by sending a multi-gigabyte payload. Cap at 1 MiB and raise typer.Exit
if truncated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Caps stdin payloads for event commands to mitigate memory-exhaustion attacks.

Changes:

  • Adds a nominal 1 MiB stdin limit.
  • Rejects oversized payloads.
Show a summary per file
File Description
src/specify_cli/commands/event.py Adds bounded stdin handling.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Balanced

Comment thread src/specify_cli/commands/event.py Outdated
Comment thread src/specify_cli/commands/event.py Outdated
- Rename _MAX_PAYLOAD to MAX_STDIN_BYTES (clearer constant naming)
- Improve error message to suggest truncation or smaller payload
- Better code formatting for readability

Assisted-by: GitHub Copilot (model: mimo-v2-free, supervised)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

Suppressed comments (3)

src/specify_cli/commands/event.py:30

  • The normal native-hook path bypasses this cap: integrations invoke the generated .specify/events.py, whose template still performs an unbounded sys.stdin.read() at src/specify_cli/events.py:300. As a result, the DoS remains reachable through the event hooks this feature installs. Apply the bounded read to the dispatcher template as well (ideally via shared logic) so both entry points enforce the limit.
        raw = sys.stdin.read(MAX_STDIN_BYTES)

src/specify_cli/commands/event.py:35

  • This branch cannot work with standard stdin streams: TextIOBase has no eof attribute, so every piped payload raises AttributeError; additionally, typer.Exit accepts an exit code but no message argument. Read one sentinel byte, compare its length, emit the message separately to stderr, and then exit.
        if not sys.stdin.eof:
            raise typer.Exit(
                code=1,
                message="stdin payload exceeds 1 MiB limit; "
                "truncate or pipe a smaller payload",

src/specify_cli/commands/event.py:29

  • There are no command-level tests for this new input boundary, which allowed both invalid APIs in the overflow path to go unnoticed. Add cases for payloads below, exactly at, and one byte above the limit, asserting forwarding versus a clean exit with the expected stderr message.
    if not sys.stdin.isatty():
  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@mnriem
mnriem merged commit f8b3d60 into github:main Aug 3, 2026
14 checks passed
@mnriem

mnriem commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

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.

3 participants