Skip to content

improve: enforce STRANDS_TRUST_REMOTE_CODE gate in policy factory#92

Open
cagataycali wants to merge 1 commit intostrands-labs:mainfrom
cagataycali:improve/policy-trust-gate
Open

improve: enforce STRANDS_TRUST_REMOTE_CODE gate in policy factory#92
cagataycali wants to merge 1 commit intostrands-labs:mainfrom
cagataycali:improve/policy-trust-gate

Conversation

@cagataycali
Copy link
Copy Markdown
Member

Summary

The create_policy() docstring references STRANDS_TRUST_REMOTE_CODE as a mechanism for users to acknowledge the risk of loading HuggingFace models with trust_remote_code=True, but the environment variable was never actually checked — the warning was documentation-only.

This PR makes the gate real.

Changes

  • _check_trust_remote_code(provider) — new helper that raises UntrustedRemoteCodeError when a provider may execute HF remote code and STRANDS_TRUST_REMOTE_CODE is not set to 1/true/yes.
  • Applied at every code path in create_policy(): runtime registry, smart-string resolution, and standard JSON lookup.
  • Safe providers (mock, lerobot_async, groot) are allowlisted and skip the check since they never download/execute remote model code.
  • UntrustedRemoteCodeError is a new public exception for downstream error handling.

Usage

# Opt in to loading remote code
export STRANDS_TRUST_REMOTE_CODE=1

# Without the env var, HF-backed providers will raise:
# UntrustedRemoteCodeError: Policy provider 'lerobot_local' may load a HuggingFace model
# with trust_remote_code=True ...

Motivation

HuggingFace's trust_remote_code=True allows arbitrary Python execution from model repositories. Making users explicitly opt in is a standard practice (HF itself does this in transformers). This brings our policy factory in line with that pattern.

@cagataycali cagataycali requested a review from mrgh-test April 1, 2026 22:52
The docstring for create_policy() mentioned STRANDS_TRUST_REMOTE_CODE
but there was no actual enforcement — the env var was never checked.

This commit:
- Adds _check_trust_remote_code() that raises UntrustedRemoteCodeError
  when a provider listed in _HF_REMOTE_CODE_PROVIDERS is used without
  STRANDS_TRUST_REMOTE_CODE being set to 1/true/yes.
- Currently gates 'lerobot_local' (the only provider that calls
  trust_remote_code=True on HuggingFace model loading).
- Mock, groot, lerobot_async, and runtime-registered providers are
  NOT gated — only providers that actually download+execute remote
  model code.
- Exports UntrustedRemoteCodeError from strands_robots.policies.
- Updates tests to use monkeypatch for the env var where needed.
- Adds dedicated TestTrustRemoteCodeGate test class.
@cagataycali cagataycali force-pushed the improve/policy-trust-gate branch from f297922 to 53668fd Compare April 1, 2026 23:13
@cagataycali cagataycali moved this from Backlog to In review in Strands Labs - Robots Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

2 participants