Skip to content

Python steps

A Python step runs a small piece of your own Python in a response workflow: call an internal API, reshape data between two steps, score an indicator with your own logic. Every run gets a fresh, isolated sandbox that is thrown away afterwards; nothing is shared between runs or between organizations.

Python code is a type of response action. Create one with Detection & Response > Response > New action > Custom Python code; the Actions tab lists your codes with the integrations (filter Python), and a code opens on its own page (source, versions, test runs). In the workflow editor, the palette lists them in the Actions group under Python: adding one inserts a Python step that runs that code.

Who may do what

Python has its own area, Response code (Python):

Level Allows
None nothing (the default for every role except administrators)
Read see the code, its versions and its runs
Read-Write write, change, restore and test code; add a Python step to a workflow or change what one runs

Adding or changing a Python step in a workflow (its code, pinned version, allowed hosts, secrets or limits) needs Read-Write here and in Response actions & playbooks. Changing only the step's input mapping, or removing the step, does not.

Writing a step

The code defines main(input, secrets) and returns a dict:

import requests


def main(input, secrets):
    """input: the step's input (JSON). secrets: the credentials bound to
    the step, by name. Return a dict: later steps read it."""
    response = requests.get(
        f"https://api.example.com/v1/ip/{input['ip']}",
        headers={"Authorization": f"Bearer {secrets['intel']['token']}"},
        timeout=10,
    )
    response.raise_for_status()
    data = response.json()
    return {"ip": input["ip"], "score": data["score"], "tags": data["tags"]}
  • input is the step's Input setting, where {{ }} expressions take values from the trigger, the incident and earlier steps ({"ip": "{{ incident.entities.src_ip[0] }}"}).
  • secrets holds the credentials bound to the step (Connectors > Credentials), by the name you give them: secrets["intel"] is the credential's value as a dict (an API key credential is {"api_key": "..."}, a token {"token": "..."}).
  • An optional third parameter, main(input, secrets, context), gets context["dry_run"] (true in test runs and dry runs), the run id, the node id, the workflow name and the incident id.
  • The return value must be a JSON object (a dict of strings, numbers, booleans, lists, dicts and None). Later steps read it as {{ steps.<step id>.output.<field> }}.
  • What the code prints is kept with the run (up to 64 KB each for output and errors); an exception fails the step and its message and traceback are shown.

Output schema

Declare what main returns as a JSON schema (in the editor, Output schema). The workflow check then only accepts expressions that name declared fields (steps.enrich.output.scroe is an error before the workflow runs), and every run's result is checked against it: a result that does not match fails the step.

Libraries

The sandbox has Python 3.12 with the standard library and requests, httpx, jmespath and python-dateutil (pinned versions). Installing packages is not possible.

Versions

Every save of a changed source or output schema is a new version with its author, time and SHA-256; versions never change. Restore makes an old version the current one again (as a new version). Every change and every run is in the audit log (xdr.code.*); the source never is. A workflow step runs the current version unless it pins one (version).

The step's settings

Setting Meaning
code_id the code to run
version a version number to pin, or empty for the current version
input the input object; takes {{ }} expressions
egress hosts the code may reach over HTTPS (below); empty: no network
secrets {name: credential} bindings
timeout_seconds wall-clock limit, default 30 (the platform sets the maximum)
memory_mb memory limit, default 256 (the platform sets the maximum)

Only input takes expressions; the other settings are fixed when the workflow is saved. Retries follow the step's retry policy: a failure in the code, a timeout or a busy runner may be retried; a step turned off by the platform, a bad setting or a result that breaks the output schema is not. A step whose worker stopped while it ran is not repeated (its code may already have called out).

Allowed hosts

Without allowed hosts the sandbox has no network at all. With them, the code reaches exactly those hosts, over HTTPS only (the standard requests and httpx proxy settings are preset, nothing to configure):

  • api.example.com (port 443), api.example.com:8443, *.example.com (any subdomain, not example.com itself), or a public IP address;
  • at most 20 entries.

Some destinations can never be allowed, whatever the workflow says: cloud metadata endpoints (169.254.169.254, fd00:ec2::254), private, loopback, link-local and other non-public addresses, internal names (localhost, *.local, *.internal, names without a dot) and the DRACO server itself. A host name is also refused at run time when it resolves to such an address.

Secrets

Bind credentials by name; the step gets their values only while it runs. Values never appear in the workflow, the step's input, the run view or the logs: wherever a run's output, printed text or error contains a bound secret's value, it is replaced by [secret]. A credential bound to a workflow step cannot be deleted (Connectors > Secrets shows the workflow under "Used by").

Testing

In the editor, Run (or Save and run for unsaved changes) runs the saved version with the sample input, the allowed hosts and the secrets you enter, and shows the result, the printed output, errors and the time it took. A test run is a real run: it counts against the quotas and is audited.

A workflow test run or dry run also runs the code, but without network and with empty secrets (context["dry_run"] is true), so nothing leaves the sandbox.

Limits

Each run has a wall-clock timeout, a memory limit, one CPU, at most 64 processes and a 16 MB writable /tmp; the rest of the file system is read only and the code runs as an unprivileged user. Output larger than the cap is cut, a result larger than 256 KB fails the step.

Your organization also has quotas, set by the platform team (a code's page shows today's usage):

  • runs per hour;
  • CPU seconds per day (the sandbox's wall-clock time, UTC day; a step whose worker stopped mid-run is billed its whole timeout, since the sandbox may have run that long);
  • steps running at once;
  • the longest timeout and the most memory a step may ask for.

A run over a quota is refused (and retried by the step's retry policy, if any). The platform team can also turn Python off for your organization; the code's page then says why.