security-scan.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # Quick security scan using grep patterns
  3. # Usage: ./security-scan.sh [directory]
  4. set -e
  5. DIR="${1:-.}"
  6. RED='\033[0;31m'
  7. YELLOW='\033[1;33m'
  8. GREEN='\033[0;32m'
  9. NC='\033[0m'
  10. echo "=== Security Scan: $DIR ==="
  11. echo ""
  12. ISSUES=0
  13. check_pattern() {
  14. local name="$1"
  15. local pattern="$2"
  16. local type="$3"
  17. echo -n "Checking: $name... "
  18. if rg -l "$pattern" "$DIR" --type "$type" 2>/dev/null | head -5 | grep -q .; then
  19. echo -e "${RED}FOUND${NC}"
  20. rg -n "$pattern" "$DIR" --type "$type" 2>/dev/null | head -10
  21. echo ""
  22. ISSUES=$((ISSUES + 1))
  23. else
  24. echo -e "${GREEN}OK${NC}"
  25. fi
  26. }
  27. # Python checks
  28. echo "--- Python Security Checks ---"
  29. check_pattern "Hardcoded secrets" "(password|secret|api_key|token)\s*=\s*['\"][^'\"]{8,}['\"]" "py"
  30. check_pattern "SQL injection (f-strings)" "execute\(f['\"]" "py"
  31. check_pattern "SQL injection (format)" "execute\(.*\.format\(" "py"
  32. check_pattern "eval() usage" "\beval\s*\(" "py"
  33. check_pattern "exec() usage" "\bexec\s*\(" "py"
  34. check_pattern "pickle.loads" "pickle\.loads?\(" "py"
  35. check_pattern "os.system" "os\.system\(" "py"
  36. check_pattern "shell=True" "subprocess.*shell\s*=\s*True" "py"
  37. check_pattern "MD5 hashing" "hashlib\.md5\(" "py"
  38. check_pattern "SHA1 hashing" "hashlib\.sha1\(" "py"
  39. echo ""
  40. # JavaScript checks
  41. echo "--- JavaScript Security Checks ---"
  42. check_pattern "innerHTML" "\.innerHTML\s*=" "js"
  43. check_pattern "eval() usage" "\beval\s*\(" "js"
  44. check_pattern "document.write" "document\.write\(" "js"
  45. echo ""
  46. # General checks
  47. echo "--- General Security Checks ---"
  48. echo -n "Checking: .env files in git... "
  49. if git ls-files | grep -E "\.env$|\.env\." | grep -q .; then
  50. echo -e "${RED}FOUND${NC}"
  51. git ls-files | grep -E "\.env$|\.env\."
  52. ISSUES=$((ISSUES + 1))
  53. else
  54. echo -e "${GREEN}OK${NC}"
  55. fi
  56. echo -n "Checking: TODO/FIXME security items... "
  57. if rg -i "TODO.*security|FIXME.*security|HACK.*security" "$DIR" 2>/dev/null | head -5 | grep -q .; then
  58. echo -e "${YELLOW}FOUND${NC}"
  59. rg -i "TODO.*security|FIXME.*security|HACK.*security" "$DIR" 2>/dev/null | head -10
  60. ISSUES=$((ISSUES + 1))
  61. else
  62. echo -e "${GREEN}OK${NC}"
  63. fi
  64. echo ""
  65. echo "=== Summary ==="
  66. if [ $ISSUES -eq 0 ]; then
  67. echo -e "${GREEN}No issues found!${NC}"
  68. exit 0
  69. else
  70. echo -e "${RED}Found $ISSUES potential security issues${NC}"
  71. echo "Review the findings above and address any real vulnerabilities."
  72. exit 1
  73. fi