LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-4239 [FIX] Remove eval() on DB error messages and exec() in settings (#2321)
Jaseem Jasd1130cc5bb54
diff --git a/backend/backend/settings/base.py b/backend/backend/settings/base.pyindex aa03b577a..82c73c555 100644--- a/backend/backend/settings/base.py+++ b/backend/backend/settings/base.py@@ -1020,13 +1020,10 @@ SOCIAL_AUTH_JSONFIELD_ENABLED = TrueSOCIAL_AUTH_FIELDS_STORED_IN_SESSION = ["oauth-key", "connector-guid"]SOCIAL_AUTH_TRAILING_SLASH = False-for key in [- "GOOGLE_OAUTH2_KEY",- "GOOGLE_OAUTH2_SECRET",- "AZUREAD_TENANT_OAUTH2_KEY",- "AZUREAD_TENANT_OAUTH2_SECRET",-]:- exec(f"SOCIAL_AUTH_{key} = os.environ.get('{key}')")+SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get("GOOGLE_OAUTH2_KEY")+SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get("GOOGLE_OAUTH2_SECRET")+SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = os.environ.get("AZUREAD_TENANT_OAUTH2_KEY")+SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = os.environ.get("AZUREAD_TENANT_OAUTH2_SECRET")SOCIAL_AUTH_PIPELINE = (# Checks if user is authenticateddiff --git a/unstract/connectors/src/unstract/connectors/databases/exceptions_helper.py b/unstract/connectors/src/unstract/connectors/databases/exceptions_helper.pyindex 367993783..4b93476c1 100644--- a/unstract/connectors/src/unstract/connectors/databases/exceptions_helper.py+++ b/unstract/connectors/src/unstract/connectors/databases/exceptions_helper.py@@ -10,8 +10,13 @@ class ExceptionHelper:Returns:str: Extracted and stripped error details as string"""- error_message = str(e)- error_code, error_details = eval(error_message)+ # Drivers raise these as (error_code, error_details), sometimes wrapped+ # in a single tuple argument; read the args directly rather than+ # evaluating the server-supplied message.+ args = e.args+ if len(args) == 1 and isinstance(args[0], tuple):+ args = args[0]+ error_details = args[1] if len(args) >= 2 else str(e)if isinstance(error_details, bytes):error_details = error_details.decode("utf-8")return str(error_details).strip()diff --git a/unstract/connectors/tests/databases/test_exceptions_helper.py b/unstract/connectors/tests/databases/test_exceptions_helper.pynew file mode 100644index 000000000..d3c99b1f6--- /dev/null+++ b/unstract/connectors/tests/databases/test_exceptions_helper.py@@ -0,0 +1,44 @@+import os+import unittest++from unstract.connectors.databases.exceptions_helper import ExceptionHelper+++class TestExtractByteException(unittest.TestCase):+ """Tests for ExceptionHelper.extract_byte_exception()."""++ def test_bytes_details(self):+ # pymssql shape: (code, b"message")+ e = Exception(208, b"Invalid object name 'missing'.\n")+ self.assertEqual(+ ExceptionHelper.extract_byte_exception(e), "Invalid object name 'missing'."+ )++ def test_str_details(self):+ # pymysql shape: (code, "message")+ e = Exception(1064, "You have an error in your SQL syntax ")+ self.assertEqual(+ ExceptionHelper.extract_byte_exception(e),+ "You have an error in your SQL syntax",+ )++ def test_single_arg_falls_back_to_message(self):+ e = Exception("connection lost")+ self.assertEqual(ExceptionHelper.extract_byte_exception(e), "connection lost")++ def test_nested_tuple_details(self):+ # pymssql connect() shape: ((code, b"message"),)+ e = Exception((20009, b"Unable to connect\n"))+ self.assertEqual(ExceptionHelper.extract_byte_exception(e), "Unable to connect")++ def test_message_is_not_evaluated(self):+ env_key = "UNSTRACT_TEST_EXCEPTION_HELPER_EVALUATED"+ self.addCleanup(os.environ.pop, env_key, None)+ payload = f"(1, __import__('os').environ.__setitem__('{env_key}', '1') or 'x')"+ e = Exception(payload)+ self.assertEqual(ExceptionHelper.extract_byte_exception(e), payload)+ self.assertNotIn(env_key, os.environ)+++if __name__ == "__main__":+ unittest.main()