djangodjango-rest-frameworksnowflake-cloud-data-platformsql-injection

how to prevent sql injection in snowflake filter function


Im using input from the user to query snowflake within DRF view, how to prevent in the below code sql injection possibility?

entity_id = kwargs['pk']
table = session.table("my_table").filter(col(ID_COL)==entity_id )

Solution

  • The good news is that the user input should be already filtered if you are using filter().

    Testing with Snowpark:

    import snowflake.snowpark as snowpark
    from snowflake.snowpark.functions import col
    
    def main(session: snowpark.Session): 
        # Your code goes here, inside the "main" handler.
        tableName = 'information_schema.packages'
        dataframe = session.table(tableName).filter(col("language") == "';drop table bobby_tables")
    
        # Print a sample of the dataframe to standard output.
        dataframe.show()
    
        # Return value will appear in the Results tab.
        return dataframe
    

    If you go check the Snowflake logs, you'll find that Snowflake ran the following query, with the quote escaped:

    SELECT  *  
    FROM information_schema.packages 
    WHERE ("LANGUAGE" = ''';drop table bobby_tables')