00:00

#0302

Search Query Builder

Easy+50 XPA03:2021 InjectionCWE-89
SQL InjectionSearch

Scenario

A blog platform exposes a search endpoint that accepts a free-text searchTerm from the user and constructs a SQL LIKE query by concatenating the term directly into the string.

An attacker can break out of the LIKE pattern with a single quote and then execute arbitrary SQL — extracting password hashes, dumping tables, or bypassing authorization.

This is a common variant of SQL injection where the injection point is inside a LIKE clause rather than a simple equality check.

Search fields are a frequent injection entry point because developers often forget that LIKE patterns are still SQL. A UNION-based injection here can silently dump any table in the database.

Your Tasks

  1. Fix buildSearchQuery so the search term is passed as a parameter.
  2. The wildcard wrapping (%term%) must happen in the params array, not inline in the SQL.
  3. Return { sql: "SELECT id,title FROM posts WHERE title LIKE $1", params: ["%" + searchTerm + "%"] }.

Examples

Example 1UNION injection blocked

buildSearchQuery("foo' UNION SELECT password,null FROM users--")
// FIX → { sql: '...LIKE $1', params: ["%foo' UNION SELECT...%"] }

Example 2Normal search

buildSearchQuery('hello')
// → { sql: '...LIKE $1', params: ['%hello%'] }

Constraints

  • Only edit the function body — do not change the function signature.
  • The returned sql must use $1 as the placeholder.
  • The params array must contain exactly one element: "%" + searchTerm + "%".
  • No external packages.

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run