From 61866f899e6a27b55e4e08c850a11c780b6d4ba6 Mon Sep 17 00:00:00 2001 From: ShaanGS <254507949+ShaanGS@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:54:46 +0530 Subject: [PATCH 1/2] test: text_search finds standard sections written as CWE-79 or ASVS V2.1.1 --- application/tests/db_test.py | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/application/tests/db_test.py b/application/tests/db_test.py index 68362cfc2..6c30ac28f 100644 --- a/application/tests/db_test.py +++ b/application/tests/db_test.py @@ -1146,6 +1146,60 @@ def test_text_search(self) -> None: res = self.collection.text_search(k) self.assertCountEqual(res, val) + def test_text_search_standard_section_ids(self) -> None: + """A standard name followed by a section ID, written the way people + usually write it ("CWE-79", "CWE 79", "ASVS V2.1.1"), finds that + section. A query of that shape that names no section still falls back + to the free text search.""" + cwe_79 = defs.Standard( + name="CWE", + section="Improper Neutralization of Input During Web Page Generation", + sectionID="79", + hyperlink="https://cwe.mitre.org/data/definitions/79.html", + ) + cwe_798 = defs.Standard( + name="CWE", + section="Use of Hard-coded Credentials", + sectionID="798", + hyperlink="https://cwe.mitre.org/data/definitions/798.html", + ) + capec_61 = defs.Standard( + name="CAPEC", + section="Session Fixation", + sectionID="61", + hyperlink="https://capec.mitre.org/data/definitions/61.html", + ) + asvs = defs.Standard( + name="ASVS", + section="Verify that user set passwords are at least 12 characters", + sectionID="V2.1.1", + hyperlink="https://github.com/OWASP/ASVS/blob/v4.0.3/4.0/en/x.md", + ) + top10 = defs.Standard( + name="Some Standard", + section="Top 10 risks", + hyperlink="https://example.com/top10", + ) + for node in (cwe_79, cwe_798, capec_61, asvs, top10): + self.collection.add_node(node) + self.collection.session.commit() + + expected: Dict[str, List[Any]] = { + "CWE-79": [cwe_79], + "CWE 79": [cwe_79], + "CWE:79": [cwe_79], + "cwe-79": [cwe_79], + " CWE-798 ": [cwe_798], + "CAPEC-61": [capec_61], + "ASVS V2.1.1": [asvs], + "Top 10": [top10], + "CWE-99999": [], + } + self.maxDiff = None + for query, val in expected.items(): + with self.subTest(query=query): + self.assertCountEqual(self.collection.text_search(query), val) + def test_text_search_expands_xxe_aliases(self) -> None: xxe_cre = defs.CRE( id="764-507", From 53d2f0e7d8ff227a71d3d6a583f18863b3db9055 Mon Sep 17 00:00:00 2001 From: ShaanGS <254507949+ShaanGS@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:57:04 +0530 Subject: [PATCH 2/2] fix(search): find standard sections written as CWE-79 or ASVS V2.1.1 text_search only found a standard's section in the internal 'Standard:CWE:79' form. 'CWE-79', 'CWE 79', 'CAPEC-61' and 'ASVS V2.1.1' fell through to the free text search and returned 404, on the website search and the MCP text_search tool alike. A ' ' query now looks that section up before the free text search. --- application/database/db.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/application/database/db.py b/application/database/db.py index 22e7358be..2d66470bb 100644 --- a/application/database/db.py +++ b/application/database/db.py @@ -2735,6 +2735,8 @@ def text_search(self, text: str) -> List[Optional[cre_defs.Document]]: all entries of and optionally, section/subsection '\d\d\d-\d\d\d' (two sets of 3 digits) will first try to match CRE ids before it performs a free text search + ' ', e.g. 'CWE-79', 'CWE 79' or 'ASVS V2.1.1', will + search for that section of the node called Anything else will be a case insensitive LIKE query in the database """ search_terms = self._expand_text_search_aliases(text) @@ -2745,6 +2747,10 @@ def text_search(self, text: str) -> List[Optional[cre_defs.Document]]: cre_id_search = r"CRE(:| )(?P\d+-\d+)" cre_naked_id_search = r"\d\d\d-\d\d\d" cre_name_search = r"CRE(:| )(?P\w+)" + name_section_id_search = ( + r"(?P[A-Za-z][\w.()&+/]*(?: [A-Za-z][\w.()&+/]*)*?)" + r"(?: *[:-] *| +)(?P[A-Za-z]{0,3}\d[\w.-]*)" + ) types = "|".join([v.value for v in cre_defs.Credoctypes]) node_search = ( r"(Node|(?P" @@ -2791,6 +2797,18 @@ def text_search(self, text: str) -> List[Optional[cre_defs.Document]]: results.extend(nodes) if results: return list(set(results)) + # " ", the way people write e.g. "CWE-79" or "ASVS V2.1.1". + # The length check keeps the pattern cheap on long free text. + if len(text) <= 100: + match = re.fullmatch(name_section_id_search, text) + if match: + nodes = self.get_nodes( + name=match.group("name"), + sectionID=match.group("id"), + ntype=None, # type: ignore + ) + if nodes: + return nodes # fuzzy matches second results = {} for search_term in search_terms: