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

Open Beginner friendly
#1,248 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, react, typescript

Research direction

Start in application/database/db.py at Node_collection.text_search(), where the term is interpolated into a LIKE pattern six times; add escaping for % and _, a minimum length for the fuzzy part and a row limit. Then fix the client in SearchBar.tsx and Search.tsx, wrapping the term in encodeURIComponent before history.push. Confirm by timing /rest/v1/text_search for session, e, % and _ and checking that /search/C%23 reaches the backend intact. The issue leaves the choice of fixes open, so agree the approach with a maintainer first.

Written by the indexing model from the issue text.

Description

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?

Dominant language
Python
Stars
216
Forks
173
Avg merge
1d 21h
Merged PRs (30d)
21

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OWASP/OpenCRE

All issues in OWASP/OpenCRE

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.