Skip to content

fix(server): close the TOCTOU race on a pending input (answer each input request once) - #1390

Open
joulook wants to merge 1 commit into
a2aproject:mainfrom
joulook:fix/pending-input-single-answer
Open

joulook wants to merge 1 commit into
a2aproject:mainfrom
joulook:fix/pending-input-single-answer

Conversation

@joulook

@joulook joulook commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

This closes the time-of-check-to-time-of-use race reported in #1389: an authorised client's answer could be run by the
agent against a stale revision of an input request another answer had already consumed (input integrity).

Right after authorizeTaskAccess in initMessageSend, a message to a task in INPUT_REQUIRED reserves that input
request, identified by the status timestamp (which updateWithMessage keeps), with an atomic
ConcurrentMap.compute on a per-task map holding the latest request answered. A message whose request is not newer
than that one is refused with InvalidRequestError ("The input requested by task ... has already been answered")
before its snapshot is saved and before the agent is started.

What this guarantees, within one DefaultRequestHandler instance (the reservation map is an instance field, neither
shared between handlers over one task store nor persisted across a restart): two messages admitted against the same
INPUT_REQUIRED status never both run the agent, also when one read the task before the other's turn; a message read
against an older request than one already answered is refused. Behaviour changes and limits: messages to tasks in any other non-final state (refinements) and new tasks are
not affected; status timestamps are taken as increasing per task (a new request whose timestamp is equal to or older
than the last answered one would be refused); a failure after the reservation (context construction, the save) leaves
the request reserved, with no rollback; one map entry, never removed, per task that has received an answer in that
handler.

Fixes #1389.

Tests

  • Branch fix/pending-input-single-answer = upstream main bc7f9d74 + this commit (d255f376).
  • New: TwoAnswersOneInputTest: fails 3 of 3 on 369ee867; passes 3 of 3 on the branch.
  • mvn -pl server-common test on the branch: 673 tests, 1 failure,
    DefaultRequestHandlerBuilderAuthorizationTest.emptyTaskIdWithoutEffectiveCardIsInvalid. It fails the same way on
    unpatched bc7f9d74 in the same build, so it is not caused by this change; an online build should be checked.
    On 369ee867 with the same patch: 661 tests, 0 failures.
  • Build: offline. server-common was built from source and the other modules taken from cached 1.5.2.Final-SNAPSHOT
    jars. The Error Prone/NullAway processors were removed from the build only. The four tooling versions upstream
    bumped since the pin (spotless, mapstruct-spi-protobuf, groovy, quarkus platform) were set back to the pin's values
    with -D, because the new ones are not in the offline cache.

Found with. We are a research team at MPI-SWS building Daavar, a conformance checker for distributed protocol
implementations. The race was found by a JMC model of the continuation path with an adversary client and confirmed
natively. This PR was prepared with AI assistance and reviewed by us.

Reproduce with Daavar (fixed form)
# from the Daavar root; prerequisite: the jmc submodule at 20a59d2f built, jmc/cp.txt holding its classpath
git checkout 386775c
cd benchmarks/security-pilot/p3/AB-06/java
javac -proc:none -d out -cp $(cat ../../../../../jmc/cp.txt) bench/secp3/ab06/Ab06.java
java -Xss4m -cp out:$(cat ../../../../../jmc/cp.txt) org.mpi_sws.jmc.mp.cli.MpLauncher count \
  --program bench.secp3.ab06.Ab06:fixed,conc,mon --strategy must --iterations 0 --seed 0 --assertions-as-err
# prints: iterations: 454 (no assertions line: no failed assertion)  (0 of 454)

A message that continues a task is admitted in three steps: the task is read and checked in
authorizeTaskAccess, the snapshot is saved with the message, and the agent is submitted. Nothing
recorded that an input request had already been answered, so two messages answering the same
INPUT_REQUIRED could both run the agent, and an answer that read the task before the other
answer's turn was saved and run after that turn had completed the task, displacing the first
answer from the history.

Right after authorizeTaskAccess, a message to a task in INPUT_REQUIRED now reserves that input
request, identified by its status timestamp, which updateWithMessage keeps. The reservation is an
atomic compute on a per-task map that holds the latest request answered. A message whose request
is not newer than that one is refused with InvalidRequestError before its snapshot is saved and
before the agent is started. Messages to tasks in other non-final states and new tasks are not
affected.

The reservation lives in the handler instance and is not shared between handlers or persisted
across a restart. Status timestamps are taken as increasing per task. A failure after the
reservation leaves the request reserved.

TwoAnswersOneInputTest holds one task-store read back after it happened and checks that of two
answers to one input request exactly one runs the agent and the other is refused.

Fixes: a2aproject#1389

This branch has not been deployed

No deployments
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.

Time-of-check-to-time-of-use race on a pending input: an authorised client's answer is run by the agent against a stale revision of the input

1 participant