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

オープン 初心者向け
#1,248 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
70/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, react, typescript

調査の方向性

application/database/db.py の Node_collection.text_search() から始めてください。ここでは用語が LIKE パターンに6回補間されています。% と _ のエスケープ、ファジー部分の最小長、行数制限を追加してください。次に SearchBar.tsx と Search.tsx のクライアントを修正し、用語を encodeURIComponent で包んでから history.push してください。/rest/v1/text_search を session、e、%、_ で実行して時間を計測し、/search/C%23 がバックエンドにそのまま到達することを確認してください。この issue は修正の選択肢をオープンにしているため、まず maintainer とアプローチについて合意してください。

索引モデルが issue の本文から書いたものです。

説明

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?

主要言語
Python
スター
216
フォーク
173
平均マージ
1日 21時間
マージ済み PR(30日)
21

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

OWASP/OpenCRE のほかの issue

OWASP/OpenCRE の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。