|
| 1 | +name: Security Gate |
| 2 | + |
| 3 | +# This workflow acts as a required check that aggregates all security scan results. |
| 4 | +# Configure this as a required status check for branch protection. |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [main, develop] |
| 9 | + pull_request: |
| 10 | + branches: [main, develop] |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + security-events: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + security-lint: |
| 18 | + name: Security Lint & Configuration Check |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - name: Checkout |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Check for hardcoded secrets in config files |
| 25 | + run: | |
| 26 | + echo "=== Scanning for potential hardcoded secrets ===" |
| 27 | + FOUND=0 |
| 28 | +
|
| 29 | + # Check appsettings files for non-empty passwords/keys |
| 30 | + for file in $(find . -name "appsettings*.json" -not -path "*/bin/*" -not -path "*/obj/*"); do |
| 31 | + echo "Checking $file..." |
| 32 | +
|
| 33 | + # Check for non-empty password fields |
| 34 | + if grep -Pi '"(password|secret|apikey|api_key|token)":\s*"[^"]{8,}"' "$file"; then |
| 35 | + echo "::error file=$file::Potential hardcoded secret found in $file" |
| 36 | + FOUND=1 |
| 37 | + fi |
| 38 | +
|
| 39 | + # Check for connection strings with inline passwords |
| 40 | + if grep -Pi 'Password=[^;]{6,}' "$file"; then |
| 41 | + echo "::error file=$file::Potential hardcoded database password in $file" |
| 42 | + FOUND=1 |
| 43 | + fi |
| 44 | + done |
| 45 | +
|
| 46 | + if [ $FOUND -eq 1 ]; then |
| 47 | + echo "" |
| 48 | + echo "::error::Hardcoded secrets detected! Move secrets to environment variables or user-secrets." |
| 49 | + exit 1 |
| 50 | + else |
| 51 | + echo "No hardcoded secrets found." |
| 52 | + fi |
| 53 | +
|
| 54 | + - name: Check .gitignore completeness |
| 55 | + run: | |
| 56 | + echo "=== Checking .gitignore ===" |
| 57 | + MISSING=0 |
| 58 | +
|
| 59 | + REQUIRED_PATTERNS=( |
| 60 | + "appsettings.Production.json" |
| 61 | + "*.pfx" |
| 62 | + "*.key" |
| 63 | + ".env" |
| 64 | + ) |
| 65 | +
|
| 66 | + for pattern in "${REQUIRED_PATTERNS[@]}"; do |
| 67 | + if ! grep -qF "$pattern" .gitignore; then |
| 68 | + echo "::warning::Missing .gitignore entry: $pattern" |
| 69 | + MISSING=1 |
| 70 | + fi |
| 71 | + done |
| 72 | +
|
| 73 | + # Check for dangerous negation rules that expose secret files |
| 74 | + if grep -q '!.*appsettings.*\.json' .gitignore; then |
| 75 | + echo "::error::Dangerous .gitignore negation rule found that may expose config files with secrets" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | +
|
| 79 | + if [ $MISSING -eq 1 ]; then |
| 80 | + echo "::warning::Some recommended .gitignore patterns are missing" |
| 81 | + else |
| 82 | + echo ".gitignore looks good." |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: Verify SECURITY.md exists |
| 86 | + run: | |
| 87 | + if [ ! -f "SECURITY.md" ]; then |
| 88 | + echo "::warning::SECURITY.md not found. Consider adding a security policy." |
| 89 | + else |
| 90 | + echo "SECURITY.md found." |
| 91 | + fi |
| 92 | +
|
| 93 | + build-and-test: |
| 94 | + name: Build Verification |
| 95 | + runs-on: ubuntu-latest |
| 96 | + steps: |
| 97 | + - name: Checkout |
| 98 | + uses: actions/checkout@v4 |
| 99 | + |
| 100 | + - name: Setup .NET |
| 101 | + uses: actions/setup-dotnet@v4 |
| 102 | + with: |
| 103 | + dotnet-version: 8.0.x |
| 104 | + |
| 105 | + - name: Restore |
| 106 | + run: dotnet restore CompanyManagementSystem.sln |
| 107 | + |
| 108 | + - name: Build |
| 109 | + run: dotnet build CompanyManagementSystem.sln --configuration Release --no-restore |
| 110 | + |
| 111 | + - name: Run tests |
| 112 | + run: | |
| 113 | + dotnet test Tests/Tests.csproj \ |
| 114 | + --configuration Release \ |
| 115 | + --no-build \ |
| 116 | + --verbosity normal \ |
| 117 | + --logger "trx;LogFileName=test-results.trx" |
0 commit comments