Skip to content

TheHiveV5: Validate entrypoint arguments via Pydantic#2880

Open
PierrickV wants to merge 1 commit into
developfrom
fix-thehivev5-guards
Open

TheHiveV5: Validate entrypoint arguments via Pydantic#2880
PierrickV wants to merge 1 commit into
developfrom
fix-thehivev5-guards

Conversation

@PierrickV

@PierrickV PierrickV commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

What

Validate TheHiveV5's action arguments via Pydantic v2 models before each action runs, so missing/blank required input fails immediately with a clear validation error instead of the action calling the API with bad data.

Changes

  • alert_id, message, filepathNonEmptyStr (kept as plain non-empty strings — TheHive alert IDs use an opaque ~-prefixed format, not a UUID)

Testing

  • pytest and black pass.

@sourcery-ai

sourcery-ai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

The PR adds input validation guards to TheHive V5 actions (add observable, upload logs, add comment) to handle missing or empty entrypoint arguments, updates tests accordingly, and bumps the module version with changelog documentation.

Sequence diagram for TheHiveV5 action guards on alert_id and message

sequenceDiagram
    actor AutomationPlatform
    participant TheHiveCreateCommentV5
    participant TheHiveConnector

    AutomationPlatform->>TheHiveCreateCommentV5: run(arguments)
    alt [missing or empty alert_id]
        TheHiveCreateCommentV5->>TheHiveCreateCommentV5: error("Alert ID is required")
        TheHiveCreateCommentV5-->>AutomationPlatform: None
    else [missing or empty message]
        TheHiveCreateCommentV5->>TheHiveCreateCommentV5: error("Comment message is required")
        TheHiveCreateCommentV5-->>AutomationPlatform: None
    else [arguments valid]
        TheHiveCreateCommentV5->>TheHiveConnector: TheHiveConnector(base_url, apikey, verify)
        TheHiveCreateCommentV5-->>AutomationPlatform: OutputComment
    end
Loading

File-Level Changes

Change Details Files
Add entrypoint argument validation to TheHive V5 actions to guard against missing or empty alert IDs and comment messages.
  • Added a guard in TheHiveCreateCommentV5.run to require non-empty alert_id and message, returning None and logging an error when invalid.
  • Added a guard in TheHiveUploadLogsV5.run to require non-empty alert_id, returning None and logging an error when invalid.
  • Added a guard in TheHiveCreateObservableV5.run to require non-empty alert_id, returning None and logging an error when invalid.
  • Updated TheHiveUploadLogsV5.run return type hint to Optional to reflect the new early-return behavior.
TheHiveV5/thehive/add_commment.py
TheHiveV5/thehive/upload_logs.py
TheHiveV5/thehive/add_observable.py
Extend test coverage to verify new guard behavior for missing or empty arguments in TheHive V5 actions.
  • Imported unittest.mock.Mock in relevant test modules to capture error calls.
  • Added parametrized tests ensuring add comment action requires alert_id and message, and does not perform any HTTP calls when invalid.
  • Added parametrized tests ensuring upload logs action requires alert_id, and does not perform any HTTP calls when invalid.
  • Added parametrized tests ensuring add observable action requires alert_id, and does not perform any HTTP calls when invalid.
TheHiveV5/tests/test_add_comment.py
TheHiveV5/tests/test_upload_logs.py
TheHiveV5/tests/test_add_observable.py
Bump TheHive V5 module version and document the fix in the changelog.
  • Updated manifest.json version from 1.1.3 to 1.1.4.
  • Added a 1.1.4 changelog entry describing the new guards for missing or empty alert entrypoint arguments and comment messages.
TheHiveV5/manifest.json
TheHiveV5/CHANGELOG.md

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

@sourcery-ai sourcery-ai Bot 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.

Hey - I've left some high level feedback:

  • The alert_id and message guards use truthiness checks (if not arguments.get(...)), which will also reject falsy but potentially valid values (e.g., 0); if those IDs/messages can be non-empty falsy values, consider explicitly checking for None/empty string instead.
  • The alert_id validation logic is now duplicated across add_observable, upload_logs, and add_comment; consider extracting a small shared helper or base-method for required-argument validation to keep the checks consistent and easier to maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `alert_id` and `message` guards use truthiness checks (`if not arguments.get(...)`), which will also reject falsy but potentially valid values (e.g., `0`); if those IDs/messages can be non-empty falsy values, consider explicitly checking for `None`/empty string instead.
- The `alert_id` validation logic is now duplicated across `add_observable`, `upload_logs`, and `add_comment`; consider extracting a small shared helper or base-method for required-argument validation to keep the checks consistent and easier to maintain.

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.

@PierrickV
PierrickV force-pushed the fix-thehivev5-guards branch from 9b771ee to 076997e Compare July 10, 2026 15:04
@PierrickV
PierrickV requested a review from a team July 11, 2026 05:40
@PierrickV PierrickV changed the title TheHiveV5: guard against missing/empty entrypoint arguments TheHiveV5: Validate entrypoint arguments via Pydantic Jul 11, 2026
@PierrickV

Copy link
Copy Markdown
Contributor Author

Updated this PR to migrate TheHiveV5 to native Pydantic v2 validation and bump the SDK from 1.13.0 to 1.23.1.

Migrated files:

  • TheHiveV5/thehive/add_commment.py
  • TheHiveV5/thehive/add_observable.py
  • TheHiveV5/thehive/upload_logs.py

Local validation:

  • pytest remains 43/43 passing (count unchanged before/after)
  • black passes
  • this module has no dedicated mypy configuration; an ad hoc mypy run only reports pre-existing missing-stub import issues

CI note: codecov/project still fails overall, but codecov/patch/TheHive and codecov/project/TheHive are both passing, so the remaining repo-wide coverage failure looks unrelated to this module.

@PierrickV

Copy link
Copy Markdown
Contributor Author

Simplified the TheHiveV5 argument guards by replacing the custom blank-check @field_validator methods with a shared Pydantic v2 NonEmptyStr alias (Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]). I intentionally kept these fields as non-empty strings rather than tightening their types: TheHive alert_id uses TheHive's own ~-prefixed internal identifier format (for example ~12345, not an RFC-4122 UUID), add_comment.message is free text, and upload_logs.filepath is a local path string. In add_observable, the only required opaque string field here is alert_id; the observable payload itself stays polymorphic via the existing events structure.

…action

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PierrickV
PierrickV force-pushed the fix-thehivev5-guards branch from b7ea111 to cb376b5 Compare July 13, 2026 07:39
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