text_search: one-letter or wildcard queries load almost the whole database (90s+), and the search box doesn't encode the term

Abierto Apto para principiantes
#1,248 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
70/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python, react, typescript

Línea de trabajo

Empieza en application/database/db.py en Node_collection.text_search(), donde el término se interpola en un patrón LIKE seis veces; añade escaping para % y _, una longitud mínima para la parte difusa y un límite de filas. Luego corrige el cliente en SearchBar.tsx y Search.tsx, envolviendo el término en encodeURIComponent antes de history.push. Confirma midiendo el tiempo de /rest/v1/text_search para session, e, % y _ y comprobando que /search/C%23 llega intacto al backend. El issue deja abierta la elección de las correcciones, así que acuerda primero el enfoque con un maintainer.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Problem

A very short search makes /rest/v1/text_search return almost everything in the database and takes minutes.

Tested on my local setup (staging at d9514e52, full upstream sync: 522 CREs, 2841 nodes):

Request Time Response
text_search?text=session 2.3 s 215 KB
text_search?text=e 89 s 8.4 MB, 3311 of 3363 rows
text_search?text=% no response after 180 s -
text_search?text=_ no response after 180 s -

In the UI, searching for e loads for a long time and then shows "The document could not be loaded".

I did not test this on opencre.org on purpose, so I don't load the live server.

Cause

In Node_collection.text_search() (application/database/db.py), the fuzzy search does:

args = [f"%{search_term}%", "", "", "", "", ""]
  • The term is used directly in LIKE, so % and _ are not escaped. text=% becomes %%% and matches every row.
  • There is no limit on results, so a common letter like e matches almost all nodes and CREs.
  • The 6 permutations run one query per column (name, section, subsection, link, description, sectionID), and each one loads all of its matches.

Because /rest/v1/text_search is public, a few of these requests can keep the server busy.

Related: the search box doesn't encode the term

SearchBar.tsx and Search.tsx do:

history.push(`${SEARCH}/${term}`);

The term is not passed through encodeURIComponent, so:

  • % throws Uncaught URIError: Pathname "/search/%" could not be decoded and nothing happens
  • C# becomes /search/C#, so it searches for C, which triggers the slow one-letter search above

Possible fixes

  1. Escape % and _ in the search term before building the LIKE pattern
  2. Require a minimum length for the fuzzy part (for example 2 or 3 characters)
  3. Limit the number of fuzzy results
  4. Encode the term in the search box with encodeURIComponent

I'm happy to work on this. Which of these would you like, and should the PR target main or staging?

Lenguaje dominante
Python
Estrellas
216
Forks
173
Merge medio
1 d 21 h
PR fusionados (30 d)
21

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de OWASP/OpenCRE

Todos los issues de OWASP/OpenCRE

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.