Skip to content
Advertisement

How to prevent MySql Injection in dynamic where?

I have an API in my C# WEBApi project which return some items from my MySQL DB.

The data to return has a visibility set in a table and the params set in the API call says which kind of data should be shown, like if i’m in TAKEAWAY route of my website i send to API “TAKEAWAY” and i should SELECT data only where flag TAKEAWAY is on.

So i have my method where i get the data but i think it’s suggested to MySQL injection as of string concat in the query, so based on type i’m setting my where like this:

And setting it in query like this:

And then i execute that query as the following:

But how can i prevent to concat the string in the query for dynamic where?

Advertisement

Answer

Your code looks safe. Although you are dynamically generating part of the query string, the switch statement on application side ensures that no malicious value can go through.

You could, however, move the logic to the query itself, which would avoid the neeed to dynamically build the query.

@type represents a query parameter, that you can pass to the query from your application.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement