Skip to content

.Net: Fix #6030 - Mitigating Prompt Injection in Liquid Templates#6048

Merged
dmytrostruk merged 24 commits intomicrosoft:feature-promptyfrom
LittleLittleCloud:u/xiaoyun/fix#6030
May 7, 2024
Merged

.Net: Fix #6030 - Mitigating Prompt Injection in Liquid Templates#6048
dmytrostruk merged 24 commits intomicrosoft:feature-promptyfrom
LittleLittleCloud:u/xiaoyun/fix#6030

Conversation

@LittleLittleCloud
Copy link
Copy Markdown
Contributor

@LittleLittleCloud LittleLittleCloud commented Apr 29, 2024

Motivation and Context

#6030

Description

In this implementation, the Ġ will be reserved in liquid template which is used to replace : in all input variables when unsafe content is not allowed.

The encoding process for input variables when unsafe content is not allowed is
- replace all : to Ġ // this is the extra step comparing with HandlerBar Template
- Encode xml using HttpUtility.HtmlEncode

The decoding process is
- replace all Ġ to :

This PR introduces a new process to mitigate potential prompt injection attacks from input variables when using liquid templates. Here's a breakdown of the steps:

Before rendering, each input variable undergoes a transformation: all occurrences of :are replaced with :. This ensures that message tags like system:, user:, or assistant: are not present if AllowUnsafeContent is set to false. No replacement occurs if AllowUnsafeContent is true.
After rendering, each message content is processed based on the AllowUnsafeContent setting. If it's false, all : instances are reverted back to :, followed by calling html_encode on each message content. If AllowUnsafeContent is true, only html_encode is called. This additional encoding step is necessary because ChatPromptParser always decodes XML message content, requiring the liquid template to undergo an extra encoding step to ensure the rendered content matches the original before rendering.

Contribution Checklist

@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel documentation labels Apr 29, 2024
@LittleLittleCloud LittleLittleCloud changed the base branch from main to feature-prompty April 29, 2024 22:12
@github-actions github-actions Bot changed the title Fix #6030 .Net: Fix #6030 Apr 29, 2024
@LittleLittleCloud LittleLittleCloud marked this pull request as ready for review April 29, 2024 23:05
@LittleLittleCloud LittleLittleCloud requested a review from a team as a code owner April 29, 2024 23:05
Comment thread dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs Outdated
Comment thread dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs Outdated
Comment thread dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplateFactory.cs Outdated
Comment thread dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs Outdated
Comment thread dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs Outdated
Comment thread dotnet/src/Extensions/PromptTemplates.Liquid/LiquidPromptTemplate.cs Outdated
Comment thread .gitignore Outdated
@stephentoub
Copy link
Copy Markdown
Member

In this implementation, the Ġ will be reserved in liquid template which is used to replace : in all input variables when unsafe content is not allowed.

Why is that necessary? And why is that ok? Why are we in a position to effectively ban "Ġ"?

@LittleLittleCloud

This comment was marked as outdated.

@LittleLittleCloud LittleLittleCloud changed the title .Net: Fix #6030 .Net: Fix #6030 - Mitigating Prompt Injection in Liquid Templates May 3, 2024
@dmytrostruk dmytrostruk merged commit f6d996b into microsoft:feature-prompty May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel.core kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants