Skip to content

Commit 8544314

Browse files
authored
fix: restrict importlib provider loading to trusted namespaces (#7463)
1 parent b047730 commit 8544314

6 files changed

Lines changed: 102 additions & 3 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ For more advanced multi-agent orchestrations and workflows, read
152152

153153
Use AutoGen Studio to prototype and run multi-agent workflows without writing code.
154154

155+
> **Caution**: AutoGen Studio is meant to help you rapidly prototype multi-agent workflows and
156+
> demonstrate an example of end user interfaces built with AutoGen. It is **not meant to be a
157+
> production-ready app**. Developers are encouraged to use the AutoGen framework to build their own
158+
> applications, implementing authentication, security and other features required for deployed
159+
> applications. See the [security note](https://microsoft.github.io/autogen/dev/user-guide/autogenstudio-user-guide/index.html#a-note-on-security) for more details.
160+
155161
```bash
156162
# Run AutoGen Studio on http://localhost:8080
157163
autogenstudio ui --port 8080 --appdir ./my-app

python/docs/src/user-guide/autogenstudio-user-guide/installation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ myst:
77

88
# Installation
99

10+
```{caution}
11+
AutoGen Studio is meant to help you rapidly prototype multi-agent workflows and demonstrate an example of end user interfaces built with AutoGen. It is not meant to be a production-ready app. Developers are encouraged to use the AutoGen framework to build their own applications, implementing authentication, security and other features required for deployed applications.
12+
```
13+
1014
There are two ways to install AutoGen Studio - from PyPi or from source. We **recommend installing from PyPi** unless you plan to modify the source code.
1115

1216
## Create a Virtual Environment (Recommended)

python/packages/autogen-core/src/autogen_core/_component_config.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ def _type_to_provider_str(t: type) -> str:
5252
"OllamaChatCompletionClient": "autogen_ext.models.ollama.OllamaChatCompletionClient",
5353
}
5454

55+
_TRUSTED_PROVIDER_NAMESPACES: tuple[str, ...] = (
56+
"autogen_core.",
57+
"autogen_agentchat.",
58+
"autogen_ext.",
59+
"autogen_studio.",
60+
"autogenstudio.",
61+
"autogen_test_utils.",
62+
)
63+
64+
65+
def _get_trusted_namespaces() -> tuple[str, ...]:
66+
"""Return the set of trusted provider namespaces.
67+
68+
The default set covers all first-party AutoGen packages. Additional namespaces
69+
can be added at runtime by setting the ``AUTOGEN_ALLOWED_PROVIDER_NAMESPACES``
70+
environment variable to a comma-separated list of package prefixes
71+
(e.g. ``mycompany_agents,mypackage``).
72+
"""
73+
import os
74+
75+
extra = os.environ.get("AUTOGEN_ALLOWED_PROVIDER_NAMESPACES", "")
76+
if extra:
77+
extras = tuple(
78+
ns.strip() if ns.strip().endswith(".") else ns.strip() + "." for ns in extra.split(",") if ns.strip()
79+
)
80+
return _TRUSTED_PROVIDER_NAMESPACES + extras
81+
return _TRUSTED_PROVIDER_NAMESPACES
82+
5583

5684
class ComponentFromConfig(Generic[FromConfigT]):
5785
@classmethod
@@ -224,6 +252,23 @@ def load_component(
224252
raise ValueError("Invalid")
225253

226254
module_path, class_name = output
255+
256+
trusted = _get_trusted_namespaces()
257+
# Also allow test modules (pytest convention) to load components
258+
module_name = module_path.rsplit(".", maxsplit=1)[-1]
259+
is_test_module = module_name.startswith("test_") or module_path.startswith("test_")
260+
if not is_test_module and not any(
261+
module_path.startswith(ns) or module_path == ns.rstrip(".") for ns in trusted
262+
):
263+
raise ValueError(
264+
f"Provider module '{module_path}' is not in a trusted namespace. "
265+
f"Allowed namespaces by default: autogen_core, autogen_agentchat, autogen_ext, "
266+
f"autogen_studio, autogenstudio. "
267+
f"To allow additional namespaces, set the AUTOGEN_ALLOWED_PROVIDER_NAMESPACES "
268+
f"environment variable to a comma-separated list "
269+
f"(e.g. AUTOGEN_ALLOWED_PROVIDER_NAMESPACES=mycompany_agents,mypackage)."
270+
)
271+
227272
module = importlib.import_module(module_path)
228273
component_class = module.__getattribute__(class_name)
229274

python/packages/autogen-core/tests/test_component_config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,19 @@ def test_component_descriptions() -> None:
367367
assert ComponentWithDocstring("test").dump_component().description == "A component using just docstring."
368368
assert ComponentWithDescription("test").dump_component().description == "Explicit description"
369369
assert ComponentWithDescription("test").dump_component().label == "Custom Component"
370+
371+
372+
def test_untrusted_provider_rejected() -> None:
373+
"""load_component must reject providers outside trusted namespaces."""
374+
bad_model = ComponentModel(provider="os.path.join", config={})
375+
with pytest.raises(ValueError, match="not in a trusted namespace"):
376+
ComponentLoader.load_component(bad_model, object) # type: ignore
377+
378+
379+
def test_trusted_provider_via_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
380+
"""AUTOGEN_ALLOWED_PROVIDER_NAMESPACES extends the allowed namespace list."""
381+
monkeypatch.setenv("AUTOGEN_ALLOWED_PROVIDER_NAMESPACES", "mycompany_agents")
382+
from autogen_core._component_config import _get_trusted_namespaces # type: ignore
383+
384+
namespaces = _get_trusted_namespaces()
385+
assert "mycompany_agents." in namespaces

python/packages/autogen-ext/src/autogen_ext/agents/video_surfer/tools.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,27 @@ def extract_audio(video_path: str, audio_output_path: str) -> str:
1616
"""
1717
Extracts audio from a video file and saves it as an MP3 file.
1818
19-
:param video_path: Path to the video file.
20-
:param audio_output_path: Path to save the extracted audio file.
19+
:param video_path: Path to the video file (must be a local file path, not a URL).
20+
:param audio_output_path: Path to save the extracted audio file (must end with .mp3).
2121
:return: Confirmation message with the path to the saved audio file.
2222
"""
23+
import os
24+
import re
25+
26+
# Reject URLs to prevent SSRF via ffmpeg
27+
if re.match(r"^[a-zA-Z][a-zA-Z0-9+\-.]*://", video_path):
28+
raise ValueError("video_path must be a local file path, not a URL.")
29+
30+
# Enforce .mp3 extension to prevent writing arbitrary file types
31+
if not audio_output_path.lower().endswith(".mp3"):
32+
raise ValueError("audio_output_path must end with .mp3.")
33+
34+
# Prevent path traversal — output must stay within the current working directory
35+
cwd = os.path.realpath(os.getcwd())
36+
output_real = os.path.realpath(audio_output_path)
37+
if not output_real.startswith(cwd + os.sep) and output_real != cwd:
38+
raise ValueError("audio_output_path must be within the current working directory.")
39+
2340
(ffmpeg.input(video_path).output(audio_output_path, format="mp3").run(quiet=True, overwrite_output=True)) # type: ignore
2441
return f"Audio extracted and saved to {audio_output_path}."
2542

python/packages/autogen-studio/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ AutoGen Studio is an AutoGen-powered AI app (user interface) to help you rapidly
99

1010
Code for AutoGen Studio is on GitHub at [microsoft/autogen](https://github.com/microsoft/autogen/tree/main/python/packages/autogen-studio)
1111

12+
> [!CAUTION]
13+
> AutoGen Studio is meant to help you rapidly prototype multi-agent workflows and demonstrate an example of end user interfaces built with AutoGen. It is **not meant to be a production-ready app**. Developers are encouraged to use the [AutoGen framework](https://microsoft.github.io/autogen) to build their own applications, implementing authentication, security and other features required for deployed applications.
14+
1215
> [!WARNING]
13-
> AutoGen Studio is under active development and is currently not meant to be a production-ready app. Expect breaking changes in upcoming releases. [Documentation](https://microsoft.github.io/autogen/docs/autogen-studio/getting-started) and the `README.md` might be outdated.
16+
> AutoGen Studio is under active development. Expect breaking changes in upcoming releases.
17+
18+
## A Note on Security
19+
20+
AutoGen Studio is a research prototype and is **not meant to be used** in a production environment. Some baseline practices are encouraged e.g., using Docker code execution environment for your agents.
21+
22+
However, other considerations such as rigorous tests related to jailbreaking, ensuring LLMs only have access to the right keys of data given the end user's permissions, and other security features are not implemented in AutoGen Studio.
23+
24+
If you are building a production application, please use the [AutoGen framework](https://microsoft.github.io/autogen) and implement the necessary security features.
1425

1526
## Updates
1627

0 commit comments

Comments
 (0)