Skip to content

Building AI agents, atomically

Hide the FXMacroData key in config and outputs, validate pagination and base_url

Robert Tidball0ed527b4756f
diff --git a/atomic-forge/tools/fxmacrodata/README.md b/atomic-forge/tools/fxmacrodata/README.md
index 8f89fd8..1091463 100644
--- a/atomic-forge/tools/fxmacrodata/README.md
+++ b/atomic-forge/tools/fxmacrodata/README.md
@@ -17,8 +17,8 @@ USD releases, the USD release calendar and the indicator catalogue work without
2. Or copy the `tool/` folder directly into your project.
## Configuration
-- `api_key` (str): FXMacroData API key, sent in the `X-API-Key` header. Falls back to the `FXMACRODATA_API_KEY` environment variable; surrounding whitespace is ignored. Leave empty for keyless USD access. The key never appears in `url` or `error`.
-- `base_url` (str): API base URL (default `https://api.fxmacrodata.com/v1`). Must be HTTPS; redirects are not followed, so the key is only ever sent to this origin.
+- `api_key` (secret str): FXMacroData API key, hidden from the config repr, sent in the `X-API-Key` header. Falls back to the `FXMACRODATA_API_KEY` environment variable; surrounding whitespace is ignored. Leave empty for keyless USD access. The key never appears in `url`, `error` or any other output field, even if the API echoes it.
+- `base_url` (str): API base URL (default `https://api.fxmacrodata.com/v1`). Must be an `https://` URL with a host, checked when the config is created; redirects are not followed, so the key is only ever sent to this origin.
- `timeout` (float): HTTP timeout in seconds (default 30).
## Input & Output Structure
diff --git a/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py b/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py
index 0e0f725..ecd350c 100644
--- a/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py
+++ b/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
import pytest
import requests
+from pydantic import ValidationError
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
@@ -215,12 +216,49 @@ def test_http_error_without_json_body(tool):
assert output.error == "FXMacroData returned 502 Bad Gateway"
-def test_non_https_base_url_is_refused():
- tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET, base_url="http://api.fxmacrodata.com/v1"))
- with patch("tool.fxmacrodata.requests.get") as mock_get:
+# Regression: base_url problems used to surface mid-request (InvalidURL/ValueError); they now fail when the
+# config is built, so they can never reach a request.
+ "base_url",
+ ["https://", "https:///v1", "http://api.fxmacrodata.com/v1", "api.fxmacrodata.com/v1", "ftp://api.fxmacrodata.com", ""],
+)
+def test_invalid_base_url_is_rejected_by_config(base_url):
+ with pytest.raises(ValidationError, match="base_url must be an https:// URL with a host"):
+ FXMacroDataToolConfig(api_key=SECRET, base_url=base_url)
+
+
+def test_base_url_is_normalised_by_config():
+ assert FXMacroDataToolConfig(base_url=" https://example.test/v1/ ").base_url == "https://example.test/v1"
+
+
+# Regression: the full key appeared in the config's repr() and str().
+def test_config_repr_and_str_hide_the_key():
+ config = FXMacroDataToolConfig(api_key=SECRET)
+ assert SECRET not in repr(config)
+ assert SECRET not in str(config)
+ assert SECRET not in config.model_dump_json()
+
+
+# Regression: a successful response echoing the key copied it into metadata and the output repr.
+def test_key_echoed_in_a_successful_response_is_redacted():
+ payload = {"data": [{"note": f"row {SECRET}"}], "detail": "echo " + SECRET, f"field-{SECRET}": 1}
+ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET))
+ with patch("tool.fxmacrodata.requests.get", return_value=_response(200, payload)):
output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))
- mock_get.assert_not_called()
- assert output.error == "The FXMacroData base URL must use HTTPS."
+
+ assert output.error is None
+ assert SECRET not in repr(output)
+ assert SECRET not in output.model_dump_json()
+ assert output.metadata["detail"] == "echo [redacted]"
+ assert output.data == [{"note": "row [redacted]"}]
+
+
+def test_key_echoed_in_a_catalogue_response_is_redacted():
+ payload = {"gdp": {"name": "GDP", "note": SECRET}}
+ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET))
+ with patch("tool.fxmacrodata.requests.get", return_value=_response(200, payload)):
+ output = tool.run(FXMacroDataToolInputSchema(endpoint="catalogue", currency="usd"))
+ assert output.data == {"gdp": {"name": "GDP", "note": "[redacted]"}}
# Regression: a 302 from the HTTPS API to an unrelated HTTP host must not carry X-API-Key there.
@@ -314,6 +352,25 @@ def test_malformed_200_responses_return_an_error(tool, endpoint, payload, messag
assert output.data == []
+# Regression: a present but malformed pagination value was normalised away and returned error=None.
+ "pagination",
+ [[], False, 0, "", "page 1", {"has_more": "yes"}, {"total_count": True}, {"next_offset": "2"}, {"limit": 1.5}],
+)
+def test_present_malformed_pagination_is_an_error(tool, pagination):
+ with patch("tool.fxmacrodata.requests.get", return_value=_response(200, {"data": [], "pagination": pagination})):
+ output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))
+ assert "unexpected response body" in output.error
+ assert output.pagination is None
+
+
+def test_null_pagination_is_treated_as_absent(tool):
+ with patch("tool.fxmacrodata.requests.get", return_value=_response(200, {"data": [], "pagination": None})):
+ output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))
+ assert output.error is None
+ assert output.pagination is None
+
+
def test_non_json_200_response_returns_an_error(tool):
response = _response(200, None)
response.json.side_effect = ValueError("Expecting value")
diff --git a/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py b/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py
index 8a1b4bd..634a9a4 100644
--- a/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py
+++ b/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py
@@ -6,7 +6,7 @@ from typing import Dict, List, Literal, Optional, Tuple, Union
from urllib.parse import urlsplit
import requests
-from pydantic import Field, JsonValue
+from pydantic import Field, JsonValue, SecretStr, field_validator
from atomic_agents import BaseIOSchema, BaseTool, BaseToolConfig
@@ -35,6 +35,18 @@ FIELD_RULES = {
JsonObject = Dict[str, JsonValue]
+# Documented pagination fields and the exact JSON types each may hold. bool is listed
+# separately because it is a subclass of int and must not pass as a count.
+PAGINATION_TYPES = {
+ "limit": {int},
+ "offset": {int},
+ "returned_count": {int},
+ "total_count": {int},
+ "next_offset": {int, type(None)},
+ "has_more": {bool},
+ "page_includes_latest_available": {bool},
+}
+
class FXMacroDataError(Exception):
"""A request or response problem, worded so it is safe to hand back to an agent."""
@@ -111,13 +123,23 @@ class FXMacroDataToolOutputSchema(BaseIOSchema):
class FXMacroDataToolConfig(BaseToolConfig):
"""Configuration for the FXMacroDataTool."""
- api_key: str = Field(
- default="",
+ api_key: SecretStr = Field(
+ default=SecretStr(""),
description="FXMacroData API key. Falls back to the FXMACRODATA_API_KEY environment variable. Optional for USD data.",
)
base_url: str = Field(default="https://api.fxmacrodata.com/v1", description="FXMacroData API base URL (HTTPS only).")
timeout: float = Field(default=30.0, ge=1.0, le=120.0, description="HTTP request timeout in seconds.")
+ @field_validator("base_url")
+ @classmethod
+ def require_https_host(cls, value: str) -> str:
+ """Reject anything but an https:// URL with a host, so a bad value fails here rather than mid-request."""
+ base_url = value.strip().rstrip("/")
+ parts = urlsplit(base_url)
+ if parts.scheme != "https" or not parts.hostname:
+ raise ValueError("base_url must be an https:// URL with a host, e.g. https://api.fxmacrodata.com/v1")
+ return base_url
+
####################
# REQUEST BUILDING #
@@ -199,6 +221,15 @@ def response_object(payload: JsonValue) -> JsonObject:
return payload
+def valid_pagination(value: JsonValue) -> bool:
+ """Absent or null is fine; anything else must be an object whose documented fields have their documented types."""
+ if value is None:
+ return True
+ if not isinstance(value, dict):
+ return False
+ return all(type(value[name]) in PAGINATION_TYPES[name] for name in PAGINATION_TYPES.keys() & value.keys())
+
+
def metadata_fields(body: JsonObject) -> JsonObject:
return {name: value for name, value in body.items() if name not in ("data", "pagination")}
@@ -206,10 +237,10 @@ def metadata_fields(body: JsonObject) -> JsonObject:
def parse_rows(payload: JsonValue) -> Tuple[List[JsonObject], Optional[JsonObject], JsonObject]:
"""Split a row endpoint's body into rows, pagination and the remaining metadata."""
body = response_object(payload)
- rows, pagination = body.get("data"), body.get("pagination") or {}
- if not is_object_list(rows) or not isinstance(pagination, dict):
+ rows, pagination = body.get("data"), body.get("pagination")
+ if not is_object_list(rows) or not valid_pagination(pagination):
raise unexpected_body(body)
- return rows, pagination or None, metadata_fields(body)
+ return rows, pagination, metadata_fields(body)
def parse_catalogue(payload: JsonValue) -> JsonObject:
@@ -228,23 +259,29 @@ class FXMacroDataTool(BaseTool[FXMacroDataToolInputSchema, FXMacroDataToolOutput
def __init__(self, config: FXMacroDataToolConfig = FXMacroDataToolConfig()):
super().__init__(config)
- self.api_key = (config.api_key or os.getenv("FXMACRODATA_API_KEY", "")).strip()
- self.base_url = config.base_url.strip().rstrip("/")
+ self._api_key = (config.api_key.get_secret_value() or os.getenv("FXMACRODATA_API_KEY", "")).strip()
+ self.base_url = config.base_url
self.timeout = config.timeout
def redact(self, message: str) -> str:
"""Remove the API key from any text that leaves the tool."""
- return message.replace(self.api_key, "[redacted]") if self.api_key else message
+ return message.replace(self._api_key, "[redacted]") if self._api_key else message
+
+ def redact_json(self, value: JsonValue) -> JsonValue:
+ """Redact the API key from every string in a response body, keys included."""
+ if isinstance(value, list):
+ return [self.redact_json(item) for item in value]
+ if isinstance(value, dict):
+ return self.redact_object(value)
+ return self.redact(value) if isinstance(value, str) else value
+
+ def redact_object(self, value: JsonObject) -> JsonObject:
+ return {self.redact(name): self.redact_json(item) for name, item in value.items()}
def headers(self) -> Dict[str, str]:
- if self.api_key and not API_KEY_PATTERN.match(self.api_key):
+ if self._api_key and not API_KEY_PATTERN.match(self._api_key):
raise FXMacroDataError("The FXMacroData API key contains characters that cannot be sent in an HTTP header.")
- return {"Accept": "application/json", **({"X-API-Key": self.api_key} if self.api_key else {})}
-
- def request_url(self, path: str) -> str:
- if urlsplit(self.base_url).scheme != "https":
- raise FXMacroDataError("The FXMacroData base URL must use HTTPS.")
- return f"{self.base_url}{path}"
+ return {"Accept": "application/json", **({"X-API-Key": self._api_key} if self._api_key else {})}
def fetch(self, url: str, query: Dict[str, str]) -> JsonValue:
"""GET one URL. Redirects are never followed, so the key only goes to the configured HTTPS origin."""
@@ -271,9 +308,9 @@ class FXMacroDataTool(BaseTool[FXMacroDataToolInputSchema, FXMacroDataToolOutput
def query_endpoint(self, params: FXMacroDataToolInputSchema) -> FXMacroDataToolOutputSchema:
path, query = build_request(params)
- url = self.request_url(path)
+ url = f"{self.base_url}{path}"
display_url = requests.Request("GET", url, params=query).prepare().url
- payload = self.fetch(url, query)
+ payload = self.redact_json(self.fetch(url, query))
if params.endpoint == "catalogue":
return FXMacroDataToolOutputSchema(endpoint=params.endpoint, url=display_url, data=parse_catalogue(payload))
rows, pagination, metadata = parse_rows(payload)