Skip to content

fix(aluno): reject invalid CPF before querying student search - #1198

Open
Silenttttttt wants to merge 2 commits into
portabilis:2.12from
Silenttttttt:fix/aluno-search-invalid-cpf
Open

fix(aluno): reject invalid CPF before querying student search#1198
Silenttttttt wants to merge 2 commits into
portabilis:2.12from
Silenttttttt:fix/aluno-search-invalid-cpf

Conversation

@Silenttttttt

Copy link
Copy Markdown

Summary

Fixes #1058.

ieducar/intranet/educar_aluno_lst.php (the student search form) strips the CPF field down to digits with preg_replace('/\D/', '', ...) and passes that value straight into LegacyStudentBuilder::whereCpf(), which does an exact where('cpf', $cpf) match. The checksum is never validated.

cadastro.fisica.cpf has no unique constraint. A student without a real CPF on file is commonly stored with a placeholder value such as 00000000000. Since that placeholder is itself an invalid CPF, typing in an invalid/mistyped CPF can coincidentally match that placeholder and return every student who shares it — matching the reported behavior of an invalid CPF returning multiple students instead of zero.

Portabilis_Utils_Validation::validatesCpf() already implements the CPF checksum validation and is used for this exact purpose elsewhere in the codebase during registration (PessoaController::validateCpf(), atendidos_cad.php), but it was never applied to this search filter.

Fix

Validate the CPF with Portabilis_Utils_Validation::validatesCpf() before it reaches the query. When a non-empty CPF fails validation, the search short-circuits to an empty result set (via an empty LengthAwarePaginator) instead of running the query, so an invalid CPF can never match student records — regardless of what happens to be stored as a placeholder value.

Notes

  • I don't believe this affects any legitimate partial/prefix CPF search: whereCpf() already does an exact match (where('cpf', $cpf)), not a LIKE, so partial digit entry never matched multiple students in the first place — it simply matched nothing (or, in the buggy case, happened to match a shared invalid placeholder).
  • For comparison, pesquisa_pessoa_lst.php (a sibling person-search screen) already validates the CPF checksum before querying and shows an explicit "Informado um CPF Inválido" message otherwise, so gating the query on CPF validity is an established pattern in this codebase, not a new behavior being introduced here.
  • I was not able to set up a full local instance (DB seeding + app) to reproduce this end-to-end, but I did verify the actual validators in this repo (validaCPF() and Portabilis_Utils_Validation::validatesCpf()) reject the placeholder value 00000000000 (and other repeated-digit placeholders), consistent with the root cause above.

educar_aluno_lst.php stripped the CPF field down to digits with
preg_replace('/\D/', ...) and passed that straight into
LegacyStudentBuilder::whereCpf(), an exact where('cpf', $cpf) match,
without ever checking the checksum.

cadastro.fisica.cpf has no unique constraint, and a CPF-less student is
commonly stored with a placeholder value such as 00000000000. Because
that placeholder is itself an invalid CPF, an invalid/mistyped CPF
search can coincidentally equal it and match every student sharing that
placeholder, returning multiple students for a single invalid CPF
lookup.

Portabilis_Utils_Validation::validatesCpf() already implements the
checksum check and is used for this exact purpose during registration
(PessoaController::validateCpf(), atendidos_cad.php). This applies the
same validation to the search filter: an invalid CPF now short-circuits
to zero results instead of reaching the query.

Fixes portabilis#1058
Gerar()'s cognitive complexity grew past this project's SonarCloud
threshold (15) after the CPF-validation branch was added in the
previous commit. Extracting the search resolution (valid CPF vs.
empty result set) and the per-student row-building into their own
private methods removes that branching and nesting from Gerar()
without changing any behavior — same queries, same output, same
early-return-on-invalid-CPF logic, just moved into searchStudents()
and studentRow().
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CPF inválido retorna vários alunos no iEducar

2 participants