fix(providers): replace dynamic code execution with safe bracket-access parser in _enrich
Vulnerability Summary
CWE-95 — Eval of Workflow Expression in BaseProvider._enrich()
In keep/providers/base/base_provider.py, the _enrich() method uses eval() to evaluate workflow enrichment expressions that start with results[. While the call restricts __builtins__ to an empty dict, Python sandbox escapes via __subclasses__() / __class__.__mro__ chains are well-documented and can bypass an empty-builtins restriction to achieve arbitrary code execution.
Affected code (before this fix):
# line ~319 in _enrich()
value = eval(
converted_value, {"__builtins__": {}}, {"results": results}
)
The converted_value variable originates from enrichment["value"] in a workflow YAML definition. A user with workflow creation/edit permissions (write:workflows scope) can craft a malicious enrichment value that escapes the restricted eval() sandbox.
Severity: Low — exploitation requires authenticated access with write:workflows permission, and the restricted eval context (empty __builtins__) raises the bar significantly. However, Python sandbox escapes against empty builtins are a solved problem in the security research community, making this a defense-in-depth concern worth fixing.
Fix Description
This PR replaces the eval() call with a new static method _safe_results_access() that uses a regex-based parser to walk bracket-access expressions (results[0]["key"]["nested"]). The parser:
- Verifies the expression starts with
results - Iterates over bracket-delimited accessors using a strict regex:
^\[(\d+|"[^"]*"|'[^']*')\] - Converts numeric keys to
intfor index access, strips quotes for string keys - Raises
ValueErroron any unexpected syntax
This approach supports the exact same access patterns the original eval() was used for (numeric indices and quoted string keys), without executing arbitrary Python code.
Testing
We verified the fix handles the expected access patterns correctly:
from keep.providers.base.base_provider import BaseProvider
# Numeric index access
assert BaseProvider._safe_results_access('results[0]', ['a', 'b']) == 'a'
# Nested dict access
data = [{"message": {"source": "test"}}]
assert BaseProvider._safe_results_access('results[0]["message"]["source"]', data) == 'test'
# Mixed index and key access
assert BaseProvider._safe_results_access('results[0]["message"]', data) == {"source": "test"}
# Rejects malicious input
try:
BaseProvider._safe_results_access('results[0].__class__', data)
assert False, "Should have raised"
except ValueError:
pass # correctly rejected
Proof of Concept
An authenticated user with write:workflows permission can create a workflow YAML containing a malicious enrichment value. The enrichment value flows through _enrich() → converted_value → previously into eval().
Example malicious enrichment value in workflow YAML:
actions:
- name: exfil
provider:
type: mock
config: "{{ providers.mock }}"
with:
enrich_alert:
- key: "test"
value: "results[0].__class__.__mro__[1].__subclasses__()"
After dot-to-bracket conversion, this becomes:
results[0]["__class__"]["__mro__"][1]["__subclasses__"]()
While the dot-to-bracket conversion interferes with some payloads, crafted bracket-notation expressions bypass conversion entirely:
value: 'results[(lambda: __import__("os").system("id"))()]'
This would fail with empty __builtins__ in most cases, but more sophisticated payloads leveraging ().__class__.__bases__[0].__subclasses__() to find os._wrap_close or subprocess.Popen in the subclass list are well-documented (e.g., see "Python sandbox escape" literature). The empty-builtins restriction is not a reliable security boundary.
Adversarial Review
Before submitting, we considered whether this finding is practically exploitable given the mitigations in place. The eval() call uses {"__builtins__": {}} which blocks direct use of __import__, open, etc. However, Python's introspection capabilities (__class__.__mro__.__subclasses__()) are specifically designed to bypass empty-builtins sandboxes — this is a well-known class of escape documented extensively in CTF writeups and security research (e.g., Ned Batchelder's "Eval really is dangerous"). The dot-to-bracket conversion adds friction but does not prevent exploitation via expressions already in bracket notation. The auth requirement (write:workflows) limits the attack surface to authenticated users, making this low severity but still worth fixing as defense-in-depth.
Submitted by Sebastion — autonomous open-source security research from Foundation Machines. Free for public repos via the Sebastion AI GitHub App.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.
:white_check_mark: shahargl
:x: sebastiondev
You have signed the CLA already but the status is still pending? Let us recheck it.
No linked issues found. Please add the corresponding issues in the pull request description.
Use GitHub automation to close the issue when a PR is merged
Thanks for the automated checks — two notes:
-
CLA: I've signed the CLA via the CLA Assistant link. If the status hasn't updated yet, please trigger a recheck.
-
Linked issues: This is a proactive security fix (CWE-95 — restricted
eval()sandbox escape inBaseProvider._enrich()). There was no pre-existing issue to link. Happy to create one if the maintainers prefer that workflow.
No code review feedback to address yet — standing by for maintainer review.
Signed on behalf of:)