Skip to content

Commit 0e7b5f6

Browse files
sobychackoilayaperumalg
authored andcommitted
Escape metadata values in Redis chat memory queries
RedisChatMemoryRepository.findByMetadata built RediSearch tag and text queries from caller-supplied input without escaping. Escape the value in the tag branch and the key and value in the general-metadata fallbacks, mirroring the existing RediSearchUtil.escape usage. Closes #6765 Signed-off-by: Soby Chacko <[email protected]>
1 parent 2d2b455 commit 0e7b5f6

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎memory-repositories/spring-ai-model-chat-memory-repository-redis/src/main/java/org/springframework/ai/chat/memory/repository/redis/RedisChatMemoryRepository.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
*
7474
* @author Brian Sam-Bodden
7575
* @author Yanming Zhou
76+
* @author Soby Chacko
7677
* @author Dimitar Proynov
7778
*/
7879
public final class RedisChatMemoryRepository implements ChatMemoryRepository, AdvancedRedisChatMemoryRepository {
@@ -727,14 +728,15 @@ public List<MessageWithConversation> findByMetadata(String metadataKey, Object m
727728

728729
catch (NumberFormatException e) {
729730
// Fall back to text search in general metadata
730-
String searchPattern = metadataKey + " " + metadataValue;
731+
String searchPattern = RediSearchUtil.escape(metadataKey) + " "
732+
+ RediSearchUtil.escape(metadataValue.toString());
731733
queryNode = QueryBuilders.intersect("metadata", Values.value(searchPattern));
732734
}
733735
}
734736
break;
735737
case "tag":
736-
// For tag fields, we don't need to escape the value
737-
queryNode = QueryBuilders.intersect(indexedFieldName, Values.tags(metadataValue.toString()));
738+
queryNode = QueryBuilders.intersect(indexedFieldName,
739+
Values.tags(RediSearchUtil.escape(metadataValue.toString())));
738740
break;
739741
case "text":
740742
default:
@@ -746,7 +748,8 @@ public List<MessageWithConversation> findByMetadata(String metadataKey, Object m
746748

747749
else {
748750
// Field not explicitly indexed - search in general metadata field
749-
String searchPattern = metadataKey + " " + metadataValue;
751+
String searchPattern = RediSearchUtil.escape(metadataKey) + " "
752+
+ RediSearchUtil.escape(metadataValue.toString());
750753
queryNode = QueryBuilders.intersect("metadata", Values.value(searchPattern));
751754
}
752755

‎memory-repositories/spring-ai-model-chat-memory-repository-redis/src/test/java/org/springframework/ai/chat/memory/repository/redis/RedisChatMemoryAdvancedQueryIT.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Brian Sam-Bodden
4545
* @author Yanming Zhou
46+
* @author Soby Chacko
4647
*/
4748
@Testcontainers
4849
class RedisChatMemoryAdvancedQueryIT {
@@ -399,6 +400,47 @@ void shouldFindMessagesByMetadata() {
399400
});
400401
}
401402

403+
@Test
404+
void shouldNeutralizeTagInjectionInFindByMetadata() {
405+
this.contextRunner.run(context -> {
406+
RedisChatMemoryRepository chatMemory = context.getBean(RedisChatMemoryRepository.class);
407+
408+
// Clear any existing test data
409+
chatMemory.findConversationIds().forEach(chatMemory::clear);
410+
411+
String conversationA = "tag-injection-a";
412+
String conversationB = "tag-injection-b";
413+
414+
// 'priority' is configured as a TAG field
415+
UserMessage messageA = new UserMessage("Message in conversation A");
416+
messageA.getMetadata().put("priority", "high");
417+
UserMessage messageB = new UserMessage("Message in conversation B");
418+
messageB.getMetadata().put("priority", "low");
419+
420+
chatMemory.add(conversationA, messageA);
421+
chatMemory.add(conversationB, messageB);
422+
423+
// Give Redis time to index the documents
424+
Thread.sleep(100);
425+
426+
// Sanity: a legitimate tag value matches only its own message
427+
assertThat(((AdvancedRedisChatMemoryRepository) chatMemory).findByMetadata("priority", "high", 10))
428+
.hasSize(1);
429+
430+
// A crafted value that, unescaped, closes the tag clause and unions a
431+
// second one: @metadata_priority:{high} | @metadata_priority:{low}
432+
// Expect: value treated as a literal tag, matching nothing
433+
List<AdvancedRedisChatMemoryRepository.MessageWithConversation> injected = ((AdvancedRedisChatMemoryRepository) chatMemory)
434+
.findByMetadata("priority", "high} | @metadata_priority:{low", 10);
435+
436+
assertThat(injected).isEmpty();
437+
438+
// Clean up
439+
chatMemory.clear(conversationA);
440+
chatMemory.clear(conversationB);
441+
});
442+
}
443+
402444
@Test
403445
void shouldExecuteCustomQuery() {
404446
this.contextRunner.run(context -> {

0 commit comments

Comments
 (0)