Clients take a flow's id or key: one resolver in the SDK, id first, explicit id:/key:, a warning for reads and a refusal for changes when a name means two flows, deleted flows found by their last key; sync creates by POST, retries a create race through the drift checks and reports the id; MCP gets create_flow and resolves every flow argument with the caller's token; displays show flow_name; an engine from before ids is named as such
This commit is contained in:
@@ -6,38 +6,80 @@ import httpx
|
||||
|
||||
from fluksio.mcp import server
|
||||
|
||||
#: What the engine lists: the grandfathered `heating`, now keyed `warmth`, and
|
||||
#: the later flow that took its old key.
|
||||
LISTED = [{"id": "heating", "name": "warmth"}, {"id": "f_heat0000", "name": "heating"}]
|
||||
|
||||
def test_a_name_cannot_climb_out_of_its_route() -> None:
|
||||
"""httpx resolves ``..``, so ``save_flow("../ai/config")`` was ``PUT /ai/config``.
|
||||
|
||||
A prompt-injected assistant acting for a superuser could repoint the
|
||||
provider that way while the stored key stayed.
|
||||
"""
|
||||
def _calls(*tools) -> list[str]:
|
||||
"""What the tools sent past the flow listing, run against a recording
|
||||
engine."""
|
||||
seen: list[str] = []
|
||||
|
||||
def record(request: httpx.Request) -> httpx.Response:
|
||||
if request.url.path == "/api/v1/flows/" and request.method == "GET":
|
||||
return httpx.Response(200, json={"data": LISTED, "count": len(LISTED)})
|
||||
seen.append(f"{request.method} {request.url.raw_path.decode()}")
|
||||
return httpx.Response(200, json={})
|
||||
|
||||
async def calls() -> None:
|
||||
async def calls() -> list:
|
||||
previous = server._client
|
||||
mock = httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(record), base_url="http://test"
|
||||
)
|
||||
server.set_client(mock)
|
||||
try:
|
||||
await server.save_flow("../ai/config", {})
|
||||
await server.delete_flow("..")
|
||||
await server.get_node_source("..", "../source")
|
||||
return [await tool() for tool in tools]
|
||||
finally:
|
||||
server.set_client(previous)
|
||||
await mock.aclose()
|
||||
|
||||
asyncio.run(calls())
|
||||
answers = asyncio.run(calls())
|
||||
return seen + [answer for answer in answers if answer != {}]
|
||||
|
||||
|
||||
def test_a_name_cannot_climb_out_of_its_route() -> None:
|
||||
"""httpx resolves ``..``, so ``save_flow("../ai/config")`` was ``PUT /ai/config``.
|
||||
|
||||
A prompt-injected assistant acting for a superuser could repoint the
|
||||
provider that way while the stored key stayed. A flow is now only ever a
|
||||
listed one, and what follows it is still one segment.
|
||||
"""
|
||||
seen = _calls(
|
||||
lambda: server.save_flow("../ai/config", {}),
|
||||
lambda: server.get_node_source("f_heat0000", "../source"),
|
||||
)
|
||||
|
||||
assert seen == [
|
||||
"PUT /api/v1/flows/%2E%2E%2Fai%2Fconfig",
|
||||
"DELETE /api/v1/flows/%2E%2E",
|
||||
"GET /api/v1/flows/%2E%2E/nodes/%2E%2E%2Fsource/source",
|
||||
"GET /api/v1/flows/f_heat0000/nodes/%2E%2E%2Fsource/source",
|
||||
{"error": "No flow with the id or key '../ai/config'."},
|
||||
]
|
||||
|
||||
|
||||
def test_a_flow_argument_goes_through_the_resolver() -> None:
|
||||
"""Id first; a read of a name two flows answer to says so, a change of
|
||||
it is refused, and the explicit forms say which."""
|
||||
seen = _calls(
|
||||
lambda: server.get_flow("heating"),
|
||||
lambda: server.delete_flow("heating"),
|
||||
lambda: server.delete_flow("key:heating"),
|
||||
lambda: server.get_flow("warmth"),
|
||||
)
|
||||
|
||||
warning = (
|
||||
"'heating' is the id of 'warmth' and the key of 'f_heat0000'. "
|
||||
"Using the id. Pass key:heating for the other."
|
||||
)
|
||||
refusal = (
|
||||
"'heating' is the id of 'warmth' and the key of 'f_heat0000'. "
|
||||
"Pass id:heating or key:heating."
|
||||
)
|
||||
assert seen == [
|
||||
"GET /api/v1/flows/heating",
|
||||
"DELETE /api/v1/flows/f_heat0000",
|
||||
"GET /api/v1/flows/heating",
|
||||
{"warning": warning},
|
||||
{"error": refusal},
|
||||
]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user