Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(tasks): read the sandbox network from the task profile (#126079)

Charles Vien3b93b7448e33
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 3e124cf6be55..a7fe45f023e1 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -104,7 +104,13 @@ from products.tasks.backend.constants import (
)
from products.tasks.backend.error_telemetry import truncate_error_message
from products.tasks.backend.facade.gateway import mint_private_gateway_token, revoke_private_gateway_token
-from products.tasks.backend.facade.profiles import TaskProfile, TaskRunOptions, TaskSource, resolve_task_origin
+from products.tasks.backend.facade.profiles import (
+ NetworkAccess,
+ TaskProfile,
+ TaskRunOptions,
+ TaskSource,
+ resolve_task_origin,
+)
from products.tasks.backend.feature_flags import (
get_model_access_error,
own_subscription_cloud_enabled as own_subscription_cloud_enabled,
@@ -5217,13 +5223,18 @@ def presign_task_run_artifact_download(
def _run_has_open_network(run: TaskRun) -> bool:
- if not (run.state or {}).get("sandbox_environment_id"):
+ environment = None
+ if (run.state or {}).get("sandbox_environment_id"):
+ environment = run.get_sandbox_environment()
+ if environment is None:
+ return False
+ profile = run.task.stored_profile
+ if profile is not None and (environment is None or environment.internal):
+ return profile.network.levels[0] == NetworkAccess.FULL
+ if environment is None:
return True
- environment = run.get_sandbox_environment()
return (
- environment is not None
- and environment.network_access_level == SandboxNetworkAccessLevel.FULL
- and environment.updated_at <= run.created_at
+ environment.network_access_level == SandboxNetworkAccessLevel.FULL and environment.updated_at <= run.created_at
)
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 0c15cf073806..76889fae885f 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -4082,16 +4082,17 @@ class SandboxEnvironment(UUIDModel):
pattern = r"^[A-Za-z_][A-Za-z0-9_]*$"
return bool(re.match(pattern, key))
- def get_effective_domains(self) -> list[str]:
- if self.network_access_level == self.NetworkAccessLevel.FULL:
+ @staticmethod
+ def effective_domains(level: str, allowed_domains: Iterable[str], *, include_default_domains: bool) -> list[str]:
+ if level == SandboxEnvironment.NetworkAccessLevel.FULL:
return []
- if self.network_access_level == self.NetworkAccessLevel.TRUSTED:
+ if level == SandboxEnvironment.NetworkAccessLevel.TRUSTED:
return DEFAULT_TRUSTED_DOMAINS.copy()
- if self.network_access_level == self.NetworkAccessLevel.CUSTOM:
- domains = list(self.allowed_domains)
- if self.include_default_domains:
+ if level == SandboxEnvironment.NetworkAccessLevel.CUSTOM:
+ domains = list(allowed_domains)
+ if include_default_domains:
for domain in DEFAULT_TRUSTED_DOMAINS:
if domain not in domains:
domains.append(domain)
@@ -4099,6 +4100,11 @@ class SandboxEnvironment(UUIDModel):
return []
+ def get_effective_domains(self) -> list[str]:
+ return SandboxEnvironment.effective_domains(
+ self.network_access_level, self.allowed_domains, include_default_domains=self.include_default_domains
+ )
+
class SandboxCustomImage(TeamScopedRootMixin):
"""User-defined custom base image for cloud task sandboxes, layered on the VM sandbox base."""
diff --git a/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py b/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py
index 5278970a4e01..f1d5a60d496d 100644
--- a/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py
+++ b/products/tasks/backend/temporal/process_task/activities/get_task_processing_context.py
@@ -48,6 +48,7 @@ from products.tasks.backend.exceptions import (
TaskRunNotReadyError,
)
from products.tasks.backend.facade.api import ensure_task_run_session
+from products.tasks.backend.facade.profiles import NetworkAccess, NetworkPolicy
from products.tasks.backend.feature_flags import own_subscription_cloud_enabled
from products.tasks.backend.logic.model_access import ModelAccess, resolve_model_access
from products.tasks.backend.logic.services.agent_instructions import agent_instructions_state_update
@@ -888,6 +889,15 @@ def _resolve_sandbox_backend(
return "hogland" if enabled else "modal"
+def _profile_allowed_domains(network: NetworkPolicy) -> list[str] | None:
+ level = network.levels[0]
+ if level == NetworkAccess.FULL:
+ return None
+ return SandboxEnvironment.effective_domains(
+ level, network.allowed_domains, include_default_domains=network.include_default_domains
+ )
+
+
def _compile_effective_network_policy(allowed_domains: list[str]) -> EffectiveNetworkPolicy:
debug_domains = _get_debug_only_domains() if settings.DEBUG else []
debug_ports = _get_debug_only_ports() if settings.DEBUG else []
@@ -961,6 +971,7 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
distinct_id = (
get_actor_distinct_id(actor_user) if actor_user else task.created_by.distinct_id or "process_task_workflow"
)
+ profile = task.stored_profile
sandbox_environment_id = state.get("sandbox_environment_id")
sandbox_environment_name: str | None = None
allowed_domains: list[str] | None = None
@@ -996,7 +1007,9 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
f"Custom image '{custom_image.name}' is not ready (status: {custom_image.status}); "
"using the default base image",
)
- if sandbox_environment.network_access_level == SandboxEnvironment.NetworkAccessLevel.FULL:
+ if profile is not None and sandbox_environment.internal:
+ allowed_domains = _profile_allowed_domains(profile.network)
+ elif sandbox_environment.network_access_level == SandboxEnvironment.NetworkAccessLevel.FULL:
allowed_domains = None
else:
allowed_domains = sandbox_environment.get_effective_domains()
@@ -1010,6 +1023,8 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
log_with_activity_context(
f"Resolved sandbox environment '{sandbox_environment.name}' with full network access", run_id=run_id
)
+ elif profile is not None:
+ allowed_domains = _profile_allowed_domains(profile.network)
# A per-run image (picked at task start) wins over the environment's image.
state_custom_image_id = state.get("custom_image_id")
@@ -1291,8 +1306,6 @@ def get_task_processing_context(input: GetTaskProcessingContextInput) -> TaskPro
if user_github_integration is not None:
user_github_integration_id = str(user_github_integration.integration.id)
- profile = task.stored_profile
-
return TaskProcessingContext(
task_id=str(task.id),
run_id=run_id,
diff --git a/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py b/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py
index fdf5e4727187..1e6ddaf5e499 100644
--- a/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py
+++ b/products/tasks/backend/temporal/process_task/activities/tests/test_get_task_processing_context.py
@@ -705,6 +705,39 @@ class TestGetTaskProcessingContextActivity:
assert result.sandbox_environment_id == str(sandbox_environment.id)
assert result.allowed_domains is None
+ @pytest.mark.django_db(transaction=True)
+ @pytest.mark.parametrize(
+ "environment,expected",
+ [
+ ("internal", []),
+ ("picked_by_a_person", None),
+ (None, []),
+ ],
+ )
+ def test_a_profiled_task_takes_its_network_from_the_profile_unless_a_person_picked_the_environment(
+ self, activity_environment, test_task, environment, expected
+ ):
+ test_task.profile = "business_knowledge"
+ test_task.save(update_fields=["profile"])
+ extra_state = {}
+ if environment is not None:
+ sandbox_environment = SandboxEnvironment.objects.create(
+ team=test_task.team,
+ created_by=test_task.created_by,
+ name="Full access env",
+ network_access_level=SandboxEnvironment.NetworkAccessLevel.FULL,
+ allowed_domains=[],
+ internal=environment == "internal",
+ )
+ extra_state["sandbox_environment_id"] = str(sandbox_environment.id)
+ task_run = test_task.create_run(extra_state=extra_state)
+
+ result = async_to_sync(activity_environment.run)(
+ get_task_processing_context, GetTaskProcessingContextInput(run_id=str(task_run.id))
+ )
+
+ assert result.allowed_domains == expected
+
@pytest.mark.django_db(transaction=True)
def test_get_task_processing_context_rejects_other_users_private_sandbox_environment(
self, activity_environment, test_task
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index d2db6be5e668..ad6197241e9a 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -5,6 +5,7 @@ import base64
import asyncio
import hashlib
from collections.abc import AsyncGenerator, Iterator
+from dataclasses import replace
from datetime import UTC, datetime, timedelta
from decimal import Decimal
from typing import Any, ClassVar, cast
@@ -63,6 +64,7 @@ from products.tasks.backend.facade import (
api as tasks_facade,
contracts as tasks_contracts,
)
+from products.tasks.backend.facade.profiles import NetworkAccess, NetworkPolicy, TaskProfile, get_task_profile
from products.tasks.backend.facade.repo_selection import RepoSelectionResult
from products.tasks.backend.facade.run_config import TaskArtifactAdapter, TaskArtifactType
from products.tasks.backend.logic.services.ai_run_defaults import update_team_ai_run_preferences
@@ -126,6 +128,8 @@ from products.tasks.backend.temporal.process_task.utils import get_cached_github
from ee.billing.quota_limiting import QuotaResource
+BUSINESS_KNOWLEDGE = get_task_profile("business_knowledge")
+
# The catalog gates no model behind a rollout flag now, so the write paths that re-check
# entitlement are exercised with a stand-in rather than with whichever model is mid-rollout.
GATED_MODEL_FLAG = "tasks-test-model-gate"
@@ -10927,6 +10931,68 @@ class TestTaskRunAPI(BaseTaskAPITest):
self.assertEqual(static.json()["scripts_available"], scripts_available)
self.assertEqual(scripted.status_code, status.HTTP_200_OK if scripts_available else status.HTTP_403_FORBIDDEN)
+ @parameterized.expand(
+ [
+ ("restricted_profile_without_environment", BUSINESS_KNOWLEDGE, None, False),
+ ("restricted_profile_on_an_open_internal_environment", BUSINESS_KNOWLEDGE, True, False),
+ ("restricted_profile_on_an_open_picked_environment", BUSINESS_KNOWLEDGE, False, True),
+ (
+ "open_profile_without_environment",
+ replace(
+ BUSINESS_KNOWLEDGE, key="test.open_network", network=NetworkPolicy(levels=(NetworkAccess.FULL,))
+ ),
+ None,
+ True,
+ ),
+ ]
+ )
+ @override_settings(
+ CANVAS_ARTIFACT_ORIGIN="https://usercontent.example", ALLOWED_HOSTS=["testserver", "usercontent.example"]
+ )
+ @patch("posthog.storage.object_storage.write")
+ @patch("posthog.storage.object_storage.tag")
+ def test_html_artifact_scripts_follow_the_profile_network_of_a_profiled_run(
+ self, _name, profile: TaskProfile, internal_environment: bool | None, scripts_available: bool, *_mocks
+ ):
+ task = self.create_task()
+ task.profile = profile.key
+ task.save(update_fields=["profile"])
+ state: dict[str, Any] = {}
+ started_at = django_timezone.now()
+ if internal_environment is not None:
+ environment = SandboxEnvironment.objects.create(
+ team=self.team,
+ name="Writer env",
+ created_by=self.user,
+ network_access_level=SandboxEnvironment.NetworkAccessLevel.FULL,
+ internal=internal_environment,
+ )
+ state["sandbox_environment_id"] = str(environment.id)
+ started_at = environment.updated_at + timedelta(minutes=1)
+ run = TaskRun.objects.create(
+ task=task, team=self.team, status=TaskRun.Status.IN_PROGRESS, state=state, created_at=started_at
+ )
+
+ with patch.dict("products.tasks.backend.facade.profiles._profiles", {profile.key: profile}):
+ uploaded = self._sandbox_oauth_client(task.id).post(
+ f"/api/projects/@current/tasks/{task.id}/runs/{run.id}/artifacts/",
+ {
+ "artifacts": [
+ {"name": "report.html", "type": "output", "source": "agent_output", "content": "<p>Report</p>"}
+ ]
+ },
+ format="json",
+ )
+ self.assertEqual(uploaded.status_code, status.HTTP_200_OK)
+
+ preview = self.client.get(
+ f"/api/projects/@current/tasks/{task.id}/runs/{run.id}/artifacts/"
+ f"{uploaded.json()['artifacts'][0]['id']}/preview/"
+ )
+
+ self.assertEqual(preview.status_code, status.HTTP_200_OK)
+ self.assertEqual(preview.json()["scripts_available"], scripts_available)
+
@override_settings(
CANVAS_ARTIFACT_ORIGIN="https://usercontent.example", ALLOWED_HOSTS=["testserver", "usercontent.example"]
)