Home / Learn Secure SQL / Project: Secure Search Feature

Project: Secure Search Feature

Build a product search box without string-built queries.

Quick summaryBuild a product search box without string-built queries.

How to think about this

Search query constructionuser keywordwildcard/escapingapplied in app codebound as oneparameter valuequery executed,results limited
Search query construction

The basic idea

A search feature: users type a keyword, and the app returns matching products, built entirely with bound parameters and a bounded LIKE pattern, never by inserting the keyword into the SQL text.

Why this comes up

Real projects are where secure patterns become habits instead of theory.

The mechanics

Work through the steps in order; do not skip the checklist.

Where it is used

  • Portfolio project
  • Code review practice
  • Interview preparation

What you will build

A search feature: users type a keyword, and the app returns matching products, built entirely with bound parameters and a bounded LIKE pattern, never by inserting the keyword into the SQL text.

LevelTime needed
Beginner2-3 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
Code editor (VS Code or similar)The program you write code in.You need somewhere to write and run the example app.free
A language runtime (Node.js, Python, PHP or similar)Installs the interpreter so your code can run on your computer.The project examples run as small scripts.free
SQLite (bundled, no install needed)A tiny file-based database engine.Every project in this course can be tried with SQLite first, with zero setup.free
GitVersion control: tracks every change you make.Lets you compare the insecure version and the fixed version of the code.free

Step 1: see the problem and the fix in the sandbox

A bound LIKE search, with the wildcard built outside the SQL textSQL: edit and run

The %Widget% pattern here stands in for a bound parameter whose value is built from the user's keyword plus wildcard characters in application code, never by formatting the keyword directly into the SQL text.

Step by step

  1. Create a products table and seed it with sample rows.
  2. Write the search query using a bound LIKE parameter, building the %keyword% pattern in application code before binding it.
  3. Add an ESCAPE clause and escape any literal % or _ characters in the user's keyword, so a search for a literal percent sign works correctly.
  4. Add LIMIT and pagination so a very broad search cannot return the entire table at once.
  5. Write a test that searches for a keyword containing a literal % and confirms it matches only literal percent signs, not a wildcard.

Security checklist before you ship it

CheckWhy it mattersHow to verify
Bound LIKE parameterPrevents unsafe string-built queriesReview the query-building code for any direct string formatting
Wildcard escapingA literal % or _ in user input should not act as a wildcardTest a search term containing a literal % character
Result limitingPrevents one query from returning unbounded rowsConfirm LIMIT and pagination are present
Tip:Commit the insecure version and the fixed version as two separate commits, so the diff itself becomes a teaching example.

Quick check

1. Where should the %...% wildcard be added to a search keyword?