File tree Expand file tree Collapse file tree
src/haystack_integrations/document_stores/pgvector Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
22#
33# SPDX-License-Identifier: Apache-2.0
4+
5+ import re
46from datetime import datetime
57from itertools import chain
68from typing import Any , Literal
1921}
2022
2123NO_VALUE = "no_value"
24+ SAFE_META_FIELD_RE = re .compile (r"^[a-zA-Z0-9_-]+$" )
2225
2326
2427def _validate_filters (filters : dict [str , Any ] | None = None ) -> None :
@@ -128,6 +131,12 @@ def _treat_meta_field(field: str, value: Any) -> str:
128131
129132 # use the ->> operator to access keys in the meta JSONB field
130133 field_name = field .split ("." , 1 )[- 1 ]
134+ if not SAFE_META_FIELD_RE .match (field_name ):
135+ msg = (
136+ f"Invalid metadata field name '{ field_name } '. "
137+ "Only alphanumeric characters, dashes, and underscores are allowed."
138+ )
139+ raise FilterError (msg )
131140 field = f"meta->>'{ field_name } '"
132141
133142 # meta fields are stored as strings in the JSONB field,
Original file line number Diff line number Diff line change @@ -144,6 +144,11 @@ def test_treat_meta_field():
144144 assert _treat_meta_field (field = "meta.name" , value = None ) == "meta->>'name'"
145145
146146
147+ def test_treat_meta_field_rejects_unsafe_metadata_key ():
148+ with pytest .raises (FilterError , match = "Invalid metadata field name" ):
149+ _treat_meta_field (field = "meta.name' OR 1=1 --" , value = "x" )
150+
151+
147152def test_comparison_condition_missing_operator ():
148153 condition = {"field" : "meta.type" , "value" : "article" }
149154 with pytest .raises (FilterError ):
You can’t perform that action at this time.
0 commit comments