Skip to content

Latest commit

 

History

History
61 lines (54 loc) · 2.56 KB

File metadata and controls

61 lines (54 loc) · 2.56 KB

BOA001 Avoid inputs in SystemChatMessage

Including user inputs in SystemChatMessage is a security risk and might allow bad actors to perform prompt injection. System Messages should only contain static information to lead the model into replying within expected boundaries.

Examples of patterns that are flagged by this analyzer

var result = await _chatClient.CompleteChatAsync(
[
    new SystemChatMessage(
        $$"""
        You are a note taker assisting a group of dungeons and dragons players tasked with recording and putting together recaps of each play session so the dungeon master and players can get insights from previous sessions.
        The transcripts provided to you might contain dialogues that are not relevant to the game, you should ignore those.
        Format the response as JSON using the following JSON schema:
        {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": { "type": "string" },
                    "description": { "type": "string" },
                    "level": { "type": "integer", "nullable": true },
                    "race": { "type": "string", "nullable": true }
                }
            }
        }
        {{transcript}}
        """),
]);

Solution

Move any user content to a UserChatMessage instead

var result = await _chatClient.CompleteChatAsync(
[
    new SystemChatMessage(
        """
        You are a note taker assisting a group of dungeons and dragons players tasked with recording and putting together recaps of each play session so the dungeon master and players can get insights from previous sessions.
        The transcripts provided to you might contain dialogues that are not relevant to the game, you should ignore those.
        Format the response as JSON using the following JSON schema:
        {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": { "type": "string" },
                    "description": { "type": "string" },
                    "level": { "type": "integer", "nullable": true },
                    "race": { "type": "string", "nullable": true }
                }
            }
        }
        """),
    new UserChatMessage(transcript)
]);

Alternatively, you can update the declaration of the variable used in the interpolation to be a constant.