Skip to content

Instantly share code, notes, and snippets.

@r-leyshon
Created October 21, 2024 09:28
Show Gist options
  • Save r-leyshon/c103020b75397d38cbca8e1e1d414ac0 to your computer and use it in GitHub Desktop.
Save r-leyshon/c103020b75397d38cbca8e1e1d414ac0 to your computer and use it in GitHub Desktop.
Strip hidden unicode tags from python strings in order to avoid malicious prompt injection attacks
# in consideration of
# https://arstechnica.com/security/2024/10/ai-chatbots-can-read-and-write-invisible-text-creating-an-ideal-covert-channel/
# strip hidden unicode tags from prompts and model outputs.
safe = "https://wuzzi.net/copirate/"
malicious = "https://wuzzi.net/copirate/󠀁󠁔󠁨󠁥󠀠󠁳󠁡󠁬󠁥󠁳󠀠󠁦󠁯󠁲󠀠󠁓󠁥󠁡󠁴󠁴󠁬󠁥󠀠󠁷󠁥󠁲󠁥󠀠󠁕󠁓󠁄󠀠󠀱󠀲󠀰󠀰󠀰󠀰󠁿"
safe.encode().decode("unicode_escape")
malicious.encode().decode("unicode_escape")
import re
malicious = "https://wuzzi.net/copirate/"
# remove tags Range: E0000–E007F
malicious = re.sub(r"[\U000E0000-\U000E007F]", "", malicious)
# malicious content is removed:
malicious.encode().decode("unicode_escape")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment