nlp2sql 0.2.0rc19__tar.gz → 0.2.0rc20__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/workflows/ci.yml +5 -3
  2. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/CHANGELOG.md +30 -0
  3. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/PKG-INFO +2 -1
  4. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/README.md +1 -0
  5. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/SECURITY.md +12 -1
  6. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/mcp_server/README.md +4 -0
  7. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/mcp_server/__init__.py +1 -1
  8. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/mcp_server/pyproject.toml +2 -2
  9. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/pyproject.toml +1 -1
  10. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/__init__.py +1 -1
  11. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/postgres_repository.py +9 -8
  12. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/redshift_adapter.py +6 -9
  13. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/config/settings.py +1 -1
  14. nlp2sql-0.2.0rc20/src/nlp2sql/core/sql_safety.py +271 -0
  15. nlp2sql-0.2.0rc20/tests/test_readonly_connections.py +48 -0
  16. nlp2sql-0.2.0rc20/tests/test_readonly_execution_against_postgres.py +77 -0
  17. nlp2sql-0.2.0rc20/tests/test_sql_safety.py +128 -0
  18. nlp2sql-0.2.0rc20/tests/test_sql_safety_against_postgres.py +91 -0
  19. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/uv.lock +1 -1
  20. nlp2sql-0.2.0rc19/src/nlp2sql/core/sql_safety.py +0 -108
  21. nlp2sql-0.2.0rc19/tests/test_sql_safety.py +0 -40
  22. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/commands/db.md +0 -0
  23. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/commands/quality.md +0 -0
  24. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/commands/test.md +0 -0
  25. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/rules/adapters.md +0 -0
  26. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/rules/architecture.md +0 -0
  27. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/rules/python-style.md +0 -0
  28. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/rules/testing.md +0 -0
  29. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.claude/settings.json +0 -0
  30. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  31. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  32. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/pull_request_template.md +0 -0
  33. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/scripts/verify_release_tag.py +0 -0
  34. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/workflows/publish-pypi.yml +0 -0
  35. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.github/workflows/publish-testpypi.yml +0 -0
  36. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.gitignore +0 -0
  37. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.mcp.json.example +0 -0
  38. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/.python-version +0 -0
  39. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/CLAUDE.md +0 -0
  40. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/CODE_OF_CONDUCT.md +0 -0
  41. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/CONTRIBUTING.md +0 -0
  42. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/LICENSE +0 -0
  43. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/assets/nlp2sql-hero-dark.svg +0 -0
  44. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/assets/nlp2sql-hero-light.svg +0 -0
  45. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/benchmarks/enterprise_benchmark_results.json +0 -0
  46. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/.env.example +0 -0
  47. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/README.md +0 -0
  48. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/docker-compose.yml +0 -0
  49. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/get-db-urls.sh +0 -0
  50. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/init-schema.sql +0 -0
  51. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/large-schema.sql +0 -0
  52. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/localstack-init.sh +0 -0
  53. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docker/test-redshift.sh +0 -0
  54. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docs/API.md +0 -0
  55. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docs/ARCHITECTURE.md +0 -0
  56. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docs/CONFIGURATION.md +0 -0
  57. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docs/ENTERPRISE.md +0 -0
  58. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/docs/Redshift.md +0 -0
  59. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/README.md +0 -0
  60. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/advanced/test_multiple_providers.py +0 -0
  61. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/advanced/test_simple_api.py +0 -0
  62. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/database_specific/test_odoo_integration.py +0 -0
  63. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/default_examples.json +0 -0
  64. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/documentation/real_world_example.py +0 -0
  65. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/getting_started/basic_usage.py +0 -0
  66. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/getting_started/simple_demo.py +0 -0
  67. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/getting_started/test_api_setup.py +0 -0
  68. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/schema_management/test_auto_schema.py +0 -0
  69. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/schema_management/test_schema_filters_comprehensive.py +0 -0
  70. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/.dockerignore +0 -0
  71. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/.env.example +0 -0
  72. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/DOCKER_README.md +0 -0
  73. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/DOCKER_SETUP.md +0 -0
  74. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/DOCKER_TESTING_SUMMARY.md +0 -0
  75. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/Dockerfile +0 -0
  76. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/INDEX.md +0 -0
  77. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/README.md +0 -0
  78. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/docker/init-ecommerce-schema.sql +0 -0
  79. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/docker-compose.yml +0 -0
  80. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/main.py +0 -0
  81. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/pyproject.toml +0 -0
  82. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/examples/strands-fastapi-agent/test-docker-endpoints.sh +0 -0
  83. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/install-dev.sh +0 -0
  84. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/mcp_server/example_config.json +0 -0
  85. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/mcp_server/server.py +0 -0
  86. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/__init__.py +0 -0
  87. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/_provider_errors.py +0 -0
  88. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/anthropic_adapter.py +0 -0
  89. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/default_error_classifier.py +0 -0
  90. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/default_repair_policy.py +0 -0
  91. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/dict_semantic_resolver.py +0 -0
  92. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/file_semantic_resolver.py +0 -0
  93. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/gemini_adapter.py +0 -0
  94. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/local_embedding_adapter.py +0 -0
  95. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/noop_semantic_resolver.py +0 -0
  96. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/noop_semantic_validator.py +0 -0
  97. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/openai_adapter.py +0 -0
  98. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/openai_embedding_adapter.py +0 -0
  99. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/pgvector_example_repository.py +0 -0
  100. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/regex_query_validator.py +0 -0
  101. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/adapters/schema_repository_execution_adapter.py +0 -0
  102. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/cli.py +0 -0
  103. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/client.py +0 -0
  104. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/config/__init__.py +0 -0
  105. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/__init__.py +0 -0
  106. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/database_prompts.py +0 -0
  107. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/entities.py +0 -0
  108. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/provider_config.py +0 -0
  109. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/result.py +0 -0
  110. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/runtime.py +0 -0
  111. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/core/sql_keywords.py +0 -0
  112. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/exceptions/__init__.py +0 -0
  113. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/factories/__init__.py +0 -0
  114. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/factories/repository_factory.py +0 -0
  115. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/__init__.py +0 -0
  116. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/ai_provider.py +0 -0
  117. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/cache.py +0 -0
  118. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/embedding_provider.py +0 -0
  119. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/error_classifier.py +0 -0
  120. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/example_repository.py +0 -0
  121. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/query_execution.py +0 -0
  122. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/query_optimizer.py +0 -0
  123. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/query_safety.py +0 -0
  124. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/query_validator.py +0 -0
  125. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/repair_policy.py +0 -0
  126. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/schema_repository.py +0 -0
  127. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/schema_strategy.py +0 -0
  128. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/semantic_resolver.py +0 -0
  129. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/ports/semantic_validator.py +0 -0
  130. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/__init__.py +0 -0
  131. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/analyzer.py +0 -0
  132. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/embedding_manager.py +0 -0
  133. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/example_loader.py +0 -0
  134. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/example_store.py +0 -0
  135. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/filters.py +0 -0
  136. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/schema/manager.py +0 -0
  137. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/__init__.py +0 -0
  138. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/example_selection_service.py +0 -0
  139. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/prompt_assembly_service.py +0 -0
  140. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/query_analysis_service.py +0 -0
  141. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/query_repair_service.py +0 -0
  142. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/query_service.py +0 -0
  143. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/semantic_resolution_service.py +0 -0
  144. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/semantic_validation_service.py +0 -0
  145. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/services/sql_intent_planning_service.py +0 -0
  146. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/__init__.py +0 -0
  147. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/artifact_loader.py +0 -0
  148. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/helpers.py +0 -0
  149. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/logger.py +0 -0
  150. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/redaction.py +0 -0
  151. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/semantic_prompt.py +0 -0
  152. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/src/nlp2sql/utils/storage.py +0 -0
  153. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/test_tmp/.gitignore +0 -0
  154. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/conftest.py +0 -0
  155. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/data/gold_questions.json +0 -0
  156. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/fakes/__init__.py +0 -0
  157. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/fakes/fake_openai.py +0 -0
  158. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/mcp/__init__.py +0 -0
  159. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/mcp/test_sql_security.py +0 -0
  160. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_artifact_loader.py +0 -0
  161. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_basic.py +0 -0
  162. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_batch_scoring.py +0 -0
  163. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cli_against_postgres.py +0 -0
  164. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cli_cache.py +0 -0
  165. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cli_inspect.py +0 -0
  166. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cli_providers.py +0 -0
  167. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cli_semantic_support.py +0 -0
  168. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cli_setup.py +0 -0
  169. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_client_hooks.py +0 -0
  170. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_client_stats.py +0 -0
  171. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_column_validation.py +0 -0
  172. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_connect_embeddings.py +0 -0
  173. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_cost_per_question.py +0 -0
  174. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_database_prompts.py +0 -0
  175. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_dsl_integration.py +0 -0
  176. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_embedding_manager.py +0 -0
  177. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_false_invalid_regression.py +0 -0
  178. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_first_run.py +0 -0
  179. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_integration.py +0 -0
  180. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_library_logging.py +0 -0
  181. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_library_output.py +0 -0
  182. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_model_defaults.py +0 -0
  183. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_pgvector_example_repository.py +0 -0
  184. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_postgres_integration.py +0 -0
  185. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_provider_compat_llm.py +0 -0
  186. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_provider_errors.py +0 -0
  187. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_query_result.py +0 -0
  188. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_query_service_contract.py +0 -0
  189. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_redaction.py +0 -0
  190. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_redshift_integration.py +0 -0
  191. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_refresh_schema.py +0 -0
  192. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_repository_cache.py +0 -0
  193. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_repository_factory.py +0 -0
  194. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_retrieval_pipeline.py +0 -0
  195. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_schema_context_cache.py +0 -0
  196. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_startup.py +0 -0
  197. {nlp2sql-0.2.0rc19 → nlp2sql-0.2.0rc20}/tests/test_version_consistency.py +0 -0
@@ -97,8 +97,10 @@ jobs:
97
97
 
98
98
  # README Quick Start per provider, the false-invalid gold set, the cost of one
99
99
  # question (LLM calls, tokens, database connections) and what an application gets
100
- # back (no output, rows, validation reasons), and the CLI against the database (row
101
- # counts, caches), with the OpenAI SDK pointed at tests/fakes/fake_openai.py (no keys, no cost).
100
+ # back (no output, rows, validation reasons), the CLI against the database (row
101
+ # counts, caches), SQL that hides code in literals never reaching the database, and
102
+ # the database refusing writes the check misses, with the OpenAI SDK pointed at
103
+ # tests/fakes/fake_openai.py (no keys, no cost).
102
104
  - name: First-run regression tests
103
105
  env:
104
106
  PYTHONPATH: src
@@ -108,7 +110,7 @@ jobs:
108
110
  NLP2SQL_TEST_POSTGRES_USER: testuser
109
111
  NLP2SQL_TEST_POSTGRES_PASS: testpass
110
112
  NLP2SQL_TEST_POSTGRES_DB: testdb
111
- run: pytest tests/test_first_run.py tests/test_false_invalid_regression.py tests/test_cost_per_question.py tests/test_library_output.py tests/test_cli_against_postgres.py -m integration -q
113
+ run: pytest tests/test_first_run.py tests/test_false_invalid_regression.py tests/test_cost_per_question.py tests/test_library_output.py tests/test_cli_against_postgres.py tests/test_sql_safety_against_postgres.py tests/test_readonly_execution_against_postgres.py -m integration -q
112
114
 
113
115
  # Installs the built wheel the way a user does: fresh environment, no lock
114
116
  # file, latest dependency versions. Catches undeclared dependencies (rc13-rc15
@@ -22,6 +22,36 @@ pre-1.0.
22
22
 
23
23
  ## [Unreleased]
24
24
 
25
+ ## [0.2.0rc20] - 2026-11-08
26
+
27
+ ### Security
28
+ - **The read-only check reads literals and comments the way PostgreSQL does.**
29
+ `is_safe_query` removed string literals with a regex that knew only `'...'` and
30
+ `"..."`. A query could hide another statement, or a prohibited function, inside a
31
+ dollar-quoted (`$$...$$`) or `E'...'` literal, and the check passed it: `ask(validate=True)`,
32
+ `execute_query()` and the MCP server's `run_sql` then ran it. The query is now split into
33
+ tokens like PostgreSQL's lexer does, and the checks look only at its code. Affects every
34
+ earlier release.
35
+ - `SELECT ... INTO` is rejected (it creates a table).
36
+ - Prohibited functions are found when quoted (`"pg_sleep"(1)`) or schema-qualified, and
37
+ `query_to_xml`, `query_to_xml_and_xmlschema`, `query_to_xmlschema` and `ts_stat` are
38
+ prohibited: they run SQL given as a string, which the check cannot see into.
39
+ - Rejected because servers read them differently: a string literal whose end depends on
40
+ whether `\` escapes the next character (`'it\'s'`, `'C:\'`; write quotes as `''`),
41
+ nested `/* */` comments and Unicode-escaped identifiers (`U&"..."`). Unterminated
42
+ literals, quoted names and comments are rejected too.
43
+ - A leading comment (`-- top customers` before `SELECT`) is now allowed.
44
+ - `apply_row_limit` added `LIMIT` after a trailing `-- comment`, where it became part of the
45
+ comment, and skipped it when `LIMIT` appeared inside a `$$...$$` literal or a comment. A
46
+ query that ended with a comment returned every row instead of the limit.
47
+ - **Queries run in read-only transactions** on PostgreSQL and Redshift, so the database
48
+ refuses a write that the check misses. Before, a write was undone only because the
49
+ connection closed without a commit: a `COMMIT` inside the query kept what followed it,
50
+ and `nextval()` moved a sequence for good. Every connection now asks for read-only
51
+ transactions, and on PostgreSQL `default_transaction_read_only` is also set for the
52
+ query's session, so a transaction started after a `COMMIT` is read-only too. `SECURITY.md`
53
+ shows how to create a database user that can only read, which is still the strongest guard.
54
+
25
55
  ## [0.2.0rc19] - 2026-11-05
26
56
 
27
57
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: nlp2sql
3
- Version: 0.2.0rc19
3
+ Version: 0.2.0rc20
4
4
  Summary: Enterprise-ready Natural Language to SQL converter with multi-provider support. Built for production scale (1000+ tables) with Clean Architecture.
5
5
  Project-URL: Homepage, https://github.com/luiscarbonel1991/nlp2sql
6
6
  Project-URL: Bug Tracker, https://github.com/luiscarbonel1991/nlp2sql/issues
@@ -111,6 +111,7 @@ The library is designed for both simple schemas and large warehouses, but all pu
111
111
  - **DSL-first API**: `connect()` returns an `NLP2SQL` client with `ask()`, `validate()`, `explain()`, and `suggest()`
112
112
  - **Business-aware generation**: optional `SemanticContext` adds canonical tables, metrics, dimensions, rules, and mappings
113
113
  - **Execution modes**: generate only, generate plus validate, and generate plus validate plus repair
114
+ - **Read-only execution**: queries pass a safety check and run in read-only transactions; connect with a database user that can only read (see [SECURITY.md](https://github.com/luiscarbonel1991/nlp2sql/blob/main/SECURITY.md#security-best-practices))
114
115
  - **Few-shot examples**: pass example lists directly or use an example repository implementation
115
116
  - **Large schema support**: FAISS plus TF-IDF hybrid retrieval, schema filters, and disk-backed caches
116
117
  - **Multiple providers**: OpenAI, Anthropic, and Gemini built in, or your own `AIProviderPort` passed to `QueryGenerationService`
@@ -31,6 +31,7 @@ The library is designed for both simple schemas and large warehouses, but all pu
31
31
  - **DSL-first API**: `connect()` returns an `NLP2SQL` client with `ask()`, `validate()`, `explain()`, and `suggest()`
32
32
  - **Business-aware generation**: optional `SemanticContext` adds canonical tables, metrics, dimensions, rules, and mappings
33
33
  - **Execution modes**: generate only, generate plus validate, and generate plus validate plus repair
34
+ - **Read-only execution**: queries pass a safety check and run in read-only transactions; connect with a database user that can only read (see [SECURITY.md](https://github.com/luiscarbonel1991/nlp2sql/blob/main/SECURITY.md#security-best-practices))
34
35
  - **Few-shot examples**: pass example lists directly or use an example repository implementation
35
36
  - **Large schema support**: FAISS plus TF-IDF hybrid retrieval, schema filters, and disk-backed caches
36
37
  - **Multiple providers**: OpenAI, Anthropic, and Gemini built in, or your own `AIProviderPort` passed to `QueryGenerationService`
@@ -58,7 +58,18 @@ Security issues relevant to this project include:
58
58
  When using nlp2sql in production:
59
59
 
60
60
  1. **Never commit `.env` files** - Use environment variables or secret management
61
- 2. **Use read-only database credentials** when possible
61
+ 2. **Connect with a database user that can only read** - nlp2sql checks every query it executes and runs it in a read-only transaction, but the database's own permissions are the guarantee that holds if a check is ever bypassed. In PostgreSQL:
62
+
63
+ ```sql
64
+ CREATE ROLE nlp2sql_reader LOGIN PASSWORD '...';
65
+ GRANT CONNECT ON DATABASE mydb TO nlp2sql_reader;
66
+ GRANT USAGE ON SCHEMA public TO nlp2sql_reader;
67
+ GRANT SELECT ON ALL TABLES IN SCHEMA public TO nlp2sql_reader;
68
+ ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO nlp2sql_reader;
69
+ ALTER ROLE nlp2sql_reader SET default_transaction_read_only = on;
70
+ ```
71
+
72
+ Do not use a superuser: it can read server files (`pg_read_file`) and end other sessions.
62
73
  3. **Enable query validation** to prevent dangerous SQL execution
63
74
  4. **Review generated SQL** before executing on sensitive data
64
75
  5. **Keep dependencies updated** - Run `uv sync` regularly
@@ -367,6 +367,10 @@ All SQL execution goes through security validation:
367
367
  - `COPY`, `EXEC`, `CALL`
368
368
  - Multiple statements (`;` injection)
369
369
 
370
+ ### Read-only Transactions
371
+
372
+ Queries run in read-only transactions (PostgreSQL and Redshift), so the database refuses a write even if the check above missed it. Configure the server with a database user that can only read; see [SECURITY.md](https://github.com/luiscarbonel1991/nlp2sql/blob/main/SECURITY.md#security-best-practices).
373
+
370
374
  ### Row Limits
371
375
 
372
376
  - Default: 100 rows
@@ -1,3 +1,3 @@
1
1
  """nlp2sql MCP Server - Expose nlp2sql as MCP tools."""
2
2
 
3
- __version__ = "0.2.0rc19"
3
+ __version__ = "0.2.0rc20"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nlp2sql-mcp"
3
- version = "0.2.0rc19"
3
+ version = "0.2.0rc20"
4
4
  description = "MCP server for nlp2sql - Natural Language to SQL conversion"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -21,4 +21,4 @@ build-backend = "hatchling.build"
21
21
  [tool.mcp]
22
22
  name = "nlp2sql"
23
23
  description = "Convert natural language queries to SQL using multiple AI providers"
24
- version = "0.2.0rc19"
24
+ version = "0.2.0rc20"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nlp2sql"
3
- version = "0.2.0rc19"
3
+ version = "0.2.0rc20"
4
4
  description = "Enterprise-ready Natural Language to SQL converter with multi-provider support. Built for production scale (1000+ tables) with Clean Architecture."
5
5
  readme = "README.md"
6
6
  license = {text = "MIT"}
@@ -49,7 +49,7 @@ from .schema.example_store import ExampleStore
49
49
  from .services.query_service import QueryGenerationService
50
50
  from .utils.logger import get_logger
51
51
 
52
- __version__ = "0.2.0rc19"
52
+ __version__ = "0.2.0rc20"
53
53
 
54
54
  # Silent unless the application configures logging (the logging HOWTO's advice for libraries)
55
55
  _logging.getLogger(__name__).addHandler(_logging.NullHandler())
@@ -84,8 +84,11 @@ class PostgreSQLRepository(SchemaRepositoryPort):
84
84
  return params
85
85
 
86
86
  def _get_connection(self):
87
- """Create a new database connection."""
88
- return psycopg2.connect(**self._connection_params)
87
+ """Create a new database connection whose transactions are read-only."""
88
+ conn = psycopg2.connect(**self._connection_params)
89
+ # nlp2sql only reads: the database refuses a write the SQL safety check misses
90
+ conn.set_session(readonly=True)
91
+ return conn
89
92
 
90
93
  def _execute_query(self, query: str, params=None) -> List[Dict]:
91
94
  """Execute a query and return results as list of dicts."""
@@ -895,6 +898,9 @@ class PostgreSQLRepository(SchemaRepositoryPort):
895
898
  psycopg2_sql.SQL("SET search_path TO {}, public").format(psycopg2_sql.Identifier(self.schema_name))
896
899
  )
897
900
  cursor.execute(f"SET statement_timeout TO {timeout_seconds * 1000}")
901
+ # set_session(readonly=True) covers only the transaction it begins; a COMMIT
902
+ # inside the query would start one that can write, unless this is the default
903
+ cursor.execute("SET default_transaction_read_only = on")
898
904
  cursor.close()
899
905
 
900
906
  # Execute the actual query
@@ -913,12 +919,7 @@ class PostgreSQLRepository(SchemaRepositoryPort):
913
919
  "execution_time_ms": execution_time_ms,
914
920
  }
915
921
  finally:
916
- try:
917
- cursor = conn.cursor()
918
- cursor.execute("SET statement_timeout TO 0")
919
- cursor.close()
920
- except Exception:
921
- pass
922
+ # Closing rolls the transaction back and ends the session with its settings
922
923
  conn.close()
923
924
 
924
925
  try:
@@ -175,8 +175,11 @@ class RedshiftRepository(SchemaRepositoryPort):
175
175
  }
176
176
 
177
177
  def _get_connection(self):
178
- """Create a new database connection."""
179
- return psycopg2.connect(**self._connection_params)
178
+ """Create a new database connection whose transactions are read-only."""
179
+ conn = psycopg2.connect(**self._connection_params)
180
+ # nlp2sql only reads: Redshift refuses a write the SQL safety check misses (BEGIN READ ONLY)
181
+ conn.set_session(readonly=True)
182
+ return conn
180
183
 
181
184
  async def initialize(self) -> None:
182
185
  """Initialize database connections and detect available system views."""
@@ -716,13 +719,7 @@ class RedshiftRepository(SchemaRepositoryPort):
716
719
  "execution_time_ms": execution_time_ms,
717
720
  }
718
721
  finally:
719
- # Reset statement timeout before closing (match PostgreSQL behavior)
720
- try:
721
- cursor = conn.cursor()
722
- cursor.execute("SET statement_timeout TO 0")
723
- cursor.close()
724
- except Exception:
725
- pass # Best effort reset
722
+ # Closing rolls the transaction back and ends the session with its settings
726
723
  conn.close()
727
724
 
728
725
  try:
@@ -29,7 +29,7 @@ class Settings(BaseSettings):
29
29
 
30
30
  # General settings
31
31
  app_name: str = "nlp2sql"
32
- version: str = "0.2.0rc19"
32
+ version: str = "0.2.0rc20"
33
33
  debug: bool = Field(default=False, validation_alias="NLP2SQL_DEBUG")
34
34
  log_level: LogLevel = Field(default=LogLevel.INFO, validation_alias="NLP2SQL_LOG_LEVEL")
35
35
 
@@ -0,0 +1,271 @@
1
+ """SQL safety constants and utilities — pure business logic, no external dependencies."""
2
+
3
+ import re
4
+ from itertools import pairwise
5
+ from typing import NamedTuple
6
+
7
+ # SQL patterns that are not allowed for security (read-only enforcement).
8
+ # Checked on the query's code only: literals, quoted names and comments are left out.
9
+ DANGEROUS_SQL_PATTERNS = [
10
+ r"\bINSERT\b",
11
+ r"\bUPDATE\b",
12
+ r"\bDELETE\b",
13
+ r"\bDROP\b",
14
+ r"\bTRUNCATE\b",
15
+ r"\bALTER\b",
16
+ r"\bCREATE\b",
17
+ r"\bGRANT\b",
18
+ r"\bREVOKE\b",
19
+ r"\bEXEC\b",
20
+ r"\bEXECUTE\b",
21
+ r"\bCALL\b",
22
+ r"\bSET\b",
23
+ r"\bCOPY\b",
24
+ r"\bUNLOAD\b",
25
+ r"\bVACUUM\b",
26
+ # SELECT ... INTO creates a table
27
+ r"\bINTO\b",
28
+ ]
29
+
30
+ # Functions that run SQL on another connection or from a string the check cannot see
31
+ # into, or that have effects outside the transaction. A quoted name ("pg_sleep") counts.
32
+ DANGEROUS_SQL_FUNCTIONS = [
33
+ "dblink",
34
+ "dblink_exec",
35
+ "dblink_send_query",
36
+ "pg_terminate_backend",
37
+ "pg_cancel_backend",
38
+ "pg_reload_conf",
39
+ "pg_read_file",
40
+ "pg_read_binary_file",
41
+ "lo_import",
42
+ "lo_export",
43
+ "set_config",
44
+ "setval",
45
+ "nextval",
46
+ "pg_sleep",
47
+ "query_to_xml",
48
+ "query_to_xml_and_xmlschema",
49
+ "query_to_xmlschema",
50
+ "ts_stat",
51
+ ]
52
+
53
+ ALLOWED_QUERY_PREFIXES = ("SELECT", "WITH", "EXPLAIN")
54
+
55
+ MAX_QUERY_ROWS = 1000
56
+ DEFAULT_QUERY_ROWS = 100
57
+
58
+ # PostgreSQL's whitespace. Other Unicode spaces are identifier characters to it.
59
+ _WHITESPACE = " \t\n\r\f\v"
60
+ _LINE_END = re.compile(r"[\r\n]")
61
+ _NUMBER = re.compile(r"(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?")
62
+ _DOLLAR_QUOTE = re.compile(r"\$(?:[A-Za-z_\u0080-\U0010ffff][A-Za-z0-9_\u0080-\U0010ffff]*)?\$")
63
+
64
+
65
+ class _Token(NamedTuple):
66
+ kind: str # word, number, symbol, string, identifier or comment
67
+ text: str # for an identifier, its name without the quotes
68
+ start: int
69
+ end: int
70
+
71
+
72
+ class _UnreadableQueryError(ValueError):
73
+ """The check cannot be sure where a literal or comment ends the way the server would."""
74
+
75
+
76
+ def _is_word_start(ch: str) -> bool:
77
+ # Like PostgreSQL: a letter, an underscore or any non-ASCII character
78
+ return ch == "_" or not ch.isascii() or ch.isalpha()
79
+
80
+
81
+ def _is_digit(ch: str) -> bool:
82
+ return ch.isascii() and ch.isdigit()
83
+
84
+
85
+ def _is_word_char(ch: str) -> bool:
86
+ return _is_word_start(ch) or _is_digit(ch) or ch == "$"
87
+
88
+
89
+ def _quote_end(sql: str, start: int, *, backslash_escapes: bool) -> int | None:
90
+ i = start + 1
91
+ while i < len(sql):
92
+ if backslash_escapes and sql[i] == "\\":
93
+ i += 2
94
+ elif sql[i] == "'":
95
+ if not sql.startswith("'", i + 1):
96
+ return i + 1
97
+ i += 2 # '' is a quote inside the literal
98
+ else:
99
+ i += 1
100
+ return None
101
+
102
+
103
+ def _string_end(sql: str, start: int) -> int:
104
+ # Whether a backslash escapes the next character depends on the server and its settings
105
+ # (standard_conforming_strings, E'' strings, Redshift). A literal whose end depends on
106
+ # that could hide code from the check, so it must end in the same place either way.
107
+ plain = _quote_end(sql, start, backslash_escapes=False)
108
+ escaped = _quote_end(sql, start, backslash_escapes=True)
109
+ if plain is None and escaped is None:
110
+ raise _UnreadableQueryError("Query has an unterminated string literal")
111
+ if plain != escaped:
112
+ raise _UnreadableQueryError("String literal with a backslash before a quote is ambiguous; escape quotes as ''")
113
+ return plain
114
+
115
+
116
+ def _literal_end(sql: str, start: int) -> int | None:
117
+ """End of the '...' or $tag$...$tag$ literal at ``start``, or None if none starts there."""
118
+ if sql[start] == "'":
119
+ return _string_end(sql, start)
120
+ dollar = _DOLLAR_QUOTE.match(sql, start)
121
+ if not dollar:
122
+ return None
123
+ close = sql.find(dollar.group(), dollar.end())
124
+ if close == -1:
125
+ raise _UnreadableQueryError("Query has an unterminated dollar-quoted string")
126
+ return close + len(dollar.group())
127
+
128
+
129
+ def _comment_end(sql: str, start: int) -> int | None:
130
+ """End of the -- or /* */ comment at ``start``, or None if none starts there."""
131
+ if sql.startswith("--", start):
132
+ line_end = _LINE_END.search(sql, start)
133
+ return line_end.start() if line_end else len(sql)
134
+ if not sql.startswith("/*", start):
135
+ return None
136
+ close = sql.find("*/", start + 2)
137
+ if close == -1:
138
+ raise _UnreadableQueryError("Query has an unterminated comment")
139
+ # PostgreSQL nests comments, other servers end at the first */
140
+ if "/*" in sql[start + 2 : close]:
141
+ raise _UnreadableQueryError("Nested comments are not allowed")
142
+ return close + 2
143
+
144
+
145
+ def _quoted_name_end(sql: str, start: int) -> int:
146
+ end = start + 1
147
+ while True:
148
+ end = sql.find('"', end)
149
+ if end == -1:
150
+ raise _UnreadableQueryError("Query has an unterminated quoted identifier")
151
+ if not sql.startswith('"', end + 1):
152
+ return end + 1
153
+ end += 2 # "" is a quote inside the name
154
+
155
+
156
+ def _word_end(sql: str, start: int) -> int:
157
+ end = start + 1
158
+ while end < len(sql) and _is_word_char(sql[end]):
159
+ end += 1
160
+ # U&"d\0061ta" spells a name with escapes the check would have to decode
161
+ if sql[start:end].upper() == "U" and sql.startswith('&"', end):
162
+ raise _UnreadableQueryError("Unicode-escaped identifiers are not allowed")
163
+ return end
164
+
165
+
166
+ def _tokenize(sql: str) -> list[_Token]:
167
+ """Split a query the way PostgreSQL's lexer does, as far as the safety check needs.
168
+
169
+ Raises:
170
+ _UnreadableQueryError: A literal, quoted name or comment is unterminated or could
171
+ be read differently by another server.
172
+ """
173
+ tokens: list[_Token] = []
174
+ i = 0
175
+ while i < len(sql):
176
+ ch = sql[i]
177
+ if ch in _WHITESPACE:
178
+ i += 1
179
+ continue
180
+ if (end := _comment_end(sql, i)) is not None:
181
+ token = _Token("comment", sql[i:end], i, end)
182
+ elif (end := _literal_end(sql, i)) is not None:
183
+ token = _Token("string", sql[i:end], i, end)
184
+ elif ch == '"':
185
+ end = _quoted_name_end(sql, i)
186
+ token = _Token("identifier", sql[i + 1 : end - 1].replace('""', '"'), i, end)
187
+ elif _is_digit(ch) or (ch == "." and _is_digit(sql[i + 1 : i + 2])):
188
+ # A number ends where a word starts: in "1INTO", INTO is a keyword
189
+ end = _NUMBER.match(sql, i).end()
190
+ token = _Token("number", sql[i:end], i, end)
191
+ elif _is_word_start(ch):
192
+ end = _word_end(sql, i)
193
+ token = _Token("word", sql[i:end], i, end)
194
+ else:
195
+ token = _Token("symbol", ch, i, i + 1)
196
+ tokens.append(token)
197
+ i = token.end
198
+ return tokens
199
+
200
+
201
+ def is_safe_query(sql: str) -> tuple[bool, str]:
202
+ """Check if a SQL query is safe to execute (read-only).
203
+
204
+ The query is split into tokens first, so keywords, function names and semicolons
205
+ inside string literals ('...', E'...', $$...$$), quoted names and comments do not count,
206
+ and nothing a literal could hide is missed. Queries the check cannot read the way
207
+ every supported server would are rejected.
208
+
209
+ Args:
210
+ sql: The SQL query to validate.
211
+
212
+ Returns:
213
+ Tuple of (is_safe, error_message).
214
+ """
215
+ try:
216
+ tokens = _tokenize(sql)
217
+ except _UnreadableQueryError as exc:
218
+ return False, str(exc)
219
+ code = [token for token in tokens if token.kind != "comment"]
220
+
221
+ if not code or code[0].kind != "word" or code[0].text.upper() not in ALLOWED_QUERY_PREFIXES:
222
+ return False, "Only SELECT, WITH, or EXPLAIN queries are allowed"
223
+
224
+ # Literals become '' and quoted names "", so 'please DELETE my account' is data
225
+ code_text = " ".join("''" if t.kind == "string" else '""' if t.kind == "identifier" else t.text for t in code)
226
+ for pattern in DANGEROUS_SQL_PATTERNS:
227
+ if re.search(pattern, code_text, re.IGNORECASE):
228
+ return False, "Query contains prohibited operation"
229
+
230
+ for current, following in pairwise(code):
231
+ is_call = current.kind in ("word", "identifier") and following.kind == "symbol" and following.text == "("
232
+ if is_call and current.text.lower() in DANGEROUS_SQL_FUNCTIONS:
233
+ return False, f"Query calls prohibited function {current.text.lower()}()"
234
+
235
+ # Check for multiple statements (SQL injection protection); a trailing ; is fine
236
+ while code and code[-1].kind == "symbol" and code[-1].text == ";":
237
+ code.pop()
238
+ if any(token.kind == "symbol" and token.text == ";" for token in code):
239
+ return False, "Multiple SQL statements are not allowed"
240
+
241
+ return True, ""
242
+
243
+
244
+ def apply_row_limit(sql: str, limit: int) -> str:
245
+ """Ensure query has a row limit applied.
246
+
247
+ Args:
248
+ sql: The SQL query.
249
+ limit: Maximum rows to return.
250
+
251
+ Returns:
252
+ SQL with LIMIT clause applied.
253
+ """
254
+ limit = min(limit, MAX_QUERY_ROWS)
255
+
256
+ try:
257
+ tokens = _tokenize(sql)
258
+ except _UnreadableQueryError:
259
+ # is_safe_query rejects these; a new line keeps a trailing comment from hiding the limit
260
+ return f"{sql.rstrip().rstrip(';')}\nLIMIT {limit}"
261
+ code = [token for token in tokens if token.kind != "comment"]
262
+
263
+ # LIMIT inside a literal ('%LIMIT%'), a quoted name or a comment is not a clause
264
+ if any(token.kind == "word" and token.text.upper() == "LIMIT" for token in code):
265
+ return sql
266
+
267
+ while code and code[-1].kind == "symbol" and code[-1].text == ";":
268
+ code.pop()
269
+ # Cut after the last token, so a trailing semicolon or -- comment cannot swallow the LIMIT
270
+ end = code[-1].end if code else 0
271
+ return f"{sql[:end]} LIMIT {limit}"
@@ -0,0 +1,48 @@
1
+ """Every connection nlp2sql opens asks the database for read-only transactions.
2
+
3
+ Redshift cannot run in CI, so this checks the connection both repositories request; the
4
+ PostgreSQL side is also tested against the docker database (test_readonly_execution_against_postgres.py).
5
+ """
6
+
7
+ from unittest.mock import MagicMock
8
+
9
+ import psycopg2
10
+ import pytest
11
+
12
+ from nlp2sql.adapters.postgres_repository import PostgreSQLRepository
13
+ from nlp2sql.adapters.redshift_adapter import RedshiftRepository
14
+
15
+ REPOSITORIES = [
16
+ pytest.param(PostgreSQLRepository, "postgresql://user:pass@localhost:5432/db", id="postgresql"),
17
+ pytest.param(RedshiftRepository, "redshift://user:pass@cluster.example.com:5439/dev", id="redshift"),
18
+ ]
19
+
20
+
21
+ @pytest.fixture
22
+ def connection(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
23
+ conn = MagicMock()
24
+ conn.cursor.return_value.fetchall.return_value = [{"id": 1}]
25
+ conn.cursor.return_value.description = [("id",)]
26
+ monkeypatch.setattr(psycopg2, "connect", MagicMock(return_value=conn))
27
+ return conn
28
+
29
+
30
+ @pytest.mark.parametrize("repository_class, url", REPOSITORIES)
31
+ def test_connections_are_read_only(repository_class, url, connection):
32
+ repository_class(url)._get_connection()
33
+
34
+ connection.set_session.assert_called_once_with(readonly=True)
35
+
36
+
37
+ @pytest.mark.asyncio
38
+ @pytest.mark.parametrize("repository_class, url", REPOSITORIES)
39
+ async def test_execute_query_runs_on_a_read_only_connection_and_closes_it(repository_class, url, connection):
40
+ repository = repository_class(url)
41
+ repository._initialized = True
42
+
43
+ result = await repository.execute_query("SELECT id FROM users")
44
+
45
+ assert result["results"] == [{"id": 1}]
46
+ connection.set_session.assert_called_once_with(readonly=True)
47
+ connection.close.assert_called_once()
48
+ connection.commit.assert_not_called()
@@ -0,0 +1,77 @@
1
+ """PostgreSQL itself refuses writes from nlp2sql, even SQL the safety check would miss.
2
+
3
+ The safety check is replaced by one that accepts everything, so only the database stands
4
+ between the query and a write. Before the read-only session, SELECT ... INTO made a table
5
+ (rolled back only because the connection closed without a commit), a COMMIT inside the
6
+ query kept the next statement's table, and nextval() moved a sequence for good.
7
+ """
8
+
9
+ import psycopg2
10
+ import pytest
11
+
12
+ import nlp2sql
13
+ from nlp2sql import ProviderConfig
14
+ from nlp2sql.adapters.postgres_repository import PostgreSQLRepository
15
+ from nlp2sql.exceptions import SchemaException
16
+
17
+ pytestmark = pytest.mark.integration
18
+
19
+ PROBE_TABLE = "nlp2sql_readonly_probe"
20
+
21
+ WRITES = {
22
+ "select_into": f"SELECT * INTO {PROBE_TABLE} FROM users",
23
+ # Ends in a SELECT, so the LIMIT that execute_query appends keeps it valid SQL
24
+ "commit_then_create": f"SELECT 1; COMMIT; CREATE TABLE {PROBE_TABLE} AS SELECT 1 AS id",
25
+ "nextval": "SELECT nextval('users_id_seq')",
26
+ }
27
+
28
+
29
+ @pytest.fixture
30
+ def check_misses_everything(monkeypatch: pytest.MonkeyPatch) -> None:
31
+ def accept(sql: str) -> tuple[bool, str]:
32
+ return True, ""
33
+
34
+ monkeypatch.setattr("nlp2sql.adapters.postgres_repository.is_safe_query", accept)
35
+ monkeypatch.setattr("nlp2sql.services.query_service.is_safe_query", accept)
36
+
37
+
38
+ def _database_state(url: str) -> tuple[bool, int]:
39
+ """Whether the probe table exists, and the users id sequence position."""
40
+ with psycopg2.connect(url) as conn, conn.cursor() as cursor:
41
+ cursor.execute("SELECT to_regclass(%s) IS NOT NULL", (f"public.{PROBE_TABLE}",))
42
+ table_exists = cursor.fetchone()[0]
43
+ cursor.execute("SELECT last_value FROM users_id_seq")
44
+ return table_exists, cursor.fetchone()[0]
45
+
46
+
47
+ @pytest.mark.asyncio
48
+ @pytest.mark.parametrize("sql", WRITES.values(), ids=WRITES.keys())
49
+ async def test_execute_query_cannot_write(sql, postgres_available, check_misses_everything):
50
+ repository = PostgreSQLRepository(postgres_available)
51
+ before = _database_state(postgres_available)
52
+
53
+ with pytest.raises(SchemaException, match="read-only transaction"):
54
+ await repository.execute_query(sql)
55
+
56
+ assert _database_state(postgres_available) == before == (False, before[1])
57
+
58
+
59
+ @pytest.mark.asyncio
60
+ async def test_ask_cannot_write(postgres_available, check_misses_everything, fake_openai, isolated_caches):
61
+ nlp = await nlp2sql.connect(postgres_available, provider=ProviderConfig(provider="openai", api_key="sk-test"))
62
+ fake_openai.sql = WRITES["commit_then_create"]
63
+ before = _database_state(postgres_available)
64
+
65
+ result = await nlp.ask("Show me the users", validate=True)
66
+
67
+ assert result.is_valid is False
68
+ assert "read-only transaction" in result.metadata["execution_validation"]["error"]
69
+ assert _database_state(postgres_available) == before
70
+
71
+
72
+ @pytest.mark.asyncio
73
+ async def test_statement_timeout_still_applies(postgres_available, check_misses_everything):
74
+ repository = PostgreSQLRepository(postgres_available)
75
+
76
+ with pytest.raises(SchemaException, match="statement timeout"):
77
+ await repository.execute_query("SELECT pg_sleep(5)", timeout_seconds=1)