Skip to content

fix(client): URL-encode path parameters and reject empty, '.' and '..' (#4) - #8

Merged
TARSDrakon merged 1 commit into
mainfrom
loop/4-encode-path-params
Oct 17, 2026
Merged

TARSDrakon merged 1 commit into
mainfrom
loop/4-encode-path-params

Conversation

@TARSDrakon

Copy link
Copy Markdown
Contributor

Closes #4

Lockstep twin of the TypeScript SDK path-parameter fixes (shieldcortex-sdk #5, #7, #10).

What changed

  • New private helper _seg(value) -> str in src/shieldcortex/_http.py: returns urllib.parse.quote(str(value), safe=""), and raises ValueError before any request is sent when str(value) is "" (path parameter must not be empty) or "." / ".." (path parameter must not be '.' or '..').
  • Every caller-supplied value interpolated into a URL path in client.py and async_client.py now goes through _seg(). Ids that were valid before keep the same URL. The only difference is that reserved characters (/, ?, #, %, space, non-ASCII) are now percent-encoded.
  • tests/test_parity.py normaliser unwraps {_seg(x)} → {x}, so the manifest path templates in endpoint_manifest.py are unchanged.
  • CHANGELOG [Unreleased] → ### Fixed entry.

Sites changed (26 per client, identical in client.py and async_client.py)

Method Path
GET /v1/audit/{id}
GET /v1/quarantine/{id}
POST /v1/quarantine/{id}/review
DELETE /v1/keys/{id}
DELETE /v1/invites/{id}
POST /v1/invites/{id}/resend
PATCH /v1/devices/{uuid}
PATCH / DELETE /v1/alerts/{id}
PATCH / DELETE /v1/webhooks/{id}
POST /v1/webhooks/{id}/test
GET /v1/webhooks/{id}/deliveries
PATCH / DELETE /v1/firewall-rules/{id}
PATCH / DELETE /v1/iron-dome/patterns/{id}
POST /v1/iron-dome/patterns/{id}/test
PATCH / DELETE /v1/iron-dome/policies/{id}
POST /v1/iron-dome/policies/{id}/default
GET / DELETE /v1/verify/{id}
GET /v1/audit/exports/{manifest_id}
POST /v1/audit/exports/{manifest_id}/verify
GET /v1/audit/exports/{manifest_id}/verifications

That makes 52 sites in total, which is every match of rg -n 'f"/v1/[^"]*\{' src/shieldcortex.

Sites deliberately left

  • url = f"{self._base_url}{path}" in the five _raw_* request helpers of each client: this joins the configured base URL with an already-built path. No caller-supplied segment is involved.
  • pagination.py and integrations/ (langchain.py, crewai.py) build no URL paths. They only call public client methods, which are now encoded.
  • No path is built by + concatenation, .format() or %. Query strings go through httpx params and are out of scope.

Tests added (tests/test_path_params.py)

  • _seg unit cases: plain, int, a/b?c#d → a%2Fb%3Fc%23d, 50% off → 50%25%20off, café → caf%C3%A9, and a..b / .hidden unchanged. "", . and .. raise.
  • Sync and async: an id containing /, ? and # stays one encoded segment (checked via url.raw_path, with an empty query) on GET, POST, PATCH and DELETE routes. An audit-export manifest id with a space, / and non-ASCII characters is encoded.
  • Sync and async: "", "." and ".." raise ValueError on GET, POST, PATCH and DELETE routes, with no request made (respx.calls.call_count == 0).
  • Sync and async: a..b and .hidden are accepted and sent unchanged.

Gate receipts (run by the author of this PR)

  • python3 -m venv … → exit 0
  • pip install -e '.[dev,langchain]' → exit 0
  • python -m pytest -q → exit 0, 182 passed
  • ruff check src tests/test_path_params.py tests/test_parity.py → All checks passed. ruff check tests still reports 6 F401/I001 errors in tests/test_client.py and tests/test_types.py, which were there before this PR and are untouched.
  • mypy src → Success: no issues found in 12 source files

🤖 Generated with Claude Code

#4)

Add a private `_seg()` helper in `_http.py` and route every caller-supplied
value interpolated into a URL path through it, in both the sync and async
clients (26 sites each). It percent-encodes the value as exactly one path
segment (`quote(str(value), safe="")`) and raises ValueError before any
request is sent for "", "." and "..", matching the TypeScript SDK contract.

The parity guard unwraps `{_seg(x)}` to `{x}` so manifest path templates
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TARSDrakon

Copy link
Copy Markdown
Contributor Author

Independent review (OpenAI-family lane, different model family from the implementer) at exact head 732538b15c366fdd14b1f0175ce3503b8d36e64e: VERDICT: APPROVE.

  • One helper (_seg in src/shieldcortex/_http.py) converts with str(), rejects empty / . / .., then returns quote(segment, safe=""); both clients use it at every parameterised route.
  • Reviewer exercised every parameterised public route through the real sync and async clients against a mocked transport: reserved delimiters, %, space, non-ASCII and backslash stayed in one encoded segment; plain ids are unchanged; empty / . / .. raise before any request is sent.
  • tests/test_parity.py still binds each template to a real route (removing a template in memory made its lookup fail), so the _seg rewrite did not make it vacuous.
  • CHANGELOG entry under [Unreleased] / Fixed is accurate.

Nits (non-blocking, follow-up): the new matrix omits a PUT route (set_default_iron_dome_policy) and an async manifest-id example; the PR body lists that route as POST, but code and the endpoint manifest use PUT.

GitHub CI: test on Python 3.9–3.13 all passed at this head.

@TARSDrakon
TARSDrakon merged commit d51c489 into main Oct 17, 2026
5 checks passed
TARSDrakon added a commit that referenced this pull request Oct 17, 2026
Brings in PR #8 (path-parameter encoding). CHANGELOG.md conflict in
[Unreleased] resolved by keeping both entries under one "Fixed" heading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CASE-Drakon CASE-Drakon mentioned this pull request Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Path parameters interpolated without URL-encoding (sibling of the TS SDK finding)

1 participant