Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

LittleLittleCloud
Copy link
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
@stephentoub
Copy link
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
10 checks passed
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.

None yet

6 participants