|
| 1 | +"""isa-operator parity: the brute-force (field_search) and SQL search paths must |
| 2 | +agree, and both must follow MATLAB's isa semantics -- a document is isa(X) iff X |
| 3 | +is its class OR one of its superclasses (matched by bare name, as produced by |
| 4 | +did.implementations.doc2sql's meta.class / meta.superclass). |
| 5 | +
|
| 6 | +Regression for the DID-python isa divergence (audit 6.1-1): the old brute-force |
| 7 | +field_search isa used a ``param1 in a`` heuristic plus an exact class_name |
| 8 | +check, so it (a) missed superclass membership -- a probe did not match |
| 9 | +isa('element') -- and (b) spuriously matched a class name that happened to be an |
| 10 | +incidental top-level field of an unrelated document. Same query, different |
| 11 | +result set per language. The SQL path already matched MATLAB (meta.class / |
| 12 | +meta.superclass); this test pins both paths to the same, correct answer. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import unittest |
| 17 | + |
| 18 | +from did.document import Document |
| 19 | +from did.implementations.sqlitedb import SQLiteDB |
| 20 | +from did.query import Query |
| 21 | +from did.datastructures import field_search |
| 22 | + |
| 23 | + |
| 24 | +def _doc(class_name, superclasses, fields=None): |
| 25 | + props = { |
| 26 | + "document_class": { |
| 27 | + "definition": f"$NDIDOCUMENTPATH/{class_name}.json", |
| 28 | + "class_name": class_name, |
| 29 | + "class_version": 1, |
| 30 | + "property_list_name": class_name, |
| 31 | + "superclasses": [ |
| 32 | + {"definition": f"$NDIDOCUMENTPATH/{s}.json"} for s in superclasses |
| 33 | + ], |
| 34 | + }, |
| 35 | + "base": { |
| 36 | + "id": f"id_{class_name}", |
| 37 | + "name": class_name, |
| 38 | + "datestamp": "2026-06-12T00:00:00", |
| 39 | + }, |
| 40 | + } |
| 41 | + if fields: |
| 42 | + props.update(fields) |
| 43 | + return Document(props) |
| 44 | + |
| 45 | + |
| 46 | +class TestIsaParity(unittest.TestCase): |
| 47 | + DB = "test_isa_parity.sqlite" |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def setUpClass(cls): |
| 51 | + if os.path.exists(cls.DB): |
| 52 | + os.remove(cls.DB) |
| 53 | + cls.db = SQLiteDB(cls.DB) |
| 54 | + cls.db.add_branch("a") |
| 55 | + # probe isa {probe, element, base}; element isa {element, base}; |
| 56 | + # session isa {session, base}. 'thing' is class thing with superclass |
| 57 | + # base only, but carries an incidental top-level 'element' field -- it |
| 58 | + # must NOT match isa('element') (the old heuristic wrongly would). |
| 59 | + specs = [ |
| 60 | + ("probe", ["element", "base"], {"probe": {"name": "p"}}), |
| 61 | + ("element", ["base"], {"element": {"name": "e"}}), |
| 62 | + ("session", ["base"], {"session": {"name": "s"}}), |
| 63 | + ("thing", ["base"], {"element": {"note": "incidental, not a superclass"}}), |
| 64 | + ] |
| 65 | + cls.by_class = {} |
| 66 | + cls.docs = [] |
| 67 | + for class_name, superclasses, fields in specs: |
| 68 | + d = _doc(class_name, superclasses, fields) |
| 69 | + cls.by_class[class_name] = d |
| 70 | + cls.docs.append(d) |
| 71 | + cls.db._do_add_doc(d, "a") |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def tearDownClass(cls): |
| 75 | + cls.db._close_db() |
| 76 | + if os.path.exists(cls.DB): |
| 77 | + os.remove(cls.DB) |
| 78 | + |
| 79 | + def _brute(self, class_name): |
| 80 | + ss = Query("", "isa", class_name).to_search_structure() |
| 81 | + return sorted( |
| 82 | + d.id() for d in self.docs if field_search(d.document_properties, ss) |
| 83 | + ) |
| 84 | + |
| 85 | + def _sql(self, class_name): |
| 86 | + return sorted(self.db.search(Query("", "isa", class_name), branch_id="a")) |
| 87 | + |
| 88 | + def _expect(self, *class_names): |
| 89 | + return sorted(self.by_class[c].id() for c in class_names) |
| 90 | + |
| 91 | + def test_isa_own_class(self): |
| 92 | + self.assertEqual(self._sql("probe"), self._expect("probe")) |
| 93 | + self.assertEqual(self._brute("probe"), self._expect("probe")) |
| 94 | + |
| 95 | + def test_isa_superclass_matches_descendant(self): |
| 96 | + # isa(element) -> the probe (element is its superclass) AND element. |
| 97 | + # Must NOT include 'thing' despite its incidental 'element' field. |
| 98 | + self.assertEqual(self._sql("element"), self._expect("probe", "element")) |
| 99 | + self.assertEqual(self._brute("element"), self._expect("probe", "element")) |
| 100 | + |
| 101 | + def test_isa_root_superclass_matches_all(self): |
| 102 | + self.assertEqual( |
| 103 | + self._sql("base"), self._expect("probe", "element", "session", "thing") |
| 104 | + ) |
| 105 | + self.assertEqual( |
| 106 | + self._brute("base"), self._expect("probe", "element", "session", "thing") |
| 107 | + ) |
| 108 | + |
| 109 | + def test_isa_unrelated_matches_nothing(self): |
| 110 | + self.assertEqual(self._sql("nonexistent"), []) |
| 111 | + self.assertEqual(self._brute("nonexistent"), []) |
| 112 | + |
| 113 | + def test_sql_and_brute_force_agree(self): |
| 114 | + for c in ["probe", "element", "base", "session", "thing", "nonexistent"]: |
| 115 | + self.assertEqual( |
| 116 | + self._sql(c), self._brute(c), f"isa({c}) SQL vs brute-force mismatch" |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +class TestSqlFieldNameValidation(unittest.TestCase): |
| 121 | + """A query field name with SQL metacharacters must not be interpolated into |
| 122 | + SQL; the leaf falls back to the injection-free brute-force path (returns the |
| 123 | + correct empty set here rather than raising or injecting).""" |
| 124 | + |
| 125 | + DB = "test_isa_fieldname.sqlite" |
| 126 | + |
| 127 | + @classmethod |
| 128 | + def setUpClass(cls): |
| 129 | + if os.path.exists(cls.DB): |
| 130 | + os.remove(cls.DB) |
| 131 | + cls.db = SQLiteDB(cls.DB) |
| 132 | + cls.db.add_branch("a") |
| 133 | + cls.db._do_add_doc(_doc("probe", ["base"], {"probe": {"name": "p"}}), "a") |
| 134 | + |
| 135 | + @classmethod |
| 136 | + def tearDownClass(cls): |
| 137 | + cls.db._close_db() |
| 138 | + if os.path.exists(cls.DB): |
| 139 | + os.remove(cls.DB) |
| 140 | + |
| 141 | + def test_malicious_field_name_does_not_inject(self): |
| 142 | + # Classic injection attempt as the field name. |
| 143 | + evil = "base.id' OR '1'='1" |
| 144 | + # Must not raise, must not return everything via injection. |
| 145 | + result = self.db.search(Query(evil, "exact_string", "anything"), branch_id="a") |
| 146 | + self.assertEqual(result, []) |
| 147 | + |
| 148 | + def test_legitimate_dotted_field_still_works(self): |
| 149 | + result = self.db.search( |
| 150 | + Query("base.name", "exact_string", "probe"), branch_id="a" |
| 151 | + ) |
| 152 | + self.assertEqual(result, ["id_probe"]) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + unittest.main() |
0 commit comments