1#!/bin/bash
2
3# Script to check for hidden/invisible characters in Go files
4# This helps detect potential prompt injection attempts
5
6echo "Checking Go files for hidden characters..."
7
8# Find all Go files in the repository
9go_files=$(find . -name "*.go" -type f)
10
11# Counter for files with hidden characters
12files_with_hidden=0
13
14for file in $go_files; do
15 # Check for specific Unicode hidden characters that could be used for prompt injection
16 # This excludes normal whitespace like tabs and newlines
17 # Looking for:
18 # - Zero-width spaces (U+200B)
19 # - Zero-width non-joiners (U+200C)
20 # - Zero-width joiners (U+200D)
21 # - Left-to-right/right-to-left marks (U+200E, U+200F)
22 # - Bidirectional overrides (U+202A-U+202E)
23 # - Byte order mark (U+FEFF)
24 if hexdump -C "$file" | grep -E 'e2 80 8b|e2 80 8c|e2 80 8d|e2 80 8e|e2 80 8f|e2 80 aa|e2 80 ab|e2 80 ac|e2 80 ad|e2 80 ae|ef bb bf' > /dev/null 2>&1; then
25 echo "Hidden characters found in: $file"
26
27 # Show the file with potential issues
28 echo " Hexdump showing suspicious characters:"
29 hexdump -C "$file" | grep -E 'e2 80 8b|e2 80 8c|e2 80 8d|e2 80 8e|e2 80 8f|e2 80 aa|e2 80 ab|e2 80 ac|e2 80 ad|e2 80 ae|ef bb bf' | head -10
30
31 files_with_hidden=$((files_with_hidden + 1))
32 fi
33done
34
35if [ $files_with_hidden -eq 0 ]; then
36 echo "No hidden characters found in any Go files."
37else
38 echo "Found hidden characters in $files_with_hidden Go file(s)."
39fi
40
41exit $files_with_hidden # Exit with number of affected files as status code