Skip to content

[P3][schema] pattern_detected and norm_tag columns: widen String(100) to avoid future truncation #516

Description

@FabioLeitao

Root cause

core/database.py defines pattern_detected and norm_tag as String(100) in both DatabaseFinding and FilesystemFinding.

In worst case (all default patterns matching + DOB_POSSIBLE_MINOR suffix + custom patterns), the concatenated pattern_detected and norm_tag strings can exceed 100 chars:

# Simulação: todos os padrões default + minor
pattern_detected = "LGPD_CPF, LGPD_CNPJ, LGPD_CNPJ_ALNUM, EMAIL, CREDIT_CARD, PHONE_BR, CCPA_SSN, DATE_DMY, DOB_POSSIBLE_MINOR, SUGGESTED_REVIEW"
# → 124 chars

norm_tag = "LGPD Art. 5; GDPR Art. 4(1); PCI/GLBA; CCPA; Personal data context; LGPD Art. 14 – possible minor data; GDPR Art. 8; Suggested review"
# → 133 chars

SQLite hoje: não enforça VARCHAR length — armazena sem truncar. Sem bug em runtime atual.

Risco:

  1. Migração futura para PostgreSQL/MySQL quebraria com StringDataRightTruncationError em qualquer finding com múltiplos patterns
  2. O schema documentado no código (String(100)) é enganoso para quem audita o schema
  3. Additive migration: novos _ensure_* já existem para colunas extras; alargar via ALTER seria trivial

Fix sugerido

# core/database.py — DatabaseFinding e FilesystemFinding
pattern_detected = Column(String(500))   # era String(100)
norm_tag = Column(String(500))           # era String(100)

Aplicar additive migration para DBs existentes:

def _ensure_wide_pattern_columns(self) -> None:
    """Widen pattern_detected and norm_tag for multi-pattern findings (no-op when already wide or SQLite)."""
    # SQLite ignores VARCHAR width at runtime; this migration is a no-op today
    # but documents the intended schema width for future RDBMS migrations.
    pass  # No ALTER needed for SQLite; update column def in ORM only

Para SQLite: apenas atualizar a definição ORM (SQLite não usa a largura). Para futuros RDBMS: adicionar ALTER TABLE ... MODIFY COLUMN na migration.

Arquivos afetados

  • core/database.py: linhas 76-77 (DatabaseFinding) e 93-94 (FilesystemFinding)
  • Nenhuma migração de dado necessária para SQLite

Critério de aceite

  • grep "String(100).*pattern_detected\|String(100).*norm_tag" core/database.py → 0 hits
  • pytest tests/test_database.py verde
  • Nenhuma regressão em tests/test_report_*.py

Ritual

  1. Commit: fix(database): widen pattern_detected and norm_tag to String(500) (#515)
  2. check-all gate → CI → PR/merge junto com outro fix de baixo risco

Out of scope

  • Migração de RDBMS
  • Mudanças na lógica de detector ou join de patterns

P3 — não bloqueia v1.7.4. Aberto por Claude (auditor READ-ONLY). Execução: Cursor.

Metadata

Metadata

Assignees

Labels

P3Low — backlog / nice-to-haveenhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions