0

Update: See also this preexisting question, and its answers, of which this question is effectively a duplicate.


I'm using a 3rd-party API that takes a WHERE condition fragment of a SQL Statement

e.g. ThirdPartyFunction(where:"Category = 'abc'", top:5)

I have strings passed through a UI or web service and need to prevent against SQL injection attack.

Without the use of parameterized queries, and without an alternate method in the .NET framework to escape SQL strings (that I know of), I expect to manually escape the SQL string.

I have ideas about the best way to write an escape method but am looking for THE most secure solution.

5

1 Answer 1

1

Parameterized query would definitely be the safest, however you could do a replace on any single quotes, with two single quotes. So if the user tries to enter malicious query within the 'abc' portion, it would handle it as a string.

What third party library are you using? Have you checked to ensure that they do not provide ability to parameterize your calls to their methods also?

Sign up to request clarification or add additional context in comments.

3 Comments

I've sifted through the third party library and might post a separate question for it, instead of naming it here. In general I'd like a definitive answer to this generic question because the scenario can crop up in other programming situation.
@John K: I'd say the definitive answer is to see if the library itself already does the checks. If it doesn't then the only secure answer is to dump the library as it likely has many other issues
Good call Chris - to inspect the actual SQL calls. Dumping such a library is ideal from a development perspective, however when tools are selected by the customer I need to use them and find a programmatic solution to some of these oddities. There might be other kinds of methods, safer options, in the API that have the same end result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.