← All articlesWeb security

How to pass a search string to SQL as a parameter and verify the result

A search string must remain a value, even when it contains an apostrophe, parentheses or a semicolon. SQL injection occurs when untrusted data becomes part of SQL syntax. We will examine one specific task: finding an exact match in a text field. Developers can verify how the parameter reaches the database call, while product owners can compare the results against known records. This requires both code review and observations using your own synthetic data.

The boundary between the query and its data

A search has two separate components: the SQL structure and the value entered by the user. The structure determines which operation to perform, while the search string supplies the data to compare. This boundary breaks down if the application adds an untrusted value directly to the SQL text. You therefore need to examine the string’s entire path, from its entry into the server-side handler to the database call.

A prepared statement preserves this separation: the structure is defined separately, and the value is bound through the database API. A bound parameter is treated as data rather than SQL syntax. A call to prepare a statement is not enough on its own: if the search string has already been concatenated into the SQL, preparing it afterward does not provide this protection.

A fictional Java search example

Imagine a completely fictional catalog in your own application. A synthetic table named demo_items has id and title fields. The operation under review searches only for an exact title match. It uses the predefined template SELECT id, title FROM demo_items WHERE title = ?. The ? placeholder represents a separate text value.

In this JDBC example, the application prepares that fixed template, passes the searchText string through setString(1, searchText), and executes the prepared statement. Index 1 refers to the first parameter. There is no need to insert the title into the SQL yourself: for example, the apostrophe in O'Brien must remain part of the supplied value. This example covers one text parameter, not every method of constructing queries.

How to verify that the parameter is actually bound

The following plan is a defensive check proposed by the author, not an official procedure from the documentation. Open the handler for the selected search and trace searchText to the database call. Record the text passed for preparation and the call used to pass the value itself. Check intermediate functions: the untrusted string must never enter the SQL through concatenation, interpolation or prior text assembly.

The acceptance criterion for our example is specific: the template remains SELECT id, title FROM demo_items WHERE title = ?, and the original string is passed separately through setString(1, searchText). Also check how the library you use operates. Some client libraries merely assemble a string before sending it; the documentation requires verifying that parameterization takes place on the server side. A method name or an API that looks similar is not sufficient.

Expected results using your own data

Continue the author’s check in the same fictional catalog. Create five synthetic records with distinct, known id values. Set their titles exactly to: “Блокнот”, “Блокнот O'Brien”, “Набор (синий)”, “Кабель; короткий” and “Чехол — 100%”. Use only your own test environment and records.

Pass each full title through the operation under review, one at a time. Each search should return exactly the corresponding record with its known id, with no additional records. “Блокнот” serves as an ordinary positive control. The other titles let you observe whether the apostrophe and other ordinary text characters remain intact in the value. Compare the actual records, not just a successful HTTP response.

Then search for the absent title “Несуществующий блокнот O'Brien”: the expected result is empty. Afterward, repeat the search for “Блокнот” and verify the known id again. This checks data retrieval; a success message alone is not enough to assess whether the search is correct.

What to fix first

If you find SQL being assembled with the search string, the first fix should be to separate the template from the value. Remove searchText from the query text and pass it through the parameter-binding API. Then trace the value again and compare the results using the same synthetic records.

Checking a value’s type, length or format does not make it safe to concatenate into SQL. Manual escaping should not replace parameter binding either. Successful searches after those measures are therefore not grounds for accepting the implementation if the untrusted value is still included in the SQL text.

What the check confirms and where its limits lie

Accept the selected operation based on both results together: the code actually binds the text parameter separately, and the search returns the expected synthetic records. The absence of errors and successful responses do not, by themselves, prove parameterization. These observations also provide no statistical guarantees about the security of the entire product.

This discussion is limited to one text value in an exact-match search. Dynamic table and column names, sorting and other mechanisms require separate consideration. If you would like to review the search implementation in your product, you can start with a free audit of your website or the web portion of your application.

Sources and documentation

  1. SQL Injection Prevention Cheat Sheet
  2. Query Parameterization Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free