Skip to content

fix(git): a sync path can no longer be read as a git option (abilityai/trinity-enterprise#1117) - #4294

Merged
vybe merged 13 commits into
devfrom
AndriiPasternak31/ent1117-s6a-git-add-option-injection
Nov 6, 2026
Merged

vybe merged 13 commits into
devfrom
AndriiPasternak31/ent1117-s6a-git-add-option-injection

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Part of abilityai/trinity-enterprise#1117.

Journey Impact: none: git sync refuses a path that starts with '-'; ordinary syncs are unchanged

Problem

The agent server's Push (POST /api/git/sync) staged the caller's paths with git add <paths>, so git parsed any element starting with - as an option. paths: ["-f", ".env"] force-staged the gitignored .env, and the same sync committed it and pushed it to the remote. An agent's owner, an admin, or the agent's own key can send that request through the API or the git_sync MCP tool, without shell access. CodeQL reports it as py/command-line-injection (Critical, alert #22).

Fix

  • Agent server. It stages with git add -- <paths>, so every element is a pathspec. The no-paths branch (git add -A) and the rest of the sync are unchanged.
  • Backend. The sync request refuses any paths element that starts with - with a 422, before the agent is called.
    • The message leads with the index and the element (paths[0] '-f' starts with '-' ...) and echoes at most 200 characters of it.
    • Both echoes are escaped, so an element carrying an unpaired surrogate (legal in a JSON body as \ud800) also gets a 422, not a 500.
    • This protects agents that still run an older agent server: an agent keeps its agent server until it is recreated on the new base image.
  • MCP. The git_sync tool schema refuses the same elements with a parameter error naming the element (paths.0: '-f' starts with '-' ...). It is capped the same way, counting code points like the backend, so the cut never splits a surrogate pair. The tool's JSON schema advertises it as pattern: ^(?!-).
  • CI. The MCP server tests also run when the shared path table below changes.

A dash anywhere else in a path (sub/-x.md) is still accepted, as are leading whitespace, an empty string and Unicode dashes: git reads an option only when the first byte is an ASCII -. A file whose name really starts with - can be named as ./-name.

Evidence

  • Agent server: tests/unit/test_ent1117_s6a_git_add_option_injection.py drives the real endpoint against a scratch repo with a local bare origin.
    • Before the fix, .env reached origin (assert '.env' not in {'.env', '.gitignore', 'notes.md'}). After the fix, 4 passed.
    • A subset sync pushes only the named paths: an unrelated modified tracked file stays out of the push and stays dirty locally.
    • ./-x.md stages a file whose name starts with -.
    • The no-paths branch still runs exactly git add -A.
  • Backend: tests/unit/test_ent1117_s6a_sync_paths_option_reject.py covers the model and the real router with the platform's 422 handler.
    • Before the fix, the route returned 200 and forwarded -f.
    • After the fix, the 422 leads with paths[0] '-f', the agent is never called, a dash in the middle is forwarded unchanged, and an empty body still syncs everything.
    • A 10,000-character element gives a message under 1,000 characters.
    • An element carrying a lone surrogate, at the start, in the middle, inside the echo or past it, gets a 422 whose body is valid UTF-8 JSON. Without the escaping, the first three cases were a 500.
  • MCP: src/mcp-server/src/tools/git.ent1117-s6a.test.ts validates through the same ~standard.validate call FastMCP makes. It also checks that a refusal carrying a lone surrogate serialises to valid UTF-8 JSON, and that the cap never splits a surrogate pair.
  • Shared table: the backend and MCP tests both read tests/fixtures/git-sync-path-cases.json. It has 30 inputs, including leading whitespace, '', a dash before or after a newline, seven Unicode dashes, a BOM, and lone surrogates. Both sides must decide every row the same way: for any input in the table, a test fails if one side refuses what the other accepts.
  • Repeatable runs:
    • Python, from tests/: pytest -p no:randomly unit/test_ent1117_s6a_git_add_option_injection.py unit/test_ent1117_s6a_sync_paths_option_reject.py unit/test_models_centralized.py unit/test_ent109_validation_error_no_input.py unit/test_2529_gitignore_precedence.py unit/test_1596_git_sync_observability.py unit/test_4001_no_auto_maintenance.py unit/test_agent_server_auto_sync.py unit/test_2338_journey_catalog.py gives 191 passed, 1 skipped.
    • MCP, from src/mcp-server: node --import tsx --test src/tools/git.test.ts src/tools/git.ent1117-s6a.test.ts gives 52 passed.
    • tsc --noEmit is clean.

Not covered

  • Running agents keep the old agent server until they are recreated on the new base image. Until then the backend and MCP checks are what protect them. A caller that reaches an old agent server directly, without going through the backend, is not covered by this change.
  • A .env already pushed by this route before the fix stays in the remote's history. Rotate those credentials.
  • The CodeQL re-scan. It is the only check that tells the list-form argv (["git", "add", "--", *paths]) from an equivalent concatenation, which builds the same list at runtime; no test can distinguish them.
  • Inputs outside the shared table. Backend/MCP agreement is pinned only for inputs in the table.
  • The prod backend image. The surrogate case was run with the local FastAPI/pydantic versions, not with the image's pinned FastAPI 0.115.6.
  • A sync against a live agent, on both the new and the old base image, is run separately from this PR.

…bilityai/trinity-enterprise#1117)

The Push endpoint staged the caller's paths with `git add <paths>`, so an
element such as `-f` was parsed as an option: `paths: ["-f", ".env"]`
force-staged the gitignored .env and the same sync committed and pushed it.
Stage with `git add -- <paths>`, so every element is a pathspec.

The test drives the real endpoint against a scratch repo with a local bare
origin and asserts on what reached origin; the no-paths `git add -A` branch
is pinned unchanged.
…ai/trinity-enterprise#1117)

An agent keeps the agent server it was created with until it is recreated
on a new base image, and the older server stages paths without `--`. The
backend sync request now rejects any `paths` element starting with '-' with
a 422 that names the element, before the agent is called. A dash elsewhere
in a path (`sub/-x.md`) is accepted; a file whose name starts with '-' can
be named as `./-name`.
…nity-enterprise#1117)

The git_sync tool schema now rejects any `paths` element starting with '-'
with a parameter error naming the element (`paths.<i>: '<element>' starts
with '-' ...`), so the call never reaches the backend. The constraint is
also advertised in the tool's JSON schema as `pattern: ^(?!-)`.
…yai/trinity-enterprise#1117)

The plain-paths sync test changed only the files it named, so staging
everything (`git add -A`) whenever `paths` is given passed it too. It now
also modifies an unrelated tracked file and asserts that file is not in
origin's tree at its new content and is still dirty locally. It also names
a root-level file whose name starts with '-' as `./-x.md`, the spelling the
backend's 422 recommends, and checks it is pushed.

(Abilityai/trinity-enterprise#1117)
…aths alike (Abilityai/trinity-enterprise#1117)

The backend model and the MCP tool schema each refuse a sync path that
starts with '-'. Nothing pinned that they agree, so either side could
become stricter (reject ' -f' or '') or looser (a multiline regex accepts
'-f\nx') and a caller would get different answers from the API and from
MCP. Both test files now read tests/fixtures/git-sync-path-cases.json and
must decide every row the same way: options, a lone dash, '--', dash then
NUL or newline are refused; leading whitespace, an empty string, './-x',
a dash after a slash or newline, Unicode dashes and a BOM are accepted,
because git reads an option only when the first byte is an ASCII '-'.

The "names the element" checks now require the message to lead with the
element (`paths[i] '<elem>' starts with '-'` and `'<elem>' starts with
'-'`). The old substring check passed for ["-"] even with the element
dropped, since "'-'" is already in "starts with '-'".

(Abilityai/trinity-enterprise#1117)
…inity-enterprise#1117)

A sync path that starts with '-' is refused with a message that names the
element twice: once to point at it, once in the './<name>' suggestion. The
request bounds nothing, so a 10,000-character element made a ~20 KB 422.
The backend validator and the git_sync tool schema now echo at most 200
characters of the element, followed by an ellipsis. The message still
leads with the index and the element.

(Abilityai/trinity-enterprise#1117)
…inst git (Abilityai/trinity-enterprise#1117)

The table held three of the seven Unicode dashes that were checked
against git. A backend that also refused U+2013 or U+2014, which the MCP
tool accepts, passed both suites. It now holds U+2010, U+2011, U+2013,
U+2014, U+2212, U+FE63 and U+FF0D as accepted rows, and the coverage test
requires all seven.

The file is now plain ASCII with \u escapes, so the BOM, zero-width space
and soft-hyphen rows cannot be stripped by an editor without showing in
the diff.

(Abilityai/trinity-enterprise#1117)
… a 500 (Abilityai/trinity-enterprise#1117)

A JSON body may carry an unpaired surrogate as an escape, for example
{"paths": ["-\ud800"]}. The refusal message quoted the element once
through repr() but put it raw into the './<name>' suggestion. A raw lone
surrogate cannot be encoded as UTF-8, so pydantic-core raised
UnicodeEncodeError while building the validation error and the route
answered 500 before the 422 handler ran. The suggestion now goes through
repr() too. For every ordinary element the message text is unchanged
('./-f').

The shared path table gains two refused rows with a lone surrogate and
one accepted row (a surrogate with no leading dash). A route test sends a
surrogate first, in the middle, inside the 200-character echo and past it,
and requires a 422 whose body decodes as strict UTF-8 JSON. On the MCP
side, the same elements are refused with an error that serialises to
valid UTF-8 JSON. The MCP tests pass before and after this change, because
JSON.stringify already escapes a lone surrogate.

(Abilityai/trinity-enterprise#1117)
…bilityai/trinity-enterprise#1117)

The git_sync refusal echoes at most 200 characters of the element. The
cut counted UTF-16 units, so an emoji straddling unit 200 was split and
the message carried half of a surrogate pair. The cut now counts code
points, the same unit the backend uses, so it never splits a pair.

(Abilityai/trinity-enterprise#1117)
…bilityai/trinity-enterprise#1117)

tests/fixtures/git-sync-path-cases.json is read by both the backend unit
tests and the MCP git_sync tests. The backend workflow has no path filter,
but the MCP workflow ran only on src/mcp-server/**. A change to the table
alone could therefore break the MCP side without CI noticing. The table
is now in both of that workflow's path lists.

(Abilityai/trinity-enterprise#1117)

@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 C). Approving the code.

  • git add -- <paths> in the agent server is the real fix; every other git call taking caller paths already uses --. The backend 422 and MCP check cover older base images and agree (first-character check, 200-code-point echo).
  • Mutation: reverting -- fails 2/4 including test_dash_f_cannot_force_stage_and_push_an_ignored_env; backend validator off fails 20/49; MCP regex neutralised fails 19/42.

Before merge:

  • Conflicts with dev at tests/registry.json (append next to the ent#1107 entry; mechanical).
  • frontend-e2e skipped (no ui label), which lane C asks for.

Follow-ups (both pre-existing):

  • agent_server/routers/git.py:2355 — when staging the named paths fails, sync logs a warning, commits whatever was staged, and returns success. The caller is never told the paths were dropped.
  • tests/fixtures/git-sync-path-cases.json:30 — a lone-surrogate path is accepted by backend and MCP, then raises UnicodeEncodeError in subprocess.run on the agent server (500 → backend 400). Refusing it at the 422 would make the table's "ordinary path" claim true.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

…117-s6a-git-add-option-injection

# Conflicts:
#	tests/registry.json
@AndriiPasternak31
AndriiPasternak31 marked this pull request as ready for review November 6, 2026 18:40
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

⚠️ Live-instance suite skipped — merge conflict against dev.

Resolve by merging dev locally and pushing the result; the next nightly re-tests.

@vybe
vybe merged commit 22e5942 into dev Nov 6, 2026
25 checks passed
yunyminaya pushed a commit to yunyminaya/trinity that referenced this pull request Nov 11, 2026
…i/trinity-enterprise#1117) (Abilityai#4294)

* fix(agent-server): a sync path can no longer be read as a git option (Abilityai/trinity-enterprise#1117)

The Push endpoint staged the caller's paths with `git add <paths>`, so an
element such as `-f` was parsed as an option: `paths: ["-f", ".env"]`
force-staged the gitignored .env and the same sync committed and pushed it.
Stage with `git add -- <paths>`, so every element is a pathspec.

The test drives the real endpoint against a scratch repo with a local bare
origin and asserts on what reached origin; the no-paths `git add -A` branch
is pinned unchanged.

* fix(git): the sync route refuses a path that starts with '-' (Abilityai/trinity-enterprise#1117)

An agent keeps the agent server it was created with until it is recreated
on a new base image, and the older server stages paths without `--`. The
backend sync request now rejects any `paths` element starting with '-' with
a 422 that names the element, before the agent is called. A dash elsewhere
in a path (`sub/-x.md`) is accepted; a file whose name starts with '-' can
be named as `./-name`.

* fix(mcp): git_sync refuses a path that starts with '-' (Abilityai/trinity-enterprise#1117)

The git_sync tool schema now rejects any `paths` element starting with '-'
with a parameter error naming the element (`paths.<i>: '<element>' starts
with '-' ...`), so the call never reaches the backend. The constraint is
also advertised in the tool's JSON schema as `pattern: ^(?!-)`.

* test(agent-server): a subset sync pushes only the named paths (Abilityai/trinity-enterprise#1117)

The plain-paths sync test changed only the files it named, so staging
everything (`git add -A`) whenever `paths` is given passed it too. It now
also modifies an unrelated tracked file and asserts that file is not in
origin's tree at its new content and is still dirty locally. It also names
a root-level file whose name starts with '-' as `./-x.md`, the spelling the
backend's 422 recommends, and checks it is pushed.

(Abilityai/trinity-enterprise#1117)

* test(git): the API and the git_sync tool decide one shared table of paths alike (Abilityai/trinity-enterprise#1117)

The backend model and the MCP tool schema each refuse a sync path that
starts with '-'. Nothing pinned that they agree, so either side could
become stricter (reject ' -f' or '') or looser (a multiline regex accepts
'-f\nx') and a caller would get different answers from the API and from
MCP. Both test files now read tests/fixtures/git-sync-path-cases.json and
must decide every row the same way: options, a lone dash, '--', dash then
NUL or newline are refused; leading whitespace, an empty string, './-x',
a dash after a slash or newline, Unicode dashes and a BOM are accepted,
because git reads an option only when the first byte is an ASCII '-'.

The "names the element" checks now require the message to lead with the
element (`paths[i] '<elem>' starts with '-'` and `'<elem>' starts with
'-'`). The old substring check passed for ["-"] even with the element
dropped, since "'-'" is already in "starts with '-'".

(Abilityai/trinity-enterprise#1117)

* fix(git): cap the path echoed in a refused sync request (Abilityai/trinity-enterprise#1117)

A sync path that starts with '-' is refused with a message that names the
element twice: once to point at it, once in the './<name>' suggestion. The
request bounds nothing, so a 10,000-character element made a ~20 KB 422.
The backend validator and the git_sync tool schema now echo at most 200
characters of the element, followed by an ellipsis. The message still
leads with the index and the element.

(Abilityai/trinity-enterprise#1117)

* test: register the sync-path option tests in tests/registry.json (Abilityai/trinity-enterprise#1117)

(Abilityai/trinity-enterprise#1117)

* test(git): the shared path table covers every Unicode dash probed against git (Abilityai/trinity-enterprise#1117)

The table held three of the seven Unicode dashes that were checked
against git. A backend that also refused U+2013 or U+2014, which the MCP
tool accepts, passed both suites. It now holds U+2010, U+2011, U+2013,
U+2014, U+2212, U+FE63 and U+FF0D as accepted rows, and the coverage test
requires all seven.

The file is now plain ASCII with \u escapes, so the BOM, zero-width space
and soft-hyphen rows cannot be stripped by an editor without showing in
the diff.

(Abilityai/trinity-enterprise#1117)

* fix(git): a refused sync path carrying a lone surrogate is a 422, not a 500 (Abilityai/trinity-enterprise#1117)

A JSON body may carry an unpaired surrogate as an escape, for example
{"paths": ["-\ud800"]}. The refusal message quoted the element once
through repr() but put it raw into the './<name>' suggestion. A raw lone
surrogate cannot be encoded as UTF-8, so pydantic-core raised
UnicodeEncodeError while building the validation error and the route
answered 500 before the 422 handler ran. The suggestion now goes through
repr() too. For every ordinary element the message text is unchanged
('./-f').

The shared path table gains two refused rows with a lone surrogate and
one accepted row (a surrogate with no leading dash). A route test sends a
surrogate first, in the middle, inside the 200-character echo and past it,
and requires a 422 whose body decodes as strict UTF-8 JSON. On the MCP
side, the same elements are refused with an error that serialises to
valid UTF-8 JSON. The MCP tests pass before and after this change, because
JSON.stringify already escapes a lone surrogate.

(Abilityai/trinity-enterprise#1117)

* fix(mcp): cut the echoed sync path by code points, not UTF-16 units (Abilityai/trinity-enterprise#1117)

The git_sync refusal echoes at most 200 characters of the element. The
cut counted UTF-16 units, so an emoji straddling unit 200 was split and
the message carried half of a surrogate pair. The cut now counts code
points, the same unit the backend uses, so it never splits a pair.

(Abilityai/trinity-enterprise#1117)

* ci: run the MCP server tests when the shared sync-path table changes (Abilityai/trinity-enterprise#1117)

tests/fixtures/git-sync-path-cases.json is read by both the backend unit
tests and the MCP git_sync tests. The backend workflow has no path filter,
but the MCP workflow ran only on src/mcp-server/**. A change to the table
alone could therefore break the MCP side without CI noticing. The table
is now in both of that workflow's path lists.

(Abilityai/trinity-enterprise#1117)

(cherry picked from commit 22e5942)
vybe added a commit to jordanallenlewis/determinity that referenced this pull request Nov 19, 2026
main carries 7 commits that are not ancestors of dev: the ent#1117 security
back-ports (Abilityai#4291, Abilityai#4294, Abilityai#4314, Abilityai#4315, Abilityai#4399, Abilityai#4400) and the llms.txt refresh
(Abilityai#4287). Every one of them is already on dev in its original form, so this is a
merge with dev's tree kept as-is (strategy ours). It records main as a parent so
the dev -> main release PR no longer conflicts.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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