Skip to content

test: pin the Slack OAuth redirect origin, the agent dashboard refusal text, and the SSH port probe's bind-only behaviour (abilityai/trinity-enterprise#1117) - #4298

Merged
vybe merged 8 commits into
devfrom
AndriiPasternak31/ent1117-s15-scanner-claim-pins
Nov 6, 2026
Merged

vybe merged 8 commits into
devfrom
AndriiPasternak31/ent1117-s15-scanner-claim-pins

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Part of abilityai/trinity-enterprise#1117.

Journey Impact: none: test-only change; no product code changes

Problem

Three behaviours that decide how the platform answers untrusted input had no regression test:

  • The unauthenticated Slack OAuth callback redirects the browser after every outcome. That redirect must always land on the configured frontend origin, whatever text arrives in the error parameter, the token-exchange error, or any field of the decoded state. test_3563_slack_oauth_redirect_base.py pins only which base is chosen.
  • The agent server's GET /api/dashboard answers a refused dashboard.yaml with "<code>: <message>" (bug(security): agent server still parses author-controlled YAML with bare safe_load — outside the ent#314 sweep #1965). Nothing pinned what that string may contain, so a change that appended a file path or a traceback would have passed.
  • is_port_available checks an SSH port by binding 0.0.0.0:port and closing again. Nothing pinned that it never listens, always closes, or keeps the wildcard address, which is what lets it see a bind on any local address.

Fix

Three test files under tests/unit/. No product code changes.

  • test_ent1117_slack_oauth_redirect_origin.py drives the real callback route through the five redirects that carry caller data, as seven cases (agent and platform where the branch differs), with 18 hostile values. The decoded state returns the hostile value for every field the test does not set, so a redirect that starts reading a new state field by key is caught. Every Location must:

    • be a 307 with exactly one Location header and no CR/LF;
    • have scheme https and host trinity.example.com, under both Python's parser and a browser-style parse that treats \ as /;
    • come from the branch under test.

    A companion test checks that the seven cases produce seven distinct, exact Location templates. Two redirects carry only constants (missing parameters, invalid state) and are not driven. The signed state is not required, which makes the test stricter than production.

  • test_ent1117_dashboard_refusal_shape.py reads the hardened loader's source and checks three things:

    • every raise in it is a bare re-raise or a call to one of its refusal constructors;
    • the refusal codes are exactly the six documented ones;
    • every message raised in the loader's own source interpolates only its limits, the duplicated key and line, and the wrapped PyYAML error.

    It then runs the real handler on nine refused documents and requires exactly "<code>: <loader message>": no traceback, no frame, no file path. Finally, it checks that a valid file returns its whole parsed content.

  • test_ent1117_port_probe_bind_only.py checks the probe from two sides:

    • Source: the function builds exactly one socket.socket(AF_INET, SOCK_STREAM) as a context manager and names no listen, accept, create_server, create_connection or low-level _socket.
    • Runtime: it records the real function's socket calls, which must be exactly settimeout, bind(("0.0.0.0", port)) and close on both the free and the busy path. It then checks that the socket is closed and nothing accepts a connection afterwards.
    • Wildcard: a bind on a second local address is seen by the wildcard probe but would be missed by a 127.0.0.1 probe. A host without a second address fails the test rather than skipping it.

Evidence

  • All 145 new test IDs pass on Python 3.12 under three random-order seeds, and on Python 3.13.
  • They pass in one process with the files that stub services.docker_service and agent_server.routers (247 passed).
  • lint_sys_modules.py, lint_root_test_placement.py and ruff check pass.
  • Each test fails when its property is broken in a scratch copy:
    • Slack callback: dropping /agents/ (55 failures); redirecting to the error value (19), to the exchange error (37), or to an origin built from the agent name at the platform-success (19), agent-success (19) or storage-failure (37) branch; returning caller text that looks like a URL (4); a raw, unquoted Location (20); reading an extra state field into the agent-success (19) or storage-failure (37) redirect.
    • Dashboard refusal: appending the file path, a traceback or the exception repr in the handler (9 each); adding the module path to a loader message, including one raised through the error class directly (5 and 1); a new refusal code, including one raised through the error class (1 and 2); a refusal raised from a prebuilt instance (1); interpolating the document into a message (2).
    • Port probe: calling listen() (2); binding 127.0.0.1 (3); not closing the socket (3); leaking a listening socket (4); setting SO_REUSEADDR (3); briefly listening on a low-level socket (1).
  • The files run in backend-unit-test, which collects tests/unit/ on every pull request and push to dev and main, with no path filter.

Not covered

  • The second-local-address check has only run on macOS. On Linux it uses 127.0.0.2, and this PR's CI run is its first Linux run.
  • backend-unit-test's regression diff runs on every pull request but is not a required status check on dev. A red result does not by itself block a merge.
  • The two redirects that carry only constants (missing parameters, invalid state) are not driven.
  • The tests check the code paths in-process. They do not exercise nginx in front of the Slack route, or the agent container image.

…igin (Abilityai/trinity-enterprise#1117)

Drives the real unauthenticated callback route through all of its redirect
branches with 18 hostile values in the error parameter, the token-exchange
error and the state's agent_name/source. Every Location must keep the
configured scheme and host under both Python's and a browser-style parse,
carry no CR/LF, add no header, and come from the branch under test.
… sentence (Abilityai/trinity-enterprise#1117)

Pins that the loader's refusal codes are a closed set, that each refusal
message interpolates only the loader's limits, the offending key and line,
and the wrapped PyYAML error, and that the agent server's dashboard handler
returns exactly '<code>: <loader message>' with no traceback, frame or path,
while a valid file returns its whole parsed content.
…nd closes (Abilityai/trinity-enterprise#1117)

Records the real is_port_available's socket calls (exactly settimeout,
bind to 0.0.0.0:port, close, on the free and busy paths), checks the socket
is closed and nothing accepts a connection afterwards, and shows the
wildcard bind sees a bind on another local address that a 127.0.0.1 probe
would miss.
…ise (Abilityai/trinity-enterprise#1117)

The source scan now also matches refusals built with HardenedYamlError
directly, and a new test asserts every raise in the loader is either a bare
re-raise or a call to one of its refusal constructors, so a refusal raised
any other way can no longer sit outside the code and message checks.
Abilityai/trinity-enterprise#1117)

Every state key the test does not set now returns the hostile value and
reads as present, so a redirect that starts reading a new state field fails
the origin check. The test covers the five redirects that carry caller
data, as seven cases (agent and platform where the branch differs); the two
redirects that carry only constants are not driven.
…mes no listener (Abilityai/trinity-enterprise#1117)

Adds a source check that is_port_available constructs exactly one
socket.socket(AF_INET, SOCK_STREAM) as a context manager and names no
listen, accept, create_server, create_connection or low-level _socket, which
the runtime recorder cannot see.

@obasilakis obasilakis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed (code review + /validate-pr, lane A). Approving.

Mutation checks all go red: Slack route taking its base from state, port probe calling listen() or binding 127.0.0.1, refusal text appended. Code under test is already on dev, so merge order versus #4293/#4294 is free.

Non-blocking:

  • test_ent1117_dashboard_refusal_shape.py:77 — the allowlist trusts the name e. A new except OSError as e: raise err(code, f"...{e}") arm would pass and put a filesystem path in the refusal. Matching on the caught exception type closes it.
  • Brittleness, each fails on a harmless edit: :127 requires node.cause is None (adding from e fails it); :136-142 require inline f-string literals; :184-188 hardcode the 262144/64 caps; test_ent1117_slack_oauth_redirect_origin.py:697-700 pins the header set to exactly {content-length, location}; test_ent1117_port_probe_bind_only.py:415,427 pin settimeout(1) and :373-394 pin the literal socket.socket(socket.AF_INET, socket.SOCK_STREAM).
  • test_ent1117_port_probe_bind_only.py:453-460 fails (does not skip) on an offline macOS laptop.

@AndriiPasternak31
AndriiPasternak31 marked this pull request as ready for review November 6, 2026 18:40
@vybe
vybe merged commit 1e3d6c4 into dev Nov 6, 2026
23 checks passed
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.

3 participants