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

Aperta Adatta ai principianti
#1,248 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, react, typescript

Direzione di ricerca

Inizia in application/database/db.py presso Node_collection.text_search(), dove il termine viene interpolato in un pattern LIKE sei volte; aggiungi l'escaping per % e _, una lunghezza minima per la parte fuzzy e un limite di righe. Poi correggi il client in SearchBar.tsx e Search.tsx, avvolgendo il termine in encodeURIComponent prima di history.push. Conferma cronometrando /rest/v1/text_search per session, e, % e _ e verificando che /search/C%23 raggiunga il backend intatto. L'issue lascia aperta la scelta delle correzioni, quindi concorda prima l'approccio con un maintainer.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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?

Lingua principale
Python
Stelle
216
Fork
173
Merge medio
1g 21h
PR unite (30g)
21

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OWASP/OpenCRE

Tutte le issue di OWASP/OpenCRE

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.