2
0

check_hidden_chars.sh 1.5 KB

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