Close SSRF gaps in all api_url configs and block metadata IP encodings#5
Merged
Conversation
```json
{
"title": "Close SSRF gaps in all api_url configs and block metadata IP encodings",
"body": "## Summary\n\nConsolidate `api_url` SSRF validation into a single shared helper and apply it to all six SDK configs that accept an `api_url` field — previously only the storage and client configs validated the URL while the three provider configs and `EntitiesClientConfig` accepted any string.\n\n## Changes\n\n- Add `atomicmemory/core/url.py` with a single `validate_api_url` helper that centralizes scheme/host checks and SSRF defense.\n- Wire the shared validator into all six configs: `AtomicMemoryClientConfig`, `StorageClientConfig`, `EntitiesClientConfig`, `AtomicMemoryProviderConfig`, `HindsightProviderConfig`, and `Mem0ProviderConfig`.\n- Switch to posture B (match Node SDK): loopback/private/reserved IP literals are **allowed by default** (the SDK routinely connects to local and self-hosted cores); link-local and cloud-metadata addresses (`169.254.169.254`, `fe80::/10`) are always blocked.\n- Canonicalize IPv4-mapped IPv6 addresses (`::ffff:169.254.169.254` → `169.254.169.254`) so the metadata block is deterministic across Python 3.10/3.11.\n- Canonicalize legacy IPv4 encodings (decimal `2852039166`, hex `0xA9FEA9FE`, dotted-octal `0251.0376.0251.0376`) via `socket.inet_aton` so they cannot bypass the link-local block.\n- Add `allowPrivateNetworks` field (default `True`) on every config; setting it to `False` also rejects loopback/private/reserved literals.\n- Add `tests/core/test_url.py` — 12 parametrized cases covering allowed, always-blocked, strict-mode, IPv4-mapped IPv6, whitespace stripping, and encoded-address bypass scenarios.\n- Add `tests/providers/test_config_ssrf.py` — integration coverage for each config surface plus a **reflective enumeration test** (`test_every_api_url_config_blocks_imds`) that auto-discovers every `BaseModel` subclass with an `api_url` field and fails if any future config omits the guard.\n- Bump package version to 1.1.2.\n\n## Why\n\nThe three provider configs and `EntitiesClientConfig` had no URL validation at all, so a crafted `apiUrl` could reach the AWS IMDS endpoint (`169.254.169.254`) or its non-canonical encodings (decimal, hex, octal, IPv4-mapped IPv6). The validator was also applied per-surface with duplicated logic, so adding a new config would silently inherit no protection. The shared helper closes both gaps at one chokepoint.\n\n## Validation\n\n- `uv run pytest tests/core/test_url.py tests/providers/test_config_ssrf.py` — all new tests pass.\n- The reflective enumeration test asserts ≥ 6 configs are discovered and each rejects the IMDS literal; it will fail automatically if a future config with an `api_url` field omits the validator.\n- `uv run mypy atomicmemory --strict` and `uv run ruff check .` both pass clean."
}
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Consolidate
api_urlSSRF validation into a single shared helper and apply it to all six SDK configs that accept anapi_urlfield — previously only the storage and client configs validated the URL while the three provider configs andEntitiesClientConfigaccepted any string.Changes
atomicmemory/core/url.pywith a singlevalidate_api_urlhelper that centralizes scheme/host checks and SSRF defense.AtomicMemoryClientConfig,StorageClientConfig,EntitiesClientConfig,AtomicMemoryProviderConfig,HindsightProviderConfig, andMem0ProviderConfig.169.254.169.254,fe80::/10) are always blocked.::ffff:169.254.169.254→169.254.169.254) so the metadata block is deterministic across Python 3.10/3.11.2852039166, hex0xA9FEA9FE, dotted-octal0251.0376.0251.0376) viasocket.inet_atonso they cannot bypass the link-local block.allowPrivateNetworksfield (defaultTrue) on every config; setting it toFalsealso rejects loopback/private/reserved literals.tests/core/test_url.py— 12 parametrized cases covering allowed, always-blocked, strict-mode, IPv4-mapped IPv6, whitespace stripping, and encoded-address bypass scenarios.tests/providers/test_config_ssrf.py— integration coverage for each config surface plus a reflective enumeration test (test_every_api_url_config_blocks_imds) that auto-discovers everyBaseModelsubclass with anapi_urlfield and fails if any future config omits the guard.Why
The three provider configs and
EntitiesClientConfighad no URL validation at all, so a craftedapiUrlcould reach the AWS IMDS endpoint (169.254.169.254) or its non-canonical encodings (decimal, hex, octal, IPv4-mapped IPv6). The validator was also applied per-surface with duplicated logic, so adding a new config would silently inherit no protection. The shared helper closes both gaps at one chokepoint.Validation
uv run pytest tests/core/test_url.py tests/providers/test_config_ssrf.py— all new tests pass.api_urlfield omits the validator.uv run mypy atomicmemory --strictanduv run ruff check .both pass clean.