|
| 1 | +# GitHub Actions Security Workflows |
| 2 | + |
| 3 | +Este diretório contém workflows automatizados de segurança para o ProStaff API. |
| 4 | + |
| 5 | +## Workflows Disponíveis |
| 6 | + |
| 7 | +### 1. `security-scan.yml` - Security Scan Completo |
| 8 | + |
| 9 | +**Trigger:** Push/PR em `master` ou `develop`, agendamento semanal |
| 10 | + |
| 11 | +**Jobs:** |
| 12 | + |
| 13 | +#### Static Analysis (SAST) |
| 14 | + |
| 15 | +1. **Brakeman** - Análise de segurança específica para Rails |
| 16 | +2. **Dependency Check** - Verifica vulnerabilidades em gems (Bundle Audit) |
| 17 | +3. **Semgrep** - Análise estática com regras customizáveis |
| 18 | +4. **Secret Scan** - Detecta secrets com TruffleHog |
| 19 | + |
| 20 | +#### Dynamic Analysis (DAST) - NOVO! |
| 21 | + |
| 22 | +5. **SSRF Protection** - Testa proteção contra Server-Side Request Forgery |
| 23 | +* Testa bloqueio de localhost, IPs privados, AWS metadata |
| 24 | +* Verifica whitelist de domínios |
| 25 | +* Confirma que autenticação é obrigatória |
| 26 | + |
| 27 | + |
| 28 | +6. **Authentication Test** - Valida segurança de autenticação |
| 29 | +* Testa rejeição de tokens inválidos/ausentes |
| 30 | +* Verifica endpoints protegidos |
| 31 | +* Valida endpoints públicos (health check) |
| 32 | + |
| 33 | + |
| 34 | +7. **SQL Injection Test** - Testa proteção contra SQL injection |
| 35 | +* Testa queries parametrizadas |
| 36 | +* Verifica bloqueio de UNION injection |
| 37 | +* Valida que erros SQL não vazam |
| 38 | + |
| 39 | + |
| 40 | +8. **Secrets Scan** - Verifica secrets expostos |
| 41 | +* Busca hardcoded passwords |
| 42 | +* Verifica API keys |
| 43 | +* Confirma .env não está no git |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +#### Summary |
| 48 | + |
| 49 | +9. **Security Summary** - Consolida todos os resultados |
| 50 | +* Posta comentário no PR com tabela de status |
| 51 | +* Separa SAST vs DAST |
| 52 | +* Indica se pode fazer merge |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Como Funciona |
| 59 | + |
| 60 | +### Estrutura dos Jobs DAST |
| 61 | + |
| 62 | +Cada job DAST segue este padrão: |
| 63 | + |
| 64 | +```yaml |
| 65 | +services: |
| 66 | + postgres: |
| 67 | + image: postgres:15-alpine |
| 68 | + # ... |
| 69 | + redis: |
| 70 | + image: redis:7-alpine |
| 71 | + # ... |
| 72 | + |
| 73 | +steps: |
| 74 | + 1. Checkout do código |
| 75 | + 2. Setup Ruby + bundler |
| 76 | + 3. Setup database (rails db:migrate) |
| 77 | + 4. Start Rails server (porta 3333) |
| 78 | + 5. Wait for API (/up endpoint) |
| 79 | + 6. Run security test script |
| 80 | + 7. Upload results (artifacts) |
| 81 | + |
| 82 | +``` |
| 83 | +
|
| 84 | +### Scripts de Teste |
| 85 | +
|
| 86 | +Os scripts estão em `.pentest/`: |
| 87 | + |
| 88 | +| Script | Testes | O que valida | |
| 89 | +| --- | --- | --- | |
| 90 | +| `test-ssrf-quick.sh` | 9 | SSRF protection | |
| 91 | +| `test-authentication-quick.sh` | 5 | JWT auth | |
| 92 | +| `test-sql-injection-quick.sh` | 4 | SQL injection | |
| 93 | +| `test-secrets-quick.sh` | 5 | Secrets management | |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Como Usar |
| 98 | + |
| 99 | +### Desenvolvimento Local |
| 100 | + |
| 101 | +```bash |
| 102 | +# Rodar todos os testes de segurança |
| 103 | +./.pentest/test-ssrf-quick.sh |
| 104 | +./.pentest/test-authentication-quick.sh |
| 105 | +./.pentest/test-sql-injection-quick.sh |
| 106 | +./.pentest/test-secrets-quick.sh |
| 107 | +
|
| 108 | +``` |
| 109 | + |
| 110 | +### Pull Requests |
| 111 | + |
| 112 | +Ao criar um PR, o workflow automaticamente: |
| 113 | + |
| 114 | +1. Roda todos os scans (SAST + DAST) |
| 115 | +2. Posta comentário com resumo dos resultados |
| 116 | +3. Bloqueia merge se houver falhas críticas |
| 117 | + |
| 118 | +**Exemplo de comentário no PR:** |
| 119 | + |
| 120 | +```markdown |
| 121 | +## Security Scan Summary |
| 122 | +
|
| 123 | +### Static Analysis (SAST) |
| 124 | +| Check | Status | |
| 125 | +|-------|--------| |
| 126 | +| Brakeman | success | |
| 127 | +| Dependencies | success | |
| 128 | +| Semgrep | success | |
| 129 | +| Secrets | success | |
| 130 | +
|
| 131 | +### Dynamic Analysis (DAST) |
| 132 | +| Check | Status | |
| 133 | +|-------|--------| |
| 134 | +| SSRF Protection | success | |
| 135 | +| Authentication | success | |
| 136 | +| SQL Injection | success | |
| 137 | +
|
| 138 | +All security checks passed! |
| 139 | +
|
| 140 | +``` |
| 141 | + |
| 142 | +### Agendamento |
| 143 | + |
| 144 | +O workflow pode rodar semanalmente (comentado por padrão): |
| 145 | + |
| 146 | +```yaml |
| 147 | +# Descomentar para ativar |
| 148 | +schedule: |
| 149 | + - cron: '0 9 * * 1' # Segunda-feira 9am UTC |
| 150 | +
|
| 151 | +``` |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +## Artifacts |
| 156 | + |
| 157 | +Cada job gera artifacts que podem ser baixados: |
| 158 | + |
| 159 | +* `brakeman-report.json` - Relatório Brakeman |
| 160 | +* `bundle-audit-report.txt` - Relatório de dependências |
| 161 | +* `semgrep-report.json` - Relatório Semgrep |
| 162 | +* `ssrf-test-results/` - Resultados dos testes SSRF |
| 163 | + |
| 164 | +**Como baixar:** |
| 165 | + |
| 166 | +1. Vá em Actions > Workflow run |
| 167 | +2. Scroll down até "Artifacts" |
| 168 | +3. Download do artifact desejado |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## Troubleshooting |
| 173 | + |
| 174 | +### Testes DAST falhando |
| 175 | + |
| 176 | +**Problema:** API não sobe ou timeout esperando `/up` |
| 177 | + |
| 178 | +**Solução:** |
| 179 | + |
| 180 | +```yaml |
| 181 | +# Aumentar timeout em .github/workflows/security-scan.yml |
| 182 | +- name: Wait for API |
| 183 | + run: | |
| 184 | + timeout 120 bash -c 'until curl -sf http://localhost:3333/up; do sleep 2; done' |
| 185 | +
|
| 186 | +``` |
| 187 | + |
| 188 | +**Problema:** Testes passam localmente mas falham no CI |
| 189 | + |
| 190 | +**Causa comum:** Diferenças de ambiente (variáveis, portas, etc) |
| 191 | + |
| 192 | +**Debug:** |
| 193 | + |
| 194 | +```yaml |
| 195 | +# Adicionar step de debug |
| 196 | +- name: Debug |
| 197 | + run: | |
| 198 | + curl -v http://localhost:3333/up |
| 199 | + docker logs <container_name> |
| 200 | +
|
| 201 | +``` |
| 202 | + |
| 203 | +### Secrets não encontrados |
| 204 | + |
| 205 | +**Problema:** TruffleHog não roda (action externa) |
| 206 | + |
| 207 | +**Solução:** Script `.pentest/test-secrets-quick.sh` faz scan básico mesmo sem TruffleHog |
| 208 | + |
| 209 | +### Rate Limit no Multi-Tenancy |
| 210 | + |
| 211 | +**Problema:** Testes de multi-tenancy falham por rate limit (3 reg/hour) |
| 212 | + |
| 213 | +**Solução:** Criar test data via seeds em vez de criar via API: |
| 214 | + |
| 215 | +```ruby |
| 216 | +# db/seeds/test_organizations.rb |
| 217 | +if Rails.env.test? |
| 218 | + org1 = Organization.create!(name: "Test Org 1", slug: "test-org-1") |
| 219 | + org2 = Organization.create!(name: "Test Org 2", slug: "test-org-2") |
| 220 | + # ... |
| 221 | +end |
| 222 | +
|
| 223 | +``` |
| 224 | + |
| 225 | +--- |
| 226 | + |
| 227 | +## Adicionando Novos Testes |
| 228 | + |
| 229 | +### 1. Criar script de teste |
| 230 | + |
| 231 | +```bash |
| 232 | +# .pentest/test-new-feature.sh |
| 233 | +#!/bin/bash |
| 234 | +API_URL="http://localhost:3333" |
| 235 | +# ... testes |
| 236 | +
|
| 237 | +``` |
| 238 | + |
| 239 | +### 2. Adicionar job ao workflow |
| 240 | + |
| 241 | +```yaml |
| 242 | +new-feature-test: |
| 243 | + name: New Feature Security Test |
| 244 | + runs-on: ubuntu-latest |
| 245 | + services: |
| 246 | + postgres: { ... } |
| 247 | + redis: { ... } |
| 248 | + steps: |
| 249 | + - uses: actions/checkout@v4 |
| 250 | + - name: Setup Ruby |
| 251 | + uses: ruby/setup-ruby@v1 |
| 252 | + with: |
| 253 | + ruby-version: 3.4.5 |
| 254 | + bundler-cache: true |
| 255 | + - name: Setup Database |
| 256 | + run: bundle exec rails db:migrate RAILS_ENV=test |
| 257 | + - name: Start Server |
| 258 | + run: bundle exec rails s -p 3333 -d |
| 259 | + - name: Run Tests |
| 260 | + run: ./.pentest/test-new-feature.sh |
| 261 | +
|
| 262 | +``` |
| 263 | + |
| 264 | +### 3. Adicionar ao summary |
| 265 | + |
| 266 | +```yaml |
| 267 | +security-summary: |
| 268 | + needs: [..., new-feature-test] |
| 269 | + # ... |
| 270 | + const newFeature = '${{ needs.new-feature-test.result }}'; |
| 271 | + # ... |
| 272 | +
|
| 273 | +``` |
| 274 | + |
| 275 | +--- |
| 276 | + |
| 277 | +## Configuração de Secrets |
| 278 | + |
| 279 | +O workflow precisa destes secrets configurados em **Settings → Secrets → Actions**: |
| 280 | + |
| 281 | +| Secret | Obrigatório? | Uso | |
| 282 | +| --- | --- | --- | |
| 283 | +| `RIOT_API_KEY` | Não | Testes que envolvem Riot API (fallback: dummy_key) | |
| 284 | +| `SENTRY_DSN` | Não | Reporting de erros | |
| 285 | + |
| 286 | +**Como configurar:** |
| 287 | + |
| 288 | +1. GitHub → Repository → Settings |
| 289 | +2. Secrets and variables → Actions |
| 290 | +3. New repository secret |
| 291 | +4. Nome: `RIOT_API_KEY`, Value: `sua_api_key` |
| 292 | + |
| 293 | +--- |
| 294 | + |
| 295 | +## Performance |
| 296 | + |
| 297 | +**Tempo médio de execução:** |
| 298 | + |
| 299 | +| Job | Duração | Pode rodar em paralelo? | |
| 300 | +| --- | --- | --- | |
| 301 | +| Brakeman | ~1 min | Sim | |
| 302 | +| Dependencies | ~2 min | Sim | |
| 303 | +| Semgrep | ~3 min | Sim | |
| 304 | +| SSRF Test | ~2 min | Sim | |
| 305 | +| Auth Test | ~2 min | Sim | |
| 306 | +| SQL Injection | ~2 min | Sim | |
| 307 | +| Secrets | ~1 min | Sim | |
| 308 | + |
| 309 | +**Total:** ~3-4 minutos (em paralelo) |
| 310 | + |
| 311 | +--- |
| 312 | + |
| 313 | +## Integrações Futuras |
| 314 | + |
| 315 | +### Recomendado adicionar: |
| 316 | + |
| 317 | +1. **OWASP ZAP** - Scan de vulnerabilidades web |
| 318 | +2. **Nuclei** - Template-based scanning |
| 319 | +3. **Multi-tenancy test** - Quando resolver rate limit |
| 320 | +4. **JWT security test** - Algorithm confusion, expiration |
| 321 | +5. **Rate limiting test** - Validar Rack::Attack |
| 322 | + |
| 323 | +### Como adicionar ZAP: |
| 324 | + |
| 325 | +```yaml |
| 326 | +zap-scan: |
| 327 | + name: OWASP ZAP Scan |
| 328 | + runs-on: ubuntu-latest |
| 329 | + steps: |
| 330 | + - uses: actions/checkout@v4 |
| 331 | + - name: ZAP Baseline Scan |
| 332 | + uses: zaproxy/action-baseline@v0.7.0 |
| 333 | + with: |
| 334 | + target: 'http://localhost:3333' |
| 335 | +
|
| 336 | +``` |
| 337 | + |
| 338 | +--- |
| 339 | + |
| 340 | +## Compliance |
| 341 | + |
| 342 | +Este workflow cobre: |
| 343 | + |
| 344 | +* **OWASP Top 10 2025** |
| 345 | +* A01: Broken Access Control (Auth tests) |
| 346 | +* A02: Cryptographic Failures (Secrets scan) |
| 347 | +* A03: Injection (SQL injection tests) |
| 348 | +* A04: Insecure Design (Code review) |
| 349 | +* A05: Security Misconfiguration (Brakeman) |
| 350 | +* A06: Vulnerable Components (Dependency check) |
| 351 | +* A07: Auth Failures (Auth tests) |
| 352 | +* A08: Data Integrity (Semgrep) |
| 353 | +* A09: Logging Failures (Code review) |
| 354 | +* A10: SSRF (SSRF tests) |
| 355 | + |
| 356 | + |
| 357 | +* **SAST + DAST** (Static + Dynamic analysis) |
| 358 | +* **SCA** (Software Composition Analysis) |
| 359 | +* **Secrets Detection** |
| 360 | + |
| 361 | +--- |
| 362 | + |
| 363 | +## Referências |
| 364 | + |
| 365 | +* [Brakeman Docs](https://brakemanscanner.org/docs/) |
| 366 | +* [Bundle Audit](https://github.com/rubysec/bundler-audit) |
| 367 | +* [Semgrep Rules](https://semgrep.dev/r) |
| 368 | +* [TruffleHog](https://github.com/trufflesecurity/trufflehog) |
| 369 | +* [OWASP Top 10 2025](https://owasp.org/www-project-top-ten/) |
| 370 | + |
| 371 | +--- |
| 372 | + |
| 373 | +**Last Updated:** 2026-03-04 |
| 374 | +**Maintainer:** Security Team |
| 375 | +**CI/CD Status:** Active |
| 376 | + |
| 377 | +--- |
0 commit comments