This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional security scanning when upserting documents using AI Fire…
…wall (#341) * Add security scanning to canopy with RI AI Firewall * add env variables to readme * move firewall logic to knowledgeBase class * add test * add docstrings * fix linting * remove unnecessary diff * Improve test cases, add documentation links to README and docstring * fix linting for tests * modfiy config and error message
- Loading branch information
1 parent
1059e9e
commit 7c5c69c
Showing
6 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# =========================================================== | ||
# Configuration file for Canopy Server | ||
# =========================================================== | ||
tokenizer: | ||
# ------------------------------------------------------------------------------------------- | ||
# Tokenizer configuration | ||
# A Tokenizer singleton instance must be initialized before initializing any other components | ||
# ------------------------------------------------------------------------------------------- | ||
type: OpenAITokenizer # Options: [OpenAITokenizer, LlamaTokenizer] | ||
params: | ||
model_name: gpt-3.5-turbo | ||
|
||
chat_engine: | ||
# ------------------------------------------------------------------------------------------------------------- | ||
# Chat engine configuration | ||
# ------------------------------------------------------------------------------------------------------------- | ||
context_engine: | ||
# ------------------------------------------------------------------------------------------------------------- | ||
# ContextEngine configuration | ||
# ------------------------------------------------------------------------------------------------------------- | ||
knowledge_base: | ||
# ----------------------------------------------------------------------------------------------------------- | ||
# KnowledgeBase configuration | ||
# Enable security scanning using Robust Intelligence's AI Firewall to scan all uploaded documents | ||
# for prompt injections before they can be added to the knowledge base. Any document that is flagged | ||
# is rejected. | ||
# ----------------------------------------------------------------------------------------------------------- | ||
params: | ||
enable_security_scanning: true # Whether to enable security scanning for uploaded documents. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
import os | ||
|
||
import requests | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AIFirewallError(ValueError): | ||
pass | ||
|
||
|
||
class AIFirewall: | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the AI Firewall using required RI environment variables.""" | ||
self.firewall_api_key = self._get_env_var("FIREWALL_API_KEY") | ||
self.firewall_url = self._get_env_var("FIREWALL_URL") | ||
self.firewall_instance_id = self._get_env_var("FIREWALL_INSTANCE_ID") | ||
self.firewall_instance_url = ( | ||
f"{self.firewall_url}/v1-beta/firewall/{self.firewall_instance_id}/validate" | ||
) | ||
self.firewall_headers = { | ||
"X-Firewall-Api-Key": self.firewall_api_key.strip(), | ||
} | ||
|
||
@staticmethod | ||
def _get_env_var(var_name: str) -> str: | ||
env_var = os.environ.get(var_name) | ||
if not env_var: | ||
raise RuntimeError( | ||
f"{var_name} environment variable " | ||
f"is required to use security scanning." | ||
) | ||
return env_var | ||
|
||
def scan_text(self, text: str) -> bool: | ||
"""Scan the input text for potential prompt injection attacks. | ||
Returns True if prompt injection attack is detected, False otherwise. | ||
This method sends the input text to the AI Firewall via REST | ||
API for security scanning. Documentation for the Validate | ||
endpoint on the Firewall can be found [here] | ||
(https://docs.robustintelligence.com/en/latest/reference/python-sdk.html#rime_sdk.FirewallClient) | ||
""" | ||
stripped_text = text.replace("\n", " ") | ||
firewall_response = requests.put( | ||
self.firewall_instance_url, | ||
headers=self.firewall_headers, | ||
json={"user_input_text": stripped_text}, | ||
) | ||
if not firewall_response.ok: | ||
raise AIFirewallError( | ||
f"AI Firewall returned status code " | ||
f"{firewall_response.status_code} " | ||
f"with reason: {firewall_response.reason}." | ||
) | ||
fw_result = firewall_response.json()["inputResults"] | ||
if ( | ||
fw_result["FIREWALL_RULE_TYPE_PROMPT_INJECTION"]["action"] | ||
== "FIREWALL_ACTION_FLAG" | ||
): | ||
return True | ||
else: | ||
logger.info("Document text passed security scanning.") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters