Skip to content

Commit fefe87c

Browse files
kvmiloscopybara-github
authored andcommitted
fix: omit caller values from VertexAiSessionService error messages
PiperOrigin-RevId: 1002253415
1 parent d675e72 commit fefe87c

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎core/src/main/java/com/google/adk/sessions/VertexAiSessionService.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public Completable deleteSession(String appName, String userId, String sessionId
310310
if (!userId.equals(ownerUserId)) {
311311
return Completable.error(
312312
new SecurityException(
313-
"Session " + sessionId + " does not belong to user " + userId + "."));
313+
"The requested session does not belong to the requesting user."));
314314
}
315315
return client.deleteSession(reasoningEngineId, sessionId);
316316
});
@@ -345,9 +345,7 @@ static String parseReasoningEngineId(String appName) {
345345

346346
if (!matcher.matches()) {
347347
throw new IllegalArgumentException(
348-
"App name "
349-
+ appName
350-
+ " is not valid. It should either be the full"
348+
"The app name is not valid. It should either be the full"
351349
+ " ReasoningEngine resource name, or the reasoning engine id.");
352350
}
353351

@@ -363,11 +361,7 @@ static String parseReasoningEngineId(String appName) {
363361
static void validateSessionId(String sessionId) {
364362
if (sessionId == null || !SESSION_ID_PATTERN.matcher(sessionId).matches()) {
365363
throw new IllegalArgumentException(
366-
"Invalid session id: "
367-
+ sessionId
368-
+ ". It must match "
369-
+ SESSION_ID_PATTERN.pattern()
370-
+ ".");
364+
"Invalid session id. It must match " + SESSION_ID_PATTERN.pattern() + ".");
371365
}
372366
}
373367

‎core/src/test/java/com/google/adk/sessions/VertexAiSessionServiceTest.java‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -573,21 +573,29 @@ public void getSession_wrongUser_returnsEmpty() {
573573

574574
@Test
575575
public void deleteSession_wrongUser_deniedAndSessionKept() {
576+
sessionMap.put("victim-session", mockSessionJson("victim-session", "2024-12-12T12:12:12Z"));
576577
// The ownership error surfaces on subscription, so hoist the Completable out.
577-
Completable deletion = vertexAiSessionService.deleteSession("123", "attacker", "1");
578-
assertThrows(SecurityException.class, deletion::blockingAwait);
578+
Completable deletion =
579+
vertexAiSessionService.deleteSession("123", "attacker", "victim-session");
580+
SecurityException exception = assertThrows(SecurityException.class, deletion::blockingAwait);
581+
assertThat(exception).hasMessageThat().doesNotContain("attacker");
582+
assertThat(exception).hasMessageThat().doesNotContain("victim-session");
579583
// The session is still readable by its real owner, i.e. it was not deleted.
580584
assertThat(
581-
vertexAiSessionService.getSession("123", "user", "1", Optional.empty()).blockingGet())
585+
vertexAiSessionService
586+
.getSession("123", "user", "victim-session", Optional.empty())
587+
.blockingGet())
582588
.isNotNull();
583589
}
584590

585591
// Session id validation is synchronous, so each call below throws before returning a stream.
586592
@Test
587593
public void getSession_invalidSessionId_throws() {
588-
assertThrows(
589-
IllegalArgumentException.class,
590-
() -> vertexAiSessionService.getSession("123", "user", "1/../2", Optional.empty()));
594+
IllegalArgumentException exception =
595+
assertThrows(
596+
IllegalArgumentException.class,
597+
() -> vertexAiSessionService.getSession("123", "user", "1/../2", Optional.empty()));
598+
assertThat(exception).hasMessageThat().doesNotContain("1/../2");
591599
}
592600

593601
@Test
@@ -612,6 +620,15 @@ public void appendEvent_invalidSessionId_throws() {
612620
IllegalArgumentException.class, () -> vertexAiSessionService.appendEvent(session, event));
613621
}
614622

623+
@Test
624+
public void getSession_invalidAppName_throws() {
625+
IllegalArgumentException exception =
626+
assertThrows(
627+
IllegalArgumentException.class,
628+
() -> vertexAiSessionService.getSession("customer-app", "user", "1", Optional.empty()));
629+
assertThat(exception).hasMessageThat().doesNotContain("customer-app");
630+
}
631+
615632
@Test
616633
public void listEvents_empty() {
617634
assertThat(vertexAiSessionService.listEvents("789", "user1", "3").blockingGet().events())

0 commit comments

Comments
 (0)