Skip to content

Agent Mesh

sdk: a dot segment spelled %2e is a dot segment

Antonio Ojea95aad4be0316
diff --git a/sdk/js/src/libp2p-http.test.ts b/sdk/js/src/libp2p-http.test.ts
index 83350c15..aec23b19 100644
--- a/sdk/js/src/libp2p-http.test.ts
+++ b/sdk/js/src/libp2p-http.test.ts
@@ -31,6 +31,7 @@ import { ROLE_NODE } from "./controlplane.ts";
import {
HTTP_PROTOCOL,
a2aEndpoint,
+ admitIngress,
fetchOverStream,
httpIngressHandler,
httpRequestOverStream,
@@ -318,6 +319,19 @@ test("the ingress rejects a request without a biscuit and a dotted path", async
assert.equal(status, 400);
});
+test("a dot segment is refused however it is spelled", async () => {
+ const endpoint = a2aEndpoint({ handler: () => new Response() });
+ // Nothing in options is consulted before the path check.
+ const options = providerOptions(new Uint8Array());
+ for (const target of ["/a2a/agent/../x", "/a2a/agent/./x", "/a2a/agent/%2e%2e/x", "/a2a/agent/%2E%2E/x", "/a2a/agent/.%2e/x", "/a2a/agent/%2e/x", "/a2a/%2e%2e/other/x?q=1"]) {
+ assert.deepEqual(await admitIngress({ target, headers: new Headers(), remotePeer: "peer" }, endpoint, options), { status: 400, text: "Invalid path" }, target);
+ }
+ // Not dot segments: the request reaches the next check, the missing biscuit.
+ for (const target of ["/a2a/agent/%2e%2ex/x", "/a2a/agent/..x/x", "/a2a/agent/x?p=../y"]) {
+ assert.deepEqual(await admitIngress({ target, headers: new Headers(), remotePeer: "peer" }, endpoint, options), { status: 401, text: "Missing X-Sam-Biscuit header" }, target);
+ }
+});
+
test("mesh URLs name a peer and a service", () => {
const peer = "12D3KooWJ2Yhy3CKwVPDw7AnkxN5HbZbb65xDoRif54hdXXUzh1U";
assert.deepEqual(splitMeshURL(new URL(`http://mesh/sam/${peer}/a2a/agent`)), { peerId: peer, target: "/a2a/agent" });
diff --git a/sdk/js/src/libp2p-http.ts b/sdk/js/src/libp2p-http.ts
index f7276c7e..ab1672bd 100644
--- a/sdk/js/src/libp2p-http.ts
+++ b/sdk/js/src/libp2p-http.ts
@@ -121,8 +121,13 @@ export interface ProviderOptions extends ProviderAuthorizerOptions {
onAuthorized?(peerId: string, verified: VerifiedBiscuit, targetService: string): void;
}
+// A URL parser reads %2e as a dot too (WHATWG URL, path state), so the check
+// sees what the parser and the backend will see.
function hasDotSegment(path: string): boolean {
- return path.split("/").some((seg) => seg === "." || seg === "..");
+ return path.split("/").some((seg) => {
+ const s = seg.replace(/%2e/gi, ".");
+ return s === "." || s === "..";
+ });
}
/** What the ingress looks at before anything reaches the agent. */
diff --git a/sdk/python/src/agent_mesh/libp2p_http.py b/sdk/python/src/agent_mesh/libp2p_http.py
index f773bb92..f470fa0d 100644
--- a/sdk/python/src/agent_mesh/libp2p_http.py
+++ b/sdk/python/src/agent_mesh/libp2p_http.py
@@ -22,6 +22,7 @@ from __future__ import annotations
import base64
import json
import logging
+import re
from dataclasses import dataclass, field
from typing import AsyncIterator, Awaitable, Callable, Mapping, Optional, Sequence, Union
@@ -114,8 +115,13 @@ class ProviderOptions:
on_authorized: Optional[Callable[[str, VerifiedBiscuit, str], None]] = None
+_DOT_ENCODED = re.compile("%2e", re.IGNORECASE)
+
+
def _has_dot_segment(path: str) -> bool:
- return any(seg in (".", "..") for seg in path.split("/"))
+ # A URL parser reads %2e as a dot too (WHATWG URL, path state), so the
+ # check sees what the backend will see.
+ return any(_DOT_ENCODED.sub(".", seg) in (".", "..") for seg in path.split("/"))
async def _read_http_request(stream: INetStream, conn: h11.Connection, limit: int) -> tuple[h11.Request, bytes]:
diff --git a/sdk/python/tests/test_libp2p_http.py b/sdk/python/tests/test_libp2p_http.py
index e0274334..a7a6adff 100644
--- a/sdk/python/tests/test_libp2p_http.py
+++ b/sdk/python/tests/test_libp2p_http.py
@@ -211,6 +211,9 @@ def test_the_agent_is_reachable_by_authorized_callers(backend_url):
assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://other", "/card")).status == 404
assert (await http_request_over_stream(caller, pid, b"", "a2a://agent", "/card")).status == 401
assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/../other/x")).status == 400
+ # A URL parser reads %2e as a dot too; the spelling does not get past the check.
+ assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/%2e%2e/other/x")).status == 400
+ assert (await http_request_over_stream(caller, pid, caller_biscuit, "a2a://agent", "/.%2E/other/x")).status == 400
nursery.cancel_scope.cancel()
async def with_timeout():