Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL block potential harmful commands #8583

Closed

Conversation

Uranium2
Copy link

@Uranium2 Uranium2 commented Aug 1, 2023

Description:
This PR aims to give a bit more control on the commands executed by the SQLDatabase. At the moment, if you ask "Drop the table X in database Y" with SQLDatabaseChain, it will generate code to drop the table that will be run by SQLDatabase. Today no checks are made on the commands in SQLDatabase, it's free real estate.

Ideally, rights on user must be set to perform or not this kind of action on the database. In real life, lot of people use full rights. To prevent this I added a feature that could, if provided a list of restricted actions, stop the execution of the SQL query.

It will raise a PermissionError when restricted keywords are in the command for SQLDatabase

Key Changes:

  • Add a new argument to SQLDatabase class named restricted_keywords
  • Add a new method to detect restricted keywords in the SQL command
  • Raise PermissionError if keywords are found in the sql command
  • Execute sql command like before if not keyworkds were found
  • Add a test to check if the raise is working while adding one restricted keyword

make format, make lint and make test were tested locally

I think this could help for CVE-2023-36189 Where the developer could block any keywords that could be generated by SQLDatabaseChain. It can block any string, so he could also block the usage of wildcards, name of tables, databases. I think it can be powerfull, it gives control to the developer and protect the user doing unwanted actions or protect his database.

@baskaryan

Raise a `PermissionError` when a provided SQL keyword is provided to `SQLDatabase`.
add detect_harmful_actions
Add test_sql_harmful_keywords
@vercel
Copy link

vercel bot commented Aug 1, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Feb 18, 2024 6:53pm
langchain-deprecated ⬜️ Ignored (Inspect) Feb 18, 2024 6:53pm

@dosubot dosubot bot added the 🤖:improvement Medium size change to existing code to handle new use-cases label Aug 1, 2023
user is a table not a database
More representative of the real usage of the feature.
ran make lint and pass my files for the feature, but other files are in error
Fix line too long
@Uranium2 Uranium2 requested a review from baskaryan August 3, 2023 08:56
@leo-gan
Copy link
Collaborator

leo-gan commented Sep 20, 2023

@baskaryan Could you, please, review it?

@hwchase17 hwchase17 closed this Jan 30, 2024
@baskaryan baskaryan reopened this Jan 30, 2024
@baskaryan
Copy link
Collaborator

Apologies for the slow review! Pr has a number of merge conflicts, happy to re-review if you'd like to resolve

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 18, 2024
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Feb 18, 2024
Copy link
Collaborator

@eyurtsev eyurtsev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same logic as here: #22780 (review)

@eyurtsev eyurtsev closed this Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants