Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(etoro): make the copy-trading paper refusal structural, not per-call-site

Haozhe Wu6bffddeaa45c
diff --git a/agent/src/trading/connectors/etoro/client.py b/agent/src/trading/connectors/etoro/client.py
index 746bd4dea..f61414c71 100644
--- a/agent/src/trading/connectors/etoro/client.py
+++ b/agent/src/trading/connectors/etoro/client.py
@@ -244,8 +244,27 @@ def positions_root(cfg: EtoroConfig) -> str:
return "/api/v2/trading/demo" if cfg.is_paper else "/api/v2/trading"
+COPY_TRADING_PAPER_UNSUPPORTED = (
+ "eToro Public API copy trading is not available on demo (paper) accounts. "
+ "Use a live profile (e.g. etoro-live-trade) with a real account."
+)
+
+
def copy_root(cfg: EtoroConfig) -> str:
- """Copy-trading routes share one path for demo and real (no ``/demo`` prefix)."""
+ """Copy-trading routes share one path for demo and real (no ``/demo`` prefix).
+
+ Every other root above encodes paper/live in the path, which is half of this
+ connector's ``path_separated_key_bound`` guard. Copy has no demo path to
+ encode, so for this one surface the guard has to be a refusal instead: a
+ paper config has no legitimate copy route, and handing it the real one would
+ point demo credentials at a live endpoint. Raising here keeps that
+ structural rather than leaving each call site to remember it.
+
+ Raises:
+ EtoroConfigError: If ``cfg`` targets the demo (paper) environment.
+ """
+ if cfg.is_paper:
+ raise EtoroConfigError(COPY_TRADING_PAPER_UNSUPPORTED)
return "/api/v2/trading/copy"
diff --git a/agent/src/trading/connectors/etoro/copy_trading.py b/agent/src/trading/connectors/etoro/copy_trading.py
index 198c69726..a16e06473 100644
--- a/agent/src/trading/connectors/etoro/copy_trading.py
+++ b/agent/src/trading/connectors/etoro/copy_trading.py
@@ -8,16 +8,18 @@ import time
import uuid
from typing import Any
-from src.trading.connectors.etoro.client import EtoroAPIError, EtoroConfig, copy_root, make_client
-
+from src.trading.connectors.etoro.client import (
+ COPY_TRADING_PAPER_UNSUPPORTED,
+ EtoroAPIError,
+ EtoroConfig,
+ copy_root,
+ make_client,
+)
+# ``COPY_TRADING_PAPER_UNSUPPORTED`` is defined beside ``copy_root``, which is
+# what structurally enforces it; service.py still imports it from here.
_REFERENCE_ID_RE = re.compile(r"^[A-Za-z0-9._~-]{1,35}$")
-COPY_TRADING_PAPER_UNSUPPORTED = (
- "eToro Public API copy trading is not available on demo (paper) accounts. "
- "Use a live profile (e.g. etoro-live-trade) with a real account."
-)
-
def _base(cfg: EtoroConfig) -> dict[str, Any]:
return {
diff --git a/agent/tests/test_etoro_connector.py b/agent/tests/test_etoro_connector.py
index 09e1fff66..18b512c31 100644
--- a/agent/tests/test_etoro_connector.py
+++ b/agent/tests/test_etoro_connector.py
@@ -14,6 +14,7 @@ from src.trading.connectors.etoro import sdk as etoro_sdk
from src.trading.connectors.etoro.client import (
EtoroClient,
EtoroConfig,
+ EtoroConfigError,
build_config,
aggregate_portfolio_path,
copy_root,
@@ -100,12 +101,27 @@ def test_demo_and_real_paths_separated() -> None:
assert info_root(paper).endswith("/demo")
assert aggregate_portfolio_path(paper).endswith("/demo/aggregate-portfolio")
assert aggregate_portfolio_path(live) == "/api/v1/trading/info/aggregate-portfolio"
- assert copy_root(paper) == "/api/v2/trading/copy"
assert copy_root(live) == "/api/v2/trading/copy"
assert positions_root(paper).endswith("/demo")
assert paper.api_key == live.api_key
+def test_copy_root_refuses_a_paper_config() -> None:
+ """Copy has no demo path, so a paper config must get a refusal, not the real one.
+
+ Every other root encodes paper/live in the path. If ``copy_root`` simply
+ returned the real route for a paper config, the only thing keeping demo
+ credentials off the live copy endpoint would be each call site remembering
+ to check first.
+ """
+ paper = EtoroConfig(profile="paper", api_key="k", user_key="u")
+ with pytest.raises(EtoroConfigError) as excinfo:
+ copy_root(paper)
+ assert "demo (paper)" in str(excinfo.value)
+ assert copy_root(EtoroConfig(profile="live-readonly", api_key="k", user_key="u"))
+ assert copy_root(EtoroConfig(profile="live", api_key="k", user_key="u"))
+
+
def test_shared_credentials_across_paper_and_live_profiles(monkeypatch, tmp_path) -> None:
cfg_path = tmp_path / "etoro.json"
cfg_path.write_text(