Skip to content

fix(tools): accept the documented api_key in BraveSearchTool - #9766

Open
Kiko0301 wants to merge 1 commit into
crewAIInc:mainfrom
Kiko0301:fix/brave-api-key-argument
Open

Kiko0301 wants to merge 1 commit into
crewAIInc:mainfrom
Kiko0301:fix/brave-api-key-argument

Conversation

@Kiko0301

Copy link
Copy Markdown
Contributor

Fixes #9762

Summary

The docs for BraveSearchTool say the API key can be passed directly via the api_key parameter or left to BRAVE_API_KEY, but the exported class had no such field: pydantic silently dropped the argument, __init__ only checked the environment, and _run sent os.environ["BRAVE_API_KEY"]. BraveSearchTool(api_key="explicit-key") therefore raised ValueError: BRAVE_API_KEY environment variable is required without the variable, and silently used the environment's key when it was set. The newer Brave tools already support both sources through BraveSearchToolBase; this brings the legacy BraveSearchTool in line with its docs and with them.

Changes

  • lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/brave_search_tool.py
    • Add an api_key: str | None field (exclude=True, so the secret is not serialized), defaulting to None.
    • __init__ falls back to BRAVE_API_KEY when no key was passed and raises when neither is available.
    • _run sends the resolved key in X-Subscription-Token (environment fallback kept).
  • lib/crewai-tools/tests/tools/brave_search_tool_test.py
    • Add TestLegacyBraveSearchToolApiKey: explicit key without the variable, environment fallback, explicit key winning, missing-key error, and the explicit key reaching the request header.

Verification

  • With the fix: pytest lib/crewai-tools/tests/tools/brave_search_tool_test.py -k TestLegacyBraveSearchToolApiKey -> 5 passed.
  • Against the base revision of brave_search_tool.py (new tests kept): 4 of the 5 fail (no api_key field / ValueError without the variable); only the missing-key error test passes, as before.
  • Full file: 66 passed, 1 failed; the failing test_rate_limit_disabled_when_zero also fails on the unmodified base (it exercises BraveSearchToolBase, not the legacy class).

AI assistance

I used an AI coding agent to investigate, implement, and test this change, and reviewed its results. Per CONTRIBUTING.md, please apply the llm-generated label; external contributors cannot add it with my current permissions.

The docs say BraveSearchTool takes its key as api_key or from
BRAVE_API_KEY, but the class had no api_key field: pydantic dropped the
argument and __init__ only checked the environment, so
BraveSearchTool(api_key=...) raised without the variable and sent the
environment's key when it was set.

Add an api_key field excluded from serialization, fall back to
BRAVE_API_KEY, and send the resolved key in the request header.
@strix-security

strix-security Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Strix Security Review

✅ No open security findings.

PR summary: The PR adds the previously missing `api_key` parameter to the legacy `BraveSearchTool`: the value is a new excluded model field, `__init__` falls back to `BRAVE_API_KEY` and errors when neither source is available, and `_run` sends the resolved key in `X-Subscription-Token`. It also adds five tests covering the explicit key, the environment fallback, precedence, the missing-key error, and the header actually sent.

Review summary

Reviewed the legacy BraveSearchTool change (api_key field with exclude=True, the new __init__ environment fallback, and the header line in _run) plus the accompanying tests. The question that mattered was whether materialising the Brave subscription token into an instance field makes it reachable by someone other than the operator who supplied it. Pydantic's field-level exclude is honoured on every serialization path crewAI drives for this class — plain model_dump(), JSON-mode dumps, the BaseTool plain serializer (so Agent/Crew/checkpoint payloads), nested dumps inside stream frames — and the __dict__/repr() fallback in the shared serializer only triggers when a model's own dump raises, which cannot happen for this class. The remaining exposure is repr()/str() of the tool object, which the operator triggers themselves and which the package already has on several other tools; no attacker or victim boundary, so it stays a hardening suggestion rather than a vulnerability. The API key never enters the request payload, the search-results file, or error text, and search_url is not settable from the tool's args schema or kwargs, so a caller cannot redirect the key. No security issues introduced by this PR.

Ruled out (5)
  • Does the new api_key field leak the Brave key into streaming frames or other serialized payloads handed to consumers of crew.stream()?
    Field-level exclude=True is honoured through the exact nested model_dump call that stream frames and event serialization use, and the __dict__ fallback only runs when a model's own dump raises, which is impossible for this class's JSON-serializable fields.
  • Does repr()/str() of the tool now exposing the key reach a third party?
    The plaintext key does appear in repr()/vars() because exclude=True does not cover them, but it is emitted by the tool owner's own process with no attacker-controlled input or cross-user trigger, and several pre-existing tools in the package declare plain api_key fields the same way.
  • Can a caller redirect the Brave key to an arbitrary host via the mutable search_url field?
    search_url is only read from self and is not part of the args schema or kwargs, so neither tool arguments nor a model-supplied value can change it; the env key was already sent to that field before the change.
  • Can the key be disclosed through error strings, the request payload, or the saved results file?
    The RequestException and KeyError handlers surface only the URL, status or variable name, the key is placed solely in the X-Subscription-Token header, and it never enters the payload or the _save_results_to_file content.
  • Does restoring a tool from a serialized dict bypass the new guard and change which key is used?
    model_validate-based restore leaves api_key unset so _run falls back to the environment variable; that is a restore-correctness matter, not a disclosure or an escalation.

Review 1 on this PR · 3e86323 · requested Nov 25 05:23 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings · Try a deeper review with @strix-security /deep

This branch has not been deployed

No deployments
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.

[BUG] BraveSearchTool ignores the documented api_key argument

1 participant