adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

RestApiTool path parameters with '.' / '..' still reach the wire after quote(safe="")

Open artemkulyk opened this issue 2 months ago • 4 comments

🔴 Required Information

Describe the Bug: RestApiTool (google.adk.tools.openapi_tool) percent-encodes path parameter values with urllib.parse.quote(value, safe="") and documents that this prevents a model-supplied value from redirecting the request onto an undeclared path on the same host.

That guarantee does not hold for RFC 3986 dot-segments. quote() never encodes . (it is unreserved), so a value containing .. is sent with literal dot-dot segments joined by encoded slashes. Backends/gateways that decode %2F and then merge dot-segments can dispatch the request — with the tool's configured credentials — to a path the OpenAPI spec never declared.

>>> from urllib.parse import quote
>>> quote("../../admin/secret", safe="")
'..%2F..%2Fadmin%2Fsecret'

This is a hardening follow-up to the merged encoding fix (25f53bd). Google VRP issue 557701521 was closed as Infeasible (not tracked as a security bug) with a request to file this publicly.

Steps to Reproduce:

  1. Use an OpenAPI spec that declares only GET /files/{name} against a host that also serves an undeclared route such as GET /admin/secret.
  2. Call the generated tool with args={"name": "../../admin/secret"}.
  3. Observe the outgoing request line GET /files/..%2F..%2Fadmin%2Fsecret.
  4. On a backend that decodes %2F then merges dot-segments (e.g. a Go router over path.Clean(r.URL.Path), nginx as a gateway), the undeclared /admin/secret body is returned as the tool result.

Expected Behavior: Path parameters whose /- or \-separated segments are . or .. are rejected before any HTTP request is sent. quote(safe="") continues to encode /, ?, and #. Slash-containing IDs such as foo/bar remain encoded as foo%2Fbar.

Observed Behavior: The client emits GET /files/..%2F..%2Fadmin%2Fsecret. httpx correctly treats %2F as data, so the dot-dot sequences reach the backend unchanged.

Environment Details:

  • ADK Library Version (pip show google-adk): 2.8.0 (a119dd7751082dbbd9a65f71e359abdc2be659cc)
  • Desktop OS: macOS
  • Python Version (python -V): 3.12 / 3.13

Model Information:

  • Are you using LiteLLM: N/A (library-level RestApiTool)
  • Which model is being used: N/A

🟡 Optional Information

Regression: Present in any release that contains the quote(safe="") path-param encoding fix.

Minimal Reproduction Code:

from urllib.parse import quote
print(quote("../../admin/secret", safe=""))  # '..%2F..%2Fadmin%2Fsecret'

Additional Context:

Backend behavior for GET /files/..%2F..%2Fadmin%2Fsecret:

  • Hand-rolled Go routers that clean the decoded path (path.Clean(r.URL.Path)): request is dispatched to /admin/secret.
  • Legacy Go ServeMux (pre-1.22 / GODEBUG=httpmuxgo121=1): 301 to /admin/secret. ADK does not follow redirects; a following intermediary would complete the access.
  • Modern Go ServeMux (>= 1.22), FastAPI/Starlette direct, Envoy defaults: not affected (%2F stays one segment).
  • nginx as gateway: decode-and-merge by documented analysis.

Same host/port only (origin is fixed by the spec). Suggested client-side fix: reject . / .. as path segments, then keep quote(safe="").

artemkulyk avatar Sep 09 '26 00:09 artemkulyk

Hi @artemkulyk , Thanks for reporting this. we confirmed and reproduced the issue where quote(safe="") leaves relative dot-segments unencoded, allowing traversal segments to reach the wire. Our team is reviewing your proposed fix directly in PR #7066.

sanketpatil06 avatar Sep 10 '26 08:09 sanketpatil06

@sanketpatil06

One clarification: is it an intended security property of RestApiTool that model-supplied path parameter values must not be able to cause an authenticated request to reach a path that is not declared by the OpenAPI operation?

In other words, should the OpenAPI operation itself define the boundary of where model-controlled arguments are allowed to direct an authenticated request?

artemkulyk avatar Sep 10 '26 10:09 artemkulyk

Did you find this through a security audit, or did a malformed path actually cause a real request to go somewhere unexpected? Curious how deep you had to dig to trace the quote() escaping gap.

anythingoah avatar Sep 10 '26 12:09 anythingoah

@anythingoah

It came from a security audit, not from a real production incident.

I used an AI coding agent with some of the recently released models and let it audit ADK quite broadly for about 8 hours. It produced a lot of findings. I then went through them manually, discarded most of them, reproduced the ones that looked real, and reported a subset to Google VRP.

So the discovery process was heavily automated, but the triage and reproduction were manual. This "quote()" case was one of the findings that survived that process.

I am still genuinely curious how issues like this should be classified. If these turn out to be real gaps that were simply missed, it seems like running a similar automated audit with Gemini, or reviewing whatever automated auditing is already in place using the newer models, could be worthwhile.

artemkulyk avatar Sep 10 '26 12:09 artemkulyk