Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix: address security, syntax, and test issues in Snowflake ingestor
Sameer630585e302bbc081
diff --git a/pyproject.toml b/pyproject.tomlindex 0d79f0c5..6c142969 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -101,11 +101,9 @@ parse-docling = ["docling>=1.0.0"]# ---- Database Connectors ----db-snowflake = ["snowflake-connector-python>=3.0.0", "cryptography>=3.4.0"]-db-duckdb = ["duckdb>=0.8.0"]-db-mongodb = ["pymongo>=4.0.0"]db-all = [- "semantica[db-snowflake,db-duckdb,db-mongodb]"+ "semantica[db-snowflake]"]# ---- Embedding / Models ----diff --git a/semantica/ingest/snowflake_ingestor.py b/semantica/ingest/snowflake_ingestor.pyindex ce322d89..a3cebb4e 100644--- a/semantica/ingest/snowflake_ingestor.py+++ b/semantica/ingest/snowflake_ingestor.py@@ -421,25 +421,17 @@ class SnowflakeIngestor:self.logger.debug("Snowflake ingestor initialized")- def _validate_identifier(self, identifier: str, identifier_type: str = "identifier"):- """Validate SQL identifier to prevent malformed names.+ def _escape_identifier(self, identifier: str) -> str:+ """Escape SQL identifier by wrapping in quotes and doubling internal quotes.Args:- identifier: The identifier to validate- identifier_type: Type of identifier (for error messages)+ identifier: The identifier to escape- Raises:- ValidationError: If identifier contains potentially problematic characters+ Returns:+ Properly escaped identifier safe for SQL interpolation"""- if not identifier:- return-- # Check for quotes or other potentially problematic characters- if '"' in identifier or "'" in identifier or ";" in identifier:- raise ValidationError(- f"Invalid {identifier_type}: '{identifier}'. "- f"Identifiers must not contain quotes or semicolons."- )+ escaped = identifier.replace('"', '""')+ return f'"{escaped}"'def ingest_table(self,@@ -495,16 +487,19 @@ class SnowflakeIngestor:)try:- # Validate identifiers- self._validate_identifier(table_name, "table_name")- self._validate_identifier(database, "database")- self._validate_identifier(schema, "schema")-# Connect to Snowflakeconn = self.connector.connect()- # Build query- table_ref = f'"{database}"."{schema}"."{table_name}"' if database and schema else f'"{table_name}"'+ # Build query with escaped identifiers+ if database and schema:+ table_ref = (+ f"{self._escape_identifier(database)}."+ f"{self._escape_identifier(schema)}."+ f"{self._escape_identifier(table_name)}"+ )+ else:+ table_ref = self._escape_identifier(table_name)+query = f"SELECT * FROM {table_ref}"if where:@@ -700,22 +695,17 @@ class SnowflakeIngestor:"Database name is required for schema introspection. ""Provide via 'database' parameter or set default database in connector.")-- # Validate identifiers- self._validate_identifier(table_name, "table_name")- self._validate_identifier(database, "database")- self._validate_identifier(schema, "schema")conn = self.connector.connect()- # Get column information+ # Get column information with escaped identifiersquery = f"""SELECTCOLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT- FROM {database}.INFORMATION_SCHEMA.COLUMNS+ FROM {self._escape_identifier(database)}.INFORMATION_SCHEMA.COLUMNSWHERE TABLE_SCHEMA = '{schema}'AND TABLE_NAME = '{table_name}'ORDER BY ORDINAL_POSITION@@ -726,11 +716,11 @@ class SnowflakeIngestor:columns = cursor.fetchall()cursor.close()- # Get primary key information+ # Get primary key information with escaped identifierspk_query = f"""SELECT COLUMN_NAME- FROM {database}.INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc- JOIN {database}.INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu+ FROM {self._escape_identifier(database)}.INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc+ JOIN {self._escape_identifier(database)}.INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcuON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAMEWHERE tc.TABLE_SCHEMA = '{schema}'AND tc.TABLE_NAME = '{table_name}'@@ -790,16 +780,13 @@ class SnowflakeIngestor:"Database name is required for listing tables. ""Provide via 'database' parameter or set default database in connector.")-- # Validate identifiers- self._validate_identifier(database, "database")- self._validate_identifier(schema, "schema")conn = self.connector.connect()+ # Build query with escaped database identifierquery = f"""SELECT TABLE_NAME- FROM {database}.INFORMATION_SCHEMA.TABLES+ FROM {self._escape_identifier(database)}.INFORMATION_SCHEMA.TABLESWHERE TABLE_SCHEMA = '{schema}'AND TABLE_TYPE = 'BASE TABLE'ORDER BY TABLE_NAME