Skip to content

fix(rest): handle resource paths and intercepted fields - #1373

Open
ehsavoie wants to merge 1 commit into
a2aproject:mainfrom
ehsavoie:encoded_query_fields
Open

ehsavoie wants to merge 1 commit into
a2aproject:mainfrom
ehsavoie:encoded_query_fields

Conversation

@ehsavoie

Copy link
Copy Markdown
Collaborator

Validate and encode task and push notification configuration identifiers in REST URLs. Build request URLs and pagination from intercepted values, and align v0.3 push configuration names with the intercepted parent and configuration ID.

Cover interceptor changes and encoded identifiers in both REST transports. Omit pagination parameters from v0.3 push configuration list requests.

@ehsavoie
ehsavoie requested a review from kabir November 30, 2026 21:10

@redhat-chai-bot redhat-chai-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review: fix(rest): handle resource paths and intercepted fields

Nice work on this PR — it addresses an important gap by validating and URL-encoding resource identifiers before they reach the wire, and aligns the v0.3 compat transport with the interceptor model. Test coverage is thorough.

Summary of changes

v1.0 RestTransport:

  • All task/config IDs are now validated via HttpEndpoint.checkPathSegment() early, before building the request proto. This rejects path-traversal inputs (empty, .., path separators).
  • All IDs inserted into URL paths are percent-encoded via HttpEndpoint.pathSegment().
  • buildListTasksQueryString and buildListPushNotificationConfigsQueryString now derive query parameters entirely from the intercepted/effective proto, dropping the separate original parameter. Default/empty values (empty contextId, UNSPECIFIED status, empty pageToken) are no longer sent over the wire.
  • Pagination logic is extracted into a reusable addPagination() helper.

v0.3 compat RestTransport_v0_3:

  • The generic resourceUrl(name) is replaced by specific taskUrl(name), taskUrlFromId(id), configUrl(name) methods that parse resource names into their structural components and rebuild URLs with proper encoding.
  • New resource-name construction helpers (taskName, pushNotificationConfigName) and parsing (taskIdFromName, configNameSegments, resourceSegments).
  • alignPushConfigurationName() ensures the wire body's parent and config.name fields match the intercepted parent + configId, not whatever was in the original request.
  • Pagination parameters are dropped from v0.3 push config list requests (aligning with v0.3 spec behavior).
  • PayloadAndHeaders_v0_3.getHeaders() null checks removed — safe because PayloadAndHeaders_v0_3 already maps null → Collections.emptyMap() in its constructor.
  • Import cleanup and Javadoc improvements.

Tests:

  • RestInterceptorWireTest: verifies that explicit-default ListTasksParams no longer sends default values as query params.
  • RestTransport_v0_3_Test: major expansion — parameterized @ValueSource / @MethodSource tests, a new in-process HttpServer capture test (createPushConfigurationUsesInterceptedRawIdsInBodyAndEncodesUrl) verifying URL encoding for special characters like café%2E and jalapeño%2f, and verification that wire body fields are aligned after interception.

Observations

  1. Behavioral change in query string construction (v1.0): The old buildListTasksQueryString had a secondary code path that sent explicitly-set default values (empty contextId, UNSPECIFIED status, empty pageToken) over the wire. The new version omits these since proto defaults are indistinguishable from "not set." This is cleaner, but it is a subtle behavioral change — worth confirming no server implementation relies on receiving these explicit defaults.

  2. %1s format specifier: Pre-existing, not introduced here — String.format("/tasks/%1s", ...) effectively means "minimum width 1," which is a no-op for any non-empty string. Consider using plain %s for clarity in a future cleanup.

Overall this looks solid. The security hardening (path traversal prevention, URL encoding) is important, the interceptor alignment is correct, and the test coverage validates the key edge cases well.


Chai persona: A2A Java SDK

AI-generated. Review for accuracy.

@ehsavoie
ehsavoie force-pushed the encoded_query_fields branch from bccd54f to 68f0263 Compare November 30, 2026 21:38
@Override
public Task getTask(TaskQueryParams taskQueryParams, @Nullable ClientCallContext context) throws A2AClientException {
checkNotNullParam("taskQueryParams", taskQueryParams);
HttpEndpoint.checkPathSegment(taskQueryParams.id());

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.

AIUI, HttpEndpoint.checkPathSegment check that the param can not be empty, . or .... This must be true regardless of the transport for the various IDs that are checked.

Would it be possible to "move up" this check so that it is verified on any transport?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I did not find a rule there prohibiting ", /, , ?, . or ..across transports. Moving the entire check into the shared records would therefore reject IDs that JSON-RPC or gRPC may currently accept. It would also affect server-side code that constructs those records.

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.

I don't understand. IDs (for tasks and requests) must be valid regardless of the transport types, right?
It's valid to create a task with one transport (eg gRPC) and then query it later from REST.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

So there is a gap in the specs then as this should be covered. I'll move that up

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@jmesnil this has quite an impact on the server side thought

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

the 2nd commit fixes that but is it quite a big change

*/
public TaskPushNotificationConfig {
Assert.checkNotNullParam("url", url);
if (taskId != null && !taskId.isEmpty()) {

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.

I don't understand, why the call to ResourceIdValidation.check is gated. Isn't this method supposed to check the validity of the parameter?

assertEquals(
"GET /defaulttenant/tasks?contextId=&status=TASK_STATE_UNSPECIFIED&pageToken=",
queue.poll(5, TimeUnit.SECONDS));
assertEquals("GET /defaulttenant/tasks", queue.poll(5, TimeUnit.SECONDS));

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.

is the change in the assertion correct?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

contextId, status, and pageToken are protobuf scalar fields without presence tracking. After conversion, the transport cannot distinguish an explicitly supplied default value from a field an interceptor cleared. It now treats "" and TASK_STATE_UNSPECIFIED as unset and sends /tasks.
The adjacent test verifies that cleared fields are omitted too. See RestTransport.java (client/transport/rest/src/main/java/org/a2aproject/sdk/client/transport/rest/RestTransport.java:294).
The assertion and the query-serialization behavior changed in its parent commit, 68f0263 (fix(rest): handle resource paths and intercepted fields). So the behavior change is part of the branch, but it predates the current commit.


import static org.a2aproject.sdk.util.Assert.checkNotNullParam;

import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;

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.

this PR is already big enough and having sorted the import in it makes it even more difficult to properly review...

@@ -46,13 +47,7 @@ public static String pathSegment(@Nullable String value) {
* @throws IllegalArgumentException if the identifier is empty or ambiguous
*/
public static void checkPathSegment(@Nullable String value) {

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.

this method can be removed and Assert.checkResourceId(value) used directly.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree

*/
public CancelTaskParams {
Assert.checkNotNullParam("id", id);
ResourceIdValidation.check(id);

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.

we are losing the name of the parameter if its value is no longer valid. Is that ok?

return new DeleteTaskPushNotificationConfigParams(
Assert.checkNotNullParam("taskId", taskId),
Assert.checkNotNullParam("id", id),
ResourceIdValidation.require(taskId),

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.

why require here and check above?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Because of the builder pattern the id is nullable here and not in the constructor

@ehsavoie
ehsavoie force-pushed the encoded_query_fields branch from e3fec11 to 6a5558c Compare December 2, 2026 12:54

@redhat-chai-bot redhat-chai-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review: fix(rest): handle resource paths and intercepted fields (rewritten)

This is a substantial and welcome rewrite — expanding from 4 files to 32, with validation moved from the REST transport layer up to the shared spec/param layer. The architecture is sound.

Architecture summary

The PR now establishes a clean three-layer validation design:

  1. Assert.checkResourceId(name, value) (common module) — the low-level primitive that rejects null, empty, ., .., and IDs containing /, \, ?, or #. Throws IllegalArgumentException.

  2. ResourceIdValidation.check(name, id) (v1.0 spec) / ResourceIdValidation_v0_3.check(name, id) (v0.3 spec) — package-private wrappers that catch IllegalArgumentException and rethrow as InvalidParamsError / InvalidParamsError_v0_3. This translates low-level validation into the protocol-appropriate error type.

  3. Parameter records (TaskIdParams, CancelTaskParams, TaskQueryParams, DeleteTaskPushNotificationConfigParams, etc.) — now call ResourceIdValidation.check() in their constructors and builders. IDs are validated at construction time, before any transport touches them.

This is the right decision. As jmesnil pointed out, IDs must be valid regardless of transport — a task created via gRPC and queried via REST should work, so validating at the spec level catches problems early and uniformly.

Transport simplification: HttpEndpoint.checkPathSegment() is removed entirely. pathSegment(name, value) now delegates to Assert.checkResourceId() for validation, then percent-encodes. The validation is technically redundant at this point (already done at param construction) but serves as defense-in-depth.

What looks good

  • Consistent pattern across all param records. Every record that takes a resource ID now validates it the same way. The ResourceIdValidation / ResourceIdValidation_v0_3 wrappers ensure the right error type at the right layer.
  • Comprehensive test coverage. ResourceIdValidationTest and ResourceIdValidation_v0_3_Test verify that all param record types reject the same set of invalid IDs (null, empty, ., .., IDs with /, \, ?, #).
  • Server-side test updates. Tests in JSONRPCUtilsTest, GrpcHandlerTest, RestHandlerTest etc. now use valid IDs (e.g. "test-task" instead of "." or ""), ensuring they don't accidentally trip the new validation.
  • Mapper cleanups. TaskPushNotificationConfigMapper_v0_3.toV10PushConfig() extraction is cleaner. MessageSendConfigurationMapper_v0_3 now passes null for taskId instead of "", which is correct — a null taskId is semantically "not set," while "" would now fail validation.
  • v0.3 PushNotificationConfig_v0_3 test. The test verifies that null/empty id is accepted (auto-assignment) while invalid IDs are rejected. This is an important edge case.

Comments on jmesnil's open threads

Gated validation in TaskPushNotificationConfig: The gating (if (taskId != null && !taskId.isEmpty())) is correct here because taskId and id are @Nullable with semantic meaning: null/empty taskId means "not associated with a task yet" (e.g. MessageSendConfiguration path), and null/empty id means "server should auto-assign." The check ensures that if a value is provided, it's a valid resource ID. A brief doc comment explaining this would help future readers.

Parameter name change ("id" → "taskId"): The old Assert.checkNotNullParam("id", id) used the Java field name; the new ResourceIdValidation.check("taskId", id) uses the protocol-level name. This is a user-facing error message change but arguably an improvement — when the user sees "Invalid path identifier for 'taskId'" it maps directly to the protocol concept. Worth documenting that parameter names in error messages reflect the protocol role, not the Java field.

Minor observations

  1. v1.0 ResourceIdValidation.check() returns String; v0.3 ResourceIdValidation_v0_3.check() returns void. The v1.0 version returns the validated ID for fluent chaining in constructors, while v0.3 uses it as a side-effect check. This works but is a subtle asymmetry between the two versions.

  2. Import sorting in RestTransport_v0_3.java does add noise to the diff, as jmesnil noted. Not a blocker but makes the diff harder to review in the short term.

  3. Retained validation in HttpEndpoint.pathSegment(): Since all param records already validate IDs, the Assert.checkResourceId() call in pathSegment() is defense-in-depth. This is fine — belt-and-suspenders is appropriate for security-sensitive URL construction.

Overall this is a well-structured improvement that moves validation to the right architectural layer with solid test coverage. 👍


Chai persona: A2A Java SDK

AI-generated. Review for accuracy.

@ehsavoie
ehsavoie force-pushed the encoded_query_fields branch 2 times, most recently from 0bc9421 to 5e183ca Compare December 2, 2026 13:17
@ehsavoie
ehsavoie requested a review from jmesnil December 2, 2026 13:25

@kabir kabir left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. [P1] Preserve access to existing task IDs. The shared constructors now reject IDs that Task and TaskStore still accept. A task with an ID such as job?east can already exist or still be returned by an agent, but the upgraded SDK rejects GetTask for it before lookup, including over JSON-RPC and gRPC. Keep the restriction at the REST boundary, or define how existing IDs remain addressable and enforce the new rule when IDs are assigned. TaskQueryParams.java:25 · InMemoryTaskStore.java:121
  2. [P2] Preserve the explicit TASK_STATE_UNSPECIFIED filter. The REST server and task store treat this value as a concrete state filter. The old client emitted it when explicitly requested. The new client omits it, so a query for tasks in that state can return tasks in every state. Preserve field presence through interception so an explicit filter and a cleared filter produce distinct URLs. RestTransport.java:299 · InMemoryTaskStore.java:152
  3. [P2] Resolve empty v0.3 config IDs to the default. The request record accepts "", and the server documents empty and omitted IDs as selecting the default configuration. The client falls back to the task ID only for null, so "" now throws IllegalArgumentException before the interceptor runs. Apply the same fallback to both values. RestTransport_v0_3.java:239 · RestHandler_v0_3.java:255
  4. [P2] Keep interceptor-supplied v0.3 pagination. The v0.3 protobuf REST binding exposes page_size and page_token. Dropping both from the URL means a client that uses an interceptor to request a later page will receive the first page again from a server that implements pagination. RestTransport_v0_3.java:273 · v0.3 protobuf request

Validate task and push configuration IDs in shared request records, and encode REST path segments using intercepted values. Invalid required IDs now raise InvalidParamsError; HttpEndpoint.pathSegment requires an identifier name.

Build pagination from intercepted requests, preserve explicit unspecified status filters, and default missing v0.3 configuration IDs to the task ID. Omit empty list-task context and page tokens and v0.3 push-configuration pagination.

Preserve v0.3 message push configuration conversion when the task has not yet been created.

Signed-off-by: Emmanuel Hugonnet <[email protected]>
@ehsavoie
ehsavoie force-pushed the encoded_query_fields branch from 5e183ca to 70ae1d8 Compare December 2, 2026 15:09

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.

4 participants