Skip to content

Onyphe: Validate entrypoint argument via Pydantic#2886

Open
PierrickV wants to merge 1 commit into
developfrom
fix-onyphe-guard
Open

Onyphe: Validate entrypoint argument via Pydantic#2886
PierrickV wants to merge 1 commit into
developfrom
fix-onyphe-guard

Conversation

@PierrickV

@PierrickV PierrickV commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

What

Validate Onyphe's datascan action argument via a Pydantic v2 model before the action runs, so a missing/blank/malformed IP or search string fails immediately with a clear validation error instead of calling the API with bad data.

Changes

  • ipIPvAnyAddress | None
  • string kept as NonEmptyStr | None
  • The existing cross-field validator (exactly one of ip/string must be set) is preserved unchanged

Testing

  • pytest and black pass.

@sourcery-ai

sourcery-ai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds early validation guards to OnypheDatascanAction.run() to handle missing or empty string/ip arguments without making HTTP requests, updates versioning and changelog, and extends tests to cover the new behavior.

Sequence diagram for updated OnypheDatascanAction.run argument guards

sequenceDiagram
    actor Client
    participant OnypheDatascanAction
    participant get_arg_ip
    participant urljoin

    Client->>OnypheDatascanAction: run(arguments)
    alt no_ip_and_no_string
        OnypheDatascanAction->>OnypheDatascanAction: error
        OnypheDatascanAction-->>Client: None
    else both_ip_and_string
        OnypheDatascanAction-->>Client: TypeError
    else ip_only
        OnypheDatascanAction->>get_arg_ip: get_arg_ip(arguments)
        get_arg_ip-->>OnypheDatascanAction: resource
        OnypheDatascanAction->>urljoin: urljoin(url, "datascan/" + resource)
        urljoin-->>OnypheDatascanAction: get_url
        OnypheDatascanAction-->>Client: result
    else string_only
        OnypheDatascanAction->>OnypheDatascanAction: resource = arguments.get("string")
        alt empty_string
            OnypheDatascanAction->>OnypheDatascanAction: error
            OnypheDatascanAction-->>Client: None
        else non_empty_string
            OnypheDatascanAction->>urljoin: urljoin(url, "datascan/" + resource)
            urljoin-->>OnypheDatascanAction: get_url
            OnypheDatascanAction-->>Client: result
        end
    end
Loading

File-Level Changes

Change Details Files
Add early argument validation in OnypheDatascanAction.run() to guard against missing or empty string/ip inputs before constructing URLs or calling the API.
  • Return None and log an error when both 'ip' and 'string' are absent in arguments.
  • Keep enforcing that exactly one of 'ip' or 'string' must be provided, raising TypeError otherwise.
  • Switch string extraction to use arguments.get('string') and add an error path where empty or None string values cause an error message and early return.
  • Ensure datascan URL is only constructed when a valid non-empty resource string is present.
Onyphe/onyphe/action_onyphe_datascan.py
Extend tests for OnypheDatascanAction to verify the new guards and ensure that the HTTP API is not called for invalid string inputs.
  • Import unittest.mock.Mock to spy on the action.error method.
  • Adjust bad_arguments fixture to remove the empty-arguments TypeError case that is now handled by the new guard.
  • Add parametrized test that covers missing, empty, and None string arguments and asserts that action.error is called with the expected message and that no HTTP calls are issued.
Onyphe/tests/test_onyphe_datascan.py
Update package metadata to reflect the new patch release and document the guard behavior change.
  • Add a 1.23.1 entry to CHANGELOG.md describing the guard against missing/empty datascan string arguments.
  • Bump manifest.json version from 1.23.0 to 1.23.1.
Onyphe/CHANGELOG.md
Onyphe/manifest.json

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 found 1 issue, and left some high level feedback:

  • The run method is now returning None in some branches but still annotated as -> dict; consider updating the type hint (e.g., to Optional[dict]) to reflect the new behavior and keep static analysis accurate.
  • The argument validation logic in run now has multiple separate checks for missing keys and empty string values; consider consolidating these into a single, clearly ordered validation block to reduce duplication and make the control flow easier to follow.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `run` method is now returning `None` in some branches but still annotated as `-> dict`; consider updating the type hint (e.g., to `Optional[dict]`) to reflect the new behavior and keep static analysis accurate.
- The argument validation logic in `run` now has multiple separate checks for missing keys and empty string values; consider consolidating these into a single, clearly ordered validation block to reduce duplication and make the control flow easier to follow.

## Individual Comments

### Comment 1
<location path="Onyphe/onyphe/action_onyphe_datascan.py" line_range="26-30" />
<code_context>

     def run(self, arguments) -> dict:
         url: str = "https://www.onyphe.io/api/v2/simple/"
+        if "ip" not in arguments and "string" not in arguments:
+            self.error("IP or string is required")
+            return None
+
         if ("ip" in arguments) == ("string" in arguments):
</code_context>
<issue_to_address>
**issue (bug_risk):** run() can now return None despite the declared dict return type

The new `return None` paths (when neither argument is provided or when `string` is falsy) break the `-> dict` contract and may surprise callers that expect a dict. Please either return a dict-shaped error object, update the annotation to `Optional[dict]`, or raise an exception instead of returning `None` so downstream code can handle failures predictably.
</issue_to_address>

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.

Comment thread Onyphe/onyphe/action_onyphe_datascan.py Outdated
@PierrickV
PierrickV requested a review from a team July 11, 2026 05:39
@PierrickV
PierrickV force-pushed the fix-onyphe-guard branch 2 times, most recently from aa48877 to 9883819 Compare July 11, 2026 05:54
@PierrickV PierrickV changed the title Onyphe: guard against missing/empty entrypoint argument Onyphe: Validate entrypoint argument via Pydantic Jul 11, 2026
@PierrickV

Copy link
Copy Markdown
Contributor Author

Updated this PR to the native Pydantic v2 pattern used in the related rollout.

  • SDK: sekoia-automation-sdk 1.13.0 -> 1.23.1
  • Migrated file(s): Onyphe/onyphe/action_onyphe_datascan.py
  • Tests: 78 -> 79 (added the whitespace-only invalid entrypoint case)
  • Local checks: black ✅, pytest ✅ (79 passed), mypy run completed with only the module's pre-existing missing-stub/untyped-import issues remaining; no new migration-specific type error remains

@PierrickV

Copy link
Copy Markdown
Contributor Author

Updated the datascan argument model while intentionally leaving the existing cross-field XOR validator untouched. The optional ip field is now validated as IPvAnyAddress | None, with whitespace trimming and rejection of non-string inputs preserved. The string field now uses a non-empty constrained string, and the action stringifies the validated IP when building the request URL. Added coverage for an invalid-shape IP and the IP-only success path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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