Skip to content

Commit 528fa28

Browse files
google-genai-botcopybara-github
authored andcommitted
fix(a2a): stop passing caller-chosen file URIs to the model by default
- Breaking: A2A file parts that carry a URI now reach the model as a text placeholder. Admit trusted locations with `fileUriPolicy(FileUriPolicy.allowPrefixes(...))` on `AgentExecutorConfig.Builder` or `RemoteA2AAgent.Builder`. `FileUriPolicy.allowAll()` restores the old behavior for `gs`, `http` and `https` URIs and Gemini Files API names; other schemes need `allowPrefixes(...)` or a predicate. PiperOrigin-RevId: 1031185307
1 parent 904ca71 commit 528fa28

7 files changed

Lines changed: 79 additions & 41 deletions

File tree

‎a2a/src/main/java/com/google/adk/a2a/converters/FileUriPolicy.java‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,6 @@ public String uri() {
169169
private static final FileUriPolicy ALLOW_ALL =
170170
new FileUriPolicy(
171171
true, ImmutableList.of(), ImmutableSet.of(), null, DisallowedAction.PLACEHOLDER);
172-
// The default until it becomes denyAll(): what converters admitted before, minus never-safe URIs.
173-
private static final FileUriPolicy LEGACY =
174-
new FileUriPolicy(
175-
false,
176-
ImmutableList.of(),
177-
ImmutableSet.of(),
178-
context -> true,
179-
DisallowedAction.PLACEHOLDER);
180172

181173
private final boolean allowAll;
182174
private final ImmutableList<URI> allowedPrefixes;
@@ -210,13 +202,9 @@ private FileUriPolicy(
210202
this.boundSession = boundSession;
211203
}
212204

213-
/**
214-
* The policy converters apply when none is configured. It currently admits every file URI except
215-
* those that are never safe, including schemes {@link #allowAll()} leaves out, so that existing
216-
* callers keep working until they can choose a policy.
217-
*/
205+
/** The policy converters apply when none is configured: {@link #denyAll()}. */
218206
public static FileUriPolicy defaultPolicy() {
219-
return LEGACY;
207+
return DENY_ALL;
220208
}
221209

222210
/** Admits no file URI and replaces each one with a placeholder. */

‎a2a/src/test/java/com/google/adk/a2a/agent/RemoteA2AAgentTest.java‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ public void runAsync_aggregatesFiles() {
341341

342342
@Test
343343
public void runAsync_passesAllowedPeerFileUriToModel() {
344-
RemoteA2AAgent agent = createAgent();
344+
RemoteA2AAgent agent =
345+
getAgentBuilder().streaming(true).fileUriPolicy(FileUriPolicy.allowAll()).build();
345346
mockStreamResponse(consumer -> {
346347
consumer.accept(
347348
createPartialFileEvent("http://example.com/file.txt", "text/plain"), agentCard);
@@ -356,7 +357,9 @@ public void runAsync_passesAllowedPeerFileUriToModel() {
356357

357358
@Test
358359
public void runAsync_replacesNeverSafePeerFileUriWithPlaceholder() {
359-
RemoteA2AAgent agent = createAgent();
360+
// Under allowAll(), so this checks never-safe filtering rather than the deny-all default.
361+
RemoteA2AAgent agent =
362+
getAgentBuilder().streaming(true).fileUriPolicy(FileUriPolicy.allowAll()).build();
360363
mockStreamResponse(consumer -> {
361364
consumer.accept(
362365
createPartialFileEvent(
@@ -381,6 +384,30 @@ public void runAsync_replacesNeverSafePeerFileUriWithPlaceholder() {
381384
assertText(events.get(1), placeholder);
382385
}
383386

387+
@Test
388+
public void runAsync_replacesPeerFileUriWithPlaceholderByDefault() {
389+
RemoteA2AAgent agent = createAgent();
390+
mockStreamResponse(consumer -> {
391+
consumer.accept(createPartialFileEvent("gs://victim/secret.txt", "text/plain"), agentCard);
392+
consumer.accept(createFinalEvent("Done"), agentCard);
393+
});
394+
395+
List<Event> events = agent.runAsync(invocationContext).toList().blockingGet();
396+
397+
String placeholder = "[A2A file reference not loaded: name=\"file\", mime=\"text/plain\"]";
398+
List<Part> parts =
399+
events.stream()
400+
.flatMap(event ->
401+
event.content().flatMap(Content::parts).orElse(ImmutableList.of()).stream())
402+
.toList();
403+
assertThat(parts.stream().filter(part -> part.fileData().isPresent())).isEmpty();
404+
assertText(events.get(0), placeholder);
405+
assertNotAggregated(events.get(0));
406+
// A peer placeholder is buffered like streamed text and flushed before the final event.
407+
assertAggregated(events.get(1));
408+
assertText(events.get(1), placeholder);
409+
}
410+
384411
@Test
385412
public void runAsync_withFileUriPolicy_passesAllowedPeerUri() {
386413
RemoteA2AAgent agent =
@@ -392,7 +419,7 @@ public void runAsync_withFileUriPolicy_passesAllowedPeerUri() {
392419
consumer.accept(
393420
createPartialFileEvent("gs://peer-artifacts/app/report.pdf", "application/pdf"),
394421
agentCard);
395-
// Admitted by the default policy, so only a policy that is applied keeps it out.
422+
// Outside the allowed prefix, so the applied policy keeps it out.
396423
consumer.accept(
397424
createPartialFileEvent("gs://elsewhere/app/report.pdf", "application/pdf"), agentCard);
398425
consumer.accept(createFinalEvent("Done"), agentCard);

‎a2a/src/test/java/com/google/adk/a2a/converters/FileUriPolicyNeverSafeTest.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import org.junit.runners.Parameterized;
2626
import org.junit.runners.Parameterized.Parameters;
2727

28-
/**
29-
* URIs that no policy admits, not even the default, {@link FileUriPolicy#allowAll()} or a
30-
* predicate.
31-
*/
28+
/** URIs that no policy admits, not even {@link FileUriPolicy#allowAll()} or a predicate. */
3229
@RunWith(Parameterized.class)
3330
public class FileUriPolicyNeverSafeTest {
3431

‎a2a/src/test/java/com/google/adk/a2a/converters/FileUriPolicyTest.java‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,8 @@ public void denyAll_deniesEveryUri() {
4141
}
4242

4343
@Test
44-
public void defaultPolicy_admitsEverythingButNeverSafeUris() {
45-
FileUriPolicy policy = FileUriPolicy.defaultPolicy();
46-
47-
assertThat(policy.isAllowed(context("gs://bucket/f"))).isTrue();
48-
assertThat(policy.isAllowed(context("https://example.com/f.png"))).isTrue();
49-
// Unlike allowAll(), other schemes still pass until callers can configure a policy.
50-
assertThat(policy.isAllowed(context("blobstore://test/file.pdf"))).isTrue();
51-
assertThat(policy.isAllowed(context("files/abc-123"))).isTrue();
52-
assertThat(policy.isAllowed(context("http://metadata.google.internal/x"))).isFalse();
53-
assertThat(policy.isAllowed(context("file:///etc/passwd"))).isFalse();
54-
assertThat(policy.onDisallowed()).isEqualTo(DisallowedAction.PLACEHOLDER);
44+
public void defaultPolicy_deniesEveryUri() {
45+
assertThat(FileUriPolicy.defaultPolicy()).isSameInstanceAs(FileUriPolicy.denyAll());
5546
}
5647

5748
@Test

‎a2a/src/test/java/com/google/adk/a2a/converters/PartConverterTest.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ public void messageToContent_withNeverSafeFileUri_doesNotPassUriToModel() {
119119
"http://metadata.google.internal/computeMetadata/v1/token"))))
120120
.build();
121121

122-
Content content = PartConverter.messageToContent(message);
122+
// Under allowAll(), so this checks never-safe filtering rather than the deny-all default.
123+
Content content =
124+
PartConverter.messageToContent(
125+
message, PartConverter.defaultConverter(FileUriPolicy.allowAll()));
123126

124127
ImmutableList<Part> parts = ImmutableList.copyOf(content.parts().get());
125128
assertThat(parts).hasSize(2);
@@ -129,13 +132,15 @@ public void messageToContent_withNeverSafeFileUri_doesNotPassUriToModel() {
129132
}
130133

131134
@Test
132-
public void toGenaiPart_withFilePartUri_passesUriUnderDefaultPolicy() {
135+
public void toGenaiPart_withFilePartUri_returnsPlaceholderByDefault() {
133136
FilePart filePart =
134137
new FilePart(new FileWithUri("text/plain", "file.txt", "https://example.com/file.txt"));
135138

136139
Part result = PartConverter.toGenaiPart(filePart);
137140

138-
assertThat(result.fileData().get().fileUri()).hasValue("https://example.com/file.txt");
141+
assertThat(result.fileData()).isEmpty();
142+
assertThat(result.text())
143+
.hasValue("[A2A file reference not loaded: name=\"file.txt\", mime=\"text/plain\"]");
139144
}
140145

141146
@Test
@@ -147,7 +152,7 @@ public void toGenaiPart_withNeverSafeFileUri_returnsPlaceholder() {
147152
"file.txt",
148153
"http://metadata.google.internal/computeMetadata/v1/token"));
149154

150-
Part result = PartConverter.toGenaiPart(filePart);
155+
Part result = PartConverter.defaultConverter(FileUriPolicy.allowAll()).convert(filePart).get(0);
151156

152157
assertThat(result.fileData()).isEmpty();
153158
assertThat(result.text())

‎a2a/src/test/java/com/google/adk/a2a/converters/ResponseConverterTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private TaskUpdateEvent statusUpdateEvent(
119119
}
120120

121121
private static final String PEER_URI = "gs://peer-artifacts/app/report.pdf";
122-
// Admitted by the default policy but not by ALLOW_PEER, so the two paths differ.
122+
// Outside ALLOW_PEER's prefix, so only PEER_URI distinguishes it from the default policy.
123123
private static final String OTHER_URI = "gs://elsewhere/app/report.pdf";
124124
private static final A2APartConverter ALLOW_PEER =
125125
PartConverter.defaultConverter(FileUriPolicy.allowPrefixes("gs://peer-artifacts/"));
@@ -189,7 +189,7 @@ public void clientEventToEvent_withConverter_appliesItToEveryEventKind() {
189189

190190
assertWithMessage("default, %s", clientEvent.getClass().getSimpleName())
191191
.that(fileUris(withDefault))
192-
.containsExactly(PEER_URI, OTHER_URI);
192+
.isEmpty();
193193
assertWithMessage("converter, %s", clientEvent.getClass().getSimpleName())
194194
.that(fileUris(withConverter))
195195
.containsExactly(PEER_URI);

‎a2a/src/test/java/com/google/adk/a2a/executor/AgentExecutorTest.java‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,9 @@ public void execute_propagatesRequestMetadataIntoRunConfig() {
462462
@Test
463463
public void execute_withNeverSafeCallerFileUri_doesNotPassUriToAgent() {
464464
testAgent.setEventsToEmit(Flowable.empty());
465-
AgentExecutor executor = executorWith(new InMemorySessionService());
465+
// Under allowAll(), so this checks never-safe filtering rather than the deny-all default.
466+
AgentExecutor executor =
467+
executorWith(AgentExecutorConfig.builder().fileUriPolicy(FileUriPolicy.allowAll()).build());
466468
Message message =
467469
new Message.Builder()
468470
.messageId("msg-1")
@@ -492,7 +494,8 @@ public void execute_withNeverSafeCallerFileUri_doesNotPassUriToAgent() {
492494
@Test
493495
public void execute_withAllowedCallerFileUri_passesUriToAgent() {
494496
testAgent.setEventsToEmit(Flowable.empty());
495-
AgentExecutor executor = executorWith(new InMemorySessionService());
497+
AgentExecutor executor =
498+
executorWith(AgentExecutorConfig.builder().fileUriPolicy(FileUriPolicy.allowAll()).build());
496499
Message message =
497500
new Message.Builder()
498501
.messageId("msg-1")
@@ -515,6 +518,33 @@ public void execute_withAllowedCallerFileUri_passesUriToAgent() {
515518
assertThat(parts.get(1).fileData().get().fileUri()).hasValue("https://example.com/report.txt");
516519
}
517520

521+
@Test
522+
public void execute_withCallerFileUri_replacesItWithPlaceholderByDefault() {
523+
testAgent.setEventsToEmit(Flowable.empty());
524+
AgentExecutor executor = executorWith(new InMemorySessionService());
525+
Message message =
526+
new Message.Builder()
527+
.messageId("msg-1")
528+
.role(Message.Role.USER)
529+
.parts(
530+
ImmutableList.of(
531+
new TextPart("Summarize this"),
532+
new FilePart(
533+
new FileWithUri("text/plain", "secret.txt", "gs://victim/secret.txt"))))
534+
.build();
535+
RequestContext ctx = mock(RequestContext.class);
536+
when(ctx.getMessage()).thenReturn(message);
537+
when(ctx.getTaskId()).thenReturn("task-1");
538+
when(ctx.getContextId()).thenReturn(CONTEXT_ID);
539+
540+
executor.execute(ctx, eventQueue);
541+
542+
List<Part> parts = testAgent.lastInvocationContext.userContent().get().parts().get();
543+
assertThat(parts.stream().filter(part -> part.fileData().isPresent())).isEmpty();
544+
assertThat(parts.get(1).text())
545+
.hasValue("[A2A file reference not loaded: name=\"secret.txt\", mime=\"text/plain\"]");
546+
}
547+
518548
@Test
519549
public void execute_withFileUriPolicy_passesAllowedUriToAgent() {
520550
testAgent.setEventsToEmit(Flowable.empty());
@@ -527,7 +557,7 @@ public void execute_withFileUriPolicy_passesAllowedUriToAgent() {
527557
executor.execute(
528558
requestWithFileUri("gs://shared/inbox/report.pdf", "task-allowed"), eventQueue);
529559
Part allowed = testAgent.lastInvocationContext.userContent().get().parts().get().get(1);
530-
// Admitted by the default policy, so only a policy that is applied keeps it out.
560+
// Outside the allowed prefix, so the applied policy keeps it out.
531561
executor.execute(requestWithFileUri("gs://elsewhere/report.pdf", "task-other"), eventQueue);
532562
Part other = testAgent.lastInvocationContext.userContent().get().parts().get().get(1);
533563

0 commit comments

Comments
 (0)