Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): keep a stored URL's userinfo when the form echoes it back

cgycoreyc6a515141905
diff --git a/agent/src/api/channels_config_routes.py b/agent/src/api/channels_config_routes.py
index c09af01dd..ad5e44d3e 100644
--- a/agent/src/api/channels_config_routes.py
+++ b/agent/src/api/channels_config_routes.py
@@ -33,6 +33,7 @@ from src.api import state as _state
from src.channels import config as _channels_config
from src.channels.bus.queue import MessageBus
from src.channels.config_meta import (
+ _strip_url_userinfo,
channel_field_hints,
is_secret_key,
split_values_secrets,
@@ -165,6 +166,34 @@ def _patch_of(name: str, body: dict[str, Any]) -> dict[str, Any]:
}
+def _drop_echoed_url_rewrites(
+ stored: dict[str, Any], patch: dict[str, Any]
+) -> dict[str, Any]:
+ """Return *patch* without values that only echo a stripped stored URL.
+
+ ``GET /channels/config`` hands out non-secret URL values with the userinfo
+ removed (``https://user:pw@host`` -> ``https://host``), and the settings form
+ writes every non-secret field back verbatim on save. Applying that echo
+ would replace the stored URL with the stripped form it was derived from,
+ destroying the embedded credential. An incoming value equal to the stripped
+ form of the stored one can only be the value this API handed out, so the
+ stored value is kept. Secret handling already works this way (a blank secret
+ keeps the stored value); ``clear_<key>`` remains the way to remove the
+ setting, and any *different* URL, including a new credential, is applied.
+ """
+ kept: dict[str, Any] = {}
+ for key, value in patch.items():
+ original = stored.get(key)
+ if (
+ isinstance(original, str)
+ and isinstance(value, str)
+ and value == _strip_url_userinfo(original)
+ ):
+ continue
+ kept[key] = value
+ return kept
+
+
def _normalize_nullable_blanks(name: str, patch: dict[str, Any]) -> dict[str, Any]:
"""Map blank form values back to None for nullable config defaults.
@@ -361,7 +390,9 @@ async def _hot_apply(name: str, section: dict[str, Any] | None) -> str:
async def _apply_update(name: str, payload: ChannelConfigUpdateRequest) -> dict[str, Any]:
"""Validate → optional enable-transition probe → write → hot apply."""
stored = _stored_section(name)
- patch = _normalize_nullable_blanks(name, _patch_of(name, payload.config))
+ patch = _drop_echoed_url_rewrites(
+ stored, _normalize_nullable_blanks(name, _patch_of(name, payload.config))
+ )
clears = _clear_flags(payload.model_extra)
merged = {**stored, **patch}
for key in clears:
diff --git a/agent/tests/test_channels_config_api.py b/agent/tests/test_channels_config_api.py
index 071196eed..0995b6200 100644
--- a/agent/tests/test_channels_config_api.py
+++ b/agent/tests/test_channels_config_api.py
@@ -535,6 +535,74 @@ def test_put_feishu_merge_patch_writes_section(tmp_path: Path, monkeypatch) -> N
assert FEISHU_STORED_SECRET not in response.text
+def test_echoed_form_patch_does_not_discard_url_userinfo(
+ tmp_path: Path, monkeypatch
+) -> None:
+ """Saving an echoed form must not strip a stored URL's embedded credentials.
+
+ ``GET /channels/config`` returns non-secret URL values with the userinfo
+ removed, and the settings form writes every non-secret field back verbatim
+ (``buildPatch``). Writing that echo to disk would silently destroy the
+ credential the stored URL carried, so the merge keeps the stored value;
+ ``clear_<key>`` remains the way to remove the setting.
+ """
+ PROXY = "http://proxy-user:[email protected]:8080"
+ client, path = _client(
+ tmp_path,
+ monkeypatch,
+ channels={"discord": {"enabled": False, "proxy": PROXY}},
+ )
+
+ entry = client.get("/channels/config").json()["channels"]["discord"]
+ assert entry["values"]["proxy"] == "http://proxy.local:8080"
+
+ patch: dict[str, Any] = {}
+ for field in entry["fields"]:
+ if field["secret"]:
+ continue
+ value = entry["values"].get(field["key"])
+ if field["type"] == "list":
+ patch[field["key"]] = value if isinstance(value, list) else []
+ elif field["type"] == "bool":
+ patch[field["key"]] = bool(value)
+ else:
+ patch[field["key"]] = "" if value is None else str(value)
+
+ response = client.put("/channels/config/discord", json={"config": patch})
+ assert response.status_code == 200, response.text
+
+ on_disk = json.loads(path.read_text(encoding="utf-8"))["channels"]["discord"]
+ assert on_disk["proxy"] == PROXY
+ assert "proxy-secret" not in response.text
+
+
+def test_changed_url_and_clear_flag_still_beat_the_echo_guard(
+ tmp_path: Path, monkeypatch
+) -> None:
+ """The echo guard must not block a real edit, nor removal of the setting."""
+ PROXY = "http://proxy-user:[email protected]:8080"
+ client, path = _client(
+ tmp_path,
+ monkeypatch,
+ channels={"discord": {"enabled": False, "proxy": PROXY}},
+ )
+
+ response = client.put(
+ "/channels/config/discord",
+ json={"config": {"proxy": "http://new-user:[email protected]:9090"}},
+ )
+ assert response.status_code == 200, response.text
+ on_disk = json.loads(path.read_text(encoding="utf-8"))["channels"]["discord"]
+ assert on_disk["proxy"] == "http://new-user:[email protected]:9090"
+
+ response = client.put(
+ "/channels/config/discord", json={"config": {}, "clear_proxy": True}
+ )
+ assert response.status_code == 200, response.text
+ on_disk = json.loads(path.read_text(encoding="utf-8"))["channels"]["discord"]
+ assert "proxy" not in on_disk
+
+
def test_put_feishu_enable_with_bad_credentials_is_blocked_before_write(
tmp_path: Path, monkeypatch
) -> None: