Thuta Learning
SQL
AdvancedData & Databasesအခြေခံ

SQL Security & Parameterized Queries

SQLသင်ခန်းစာ 2635 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • String concatenation က input ကို SQL code အဖြစ် ဘယ်လို ပြောင်းသွားစေလဲ ဥပမာနဲ့ ပြနိုင်ဖို့
  • PostgreSQL ရဲ့ $1, $2 placeholder တွေနဲ့ parameterized query ရေးပြီး driver အလိုက် placeholder ပုံစံကွဲပုံကို သိဖို့
  • ORDER BY column လို identifier တွေကို parameter မလုပ်နိုင်လို့ allow-list နဲ့ ကိုင်တွယ်နိုင်ဖို့
  • LIKE wildcard တွေကို escape လုပ်ပြီး app role ကို လိုအပ်တဲ့ GRANT တွေပဲ ပေးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

SQL injection ကို ရိုးရိုးလေး ပြောရရင်

Database က SQL string တစ်ခုလုံးကို command အဖြစ် ဖတ်ပါတယ်။ User ရိုက်ထည့်တဲ့ စာကို အဲဒီ string ထဲ ဆက်ရေးလိုက်ရင် user က command ကို ကိုယ်တိုင် ပြင်ရေးနိုင်သွားပါတယ် — ဒါကို SQL injection လို့ ခေါ်ပါတယ်။ Parameterized query ကတော့ SQL code ကို တစ်ခါ၊ value တွေကို သီးခြား ပို့လို့ value က ဘယ်လိုပဲ ရေးထားထား value အဖြစ်ပဲ ကျန်ပါတယ်။

ဘဏ်ကောင်တာက ငွေထုတ်လွှာကို စဉ်းစားကြည့်ပါ။ ဖောင်မှာ 'ပမာဏ' ကွက်လပ် သတ်သတ် ရှိလို့ အဲဒီထဲ 'နောက်ပြီး account အကုန်ပိတ်ပါ' လို့ ရေးလည်း ငွေပမာဏ မမှန်ဘူးလို့ပဲ ငြင်းပါမယ်၊ ညွှန်ကြားချက်အဖြစ် မလုပ်ပါဘူး။ Placeholder ဆိုတာ အဲဒီ ကွက်လပ်ပါပဲ။

Problem → Why → Solution

ပြဿနာ: login query ထဲ input ဆက်ရေးခြင်း

Login endpoint က email ကို form ကနေ ယူပြီး quote နှစ်ခုကြားထဲ ဆက်ရေးကာ user ကို ရှာပါတယ်။ ပုံမှန် email တွေနဲ့ စမ်းတဲ့အခါ ကောင်းကောင်း အလုပ်လုပ်ပါတယ်။ တစ်ယောက်ယောက်က email ကွက်ထဲမှာ x' OR '1'='1 လို့ ရိုက်လိုက်တော့ quote က string ကို အစောကြီး ပိတ်ပြီး OR condition အသစ်က အမြဲ true ဖြစ်လို့ user table တစ်ခုလုံး ပြန်လာပါတယ် — ပထမဆုံး row က admin ဖြစ်နေတတ်ပါတယ်။

login-unsafe.js
// email comes straight from the request body
const sql = "SELECT id, email FROM users WHERE email = '" + email + "'";
const result = await db.query(sql);

// email = "x' OR '1'='1"  produces:
// SELECT id, email FROM users WHERE email = 'x' OR '1'='1'

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

Concatenation ပြီးတဲ့အခါ database ဆီ ရောက်တာက string တစ်ခုတည်းပါ၊ ဘယ်အပိုင်းက developer ရေးတာလဲ ဘယ်အပိုင်းက user ပို့တာလဲ ခွဲလို့ မရတော့ပါဘူး။ Quote ကို escape လုပ်တာနဲ့ ဖာထေးဖို့ ကြိုးစားလည်း number column (quote မပါ) ဒါမှမဟုတ် encoding ထူးခြားချက်တွေမှာ ပေါက်ကြားတတ်ပါတယ်။ အမြစ်ကနေ ဖြေရှင်းတာက code နဲ့ data ကို တစ်ခါမှ မရောဘဲ သီးခြား channel နှစ်ခုနဲ့ ပို့တာပါ။

Core concept: code နဲ့ value ကို ခွဲပို့ခြင်း

Parameterized query မှာ SQL text ထဲ value နေရာကို placeholder နဲ့ ရေးပြီး value တွေကို array တစ်ခုအနေနဲ့ သီးခြား ပေးပါတယ်။ PostgreSQL ရဲ့ wire protocol မှာ ဒါကို Parse (SQL text ကို parse လုပ်)၊ Bind (value တွေ ချိတ်)၊ Execute ဆိုပြီး အဆင့်ခွဲလုပ်ပြီး prepared statement တစ်ခုကို value အမျိုးမျိုးနဲ့ အကြိမ်ကြိမ် bind/execute လုပ်လို့ ရပါတယ်။ SQL ရဲ့ ပုံစံကို value မရောက်ခင်ကတည်းက ဆုံးဖြတ်ပြီးသားမို့ value ထဲမှာ quote, semicolon, OR ဘာပါပါ code အဖြစ် ဘယ်တော့မှ မဖတ်ပါဘူး။

DriverPlaceholder ပုံစံ
node-postgres, PGlite, PREPARE$1, $2, ...
psycopg (Python)%s ဒါမှမဟုတ် %(name)s
JDBC PreparedStatement (Java)?
PHP PDO? ဒါမှမဟုတ် :name

psycopg ရဲ့ %s က Python ရဲ့ % string formatting နဲ့ ပုံချင်းတူပေမဲ့ လုံးဝ မတူပါဘူး: cursor.execute(sql, (email,)) လို့ value ကို argument အဖြစ် ပေးရပြီး sql % email လို့ ကိုယ်တိုင် format လုပ်ရင် concatenation နဲ့ အတူတူ ဖြစ်သွားပါတယ်။ :email ပုံစံကလည်း PostgreSQL server ကိုယ်တိုင် နားမလည်ပါဘူး — psql variable syntax ဒါမှမဟုတ် driver/ORM တချို့ရဲ့ named parameter ပုံစံပါ။

Parameter က value တွေအတွက်ပဲ ဖြစ်ပါတယ်။ Table နာမည်၊ column နာမည်၊ ASC/DESC လို keyword တွေက SQL ရဲ့ ပုံစံ (structure) ဖြစ်လို့ placeholder နဲ့ အစားထိုးလို့ မရပါဘူး။ အဲဒါတွေကို user ဆီက လက်ခံရရင် ခွင့်ပြုထားတဲ့ စာရင်း (allow-list) ထဲက ရွေးခိုင်းပြီး SQL fragment ကို ကိုယ်တိုင် ထိန်းချုပ်ရပါမယ်။ နောက်ဆုံး ကာကွယ်ရေးလိုင်းကတော့ least privilege ပါ: app ရဲ့ database role က လိုအပ်တဲ့ table/column ပေါ်မှာ လိုအပ်တဲ့ permission တွေပဲ ရှိရင် injection ဖြစ်ခဲ့ရင်တောင် ပျက်စီးမှုက ကန့်သတ်ခံရပါတယ်။ Parameter, allow-list နဲ့ ကျဉ်းကျဉ်း GRANT တို့က အလွှာတွေလို အလုပ်လုပ်ပါတယ်: တစ်ခုက လွတ်သွားတာကို နောက်တစ်ခုက ဖမ်းပေးလို့ ရင့်ကျက်တဲ့ codebase တွေက သုံးခုလုံးကို သုံးပါတယ်။

Query တစ်ခု database ဆီ ရောက်ပုံ
  1. SQL text ကို placeholder နဲ့ ရေး
  2. Database က text ကို parse လုပ်
  3. Value တွေကို ကွက်လပ်ထဲ bind လုပ်
  4. Value က code အဖြစ် ဘယ်တော့မှ မပြောင်း

Identifier တွေက parse အဆင့်မှာ လိုအပ်လို့ bind အဆင့်ကို စောင့်လို့ မရပါဘူး၊ ဒါကြောင့် allow-list လိုတာပါ။

အထွေထွေ ပုံစံ

javascript
// 1. SQL text with numbered placeholders (never user data)
const sql = "SELECT columns FROM table_name WHERE column_a = $1 AND column_b = $2";

// 2. values travel separately, in placeholder order
const result = await db.query(sql, [valueA, valueB]);

// 3. identifiers come from a fixed allow-list, never from input
const orderBy = ALLOWED_SORTS.get(userChoice);

SQL text ကို string literal အဖြစ် ကိုယ်တိုင်ပဲ ရေးပြီး input က array ထဲကိုပဲ ရောက်ရပါမယ်။ Identifier လိုအပ်ရင် input ကို key အဖြစ်သုံးပြီး ကိုယ်ပိုင် map ကနေ SQL fragment ကို ယူပါ။

Worked example: concatenation နဲ့ parameter ကို ယှဉ်ကြည့်ခြင်း

အောက်က Node.js script က PGlite (WASM ပေါ်က PostgreSQL) ထဲမှာ user နှစ်ယောက်ပါတဲ့ table ကို ဆောက်ပြီး တိုက်ခိုက်တဲ့ input တစ်ခုတည်းကို နည်းနှစ်မျိုးနဲ့ ပို့ကြည့်ပါတယ်။ Concatenation က row အားလုံး ပြန်ပေးပြီး parameter က row သုညကို ပြန်ပေးပါတယ်၊ email လုံးဝ ကိုက်ညီတဲ့ user မရှိလို့ပါ။ node-postgres ရဲ့ client.query(text, values) ကလည်း ပုံစံ အတူတူပါပဲ။

injection-demo.mjs
import { PGlite } from "@electric-sql/pglite";

const db = new PGlite();
await db.exec(`
  CREATE TABLE users (id integer PRIMARY KEY, email text NOT NULL);
  INSERT INTO users VALUES (1, 'ada@example.com'), (2, 'lin@example.com');
`);

const typed = "nobody@example.com' OR '1'='1";

// UNSAFE: the input is glued into the SQL text
const glued = "SELECT id, email FROM users WHERE email = '" + typed + "'";
console.log(glued);
const unsafe = await db.query(glued);
console.log("concatenated:", unsafe.rows.length, "rows");

// SAFE: the SQL text and the value travel separately
const sql = "SELECT id, email FROM users WHERE email = $1";
const safe = await db.query(sql, [typed]);
console.log("parameterised:", safe.rows.length, "rows");

const normal = await db.query(sql, ["lin@example.com"]);
console.log(normal.rows);

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. The attack stringconst typed = "nobody@example.com' OR '1'='1";

    ပထမ quote က developer ရဲ့ string ကို ပိတ်ပြီး ကျန်တဲ့အပိုင်းက အမြဲ true ဖြစ်တဲ့ condition အသစ် ထည့်ပါတယ်။

  2. Concatenationconst glued = "SELECT id, email FROM users WHERE email = '" + typed + "'";

    Print ထုတ်လိုက်တဲ့ SQL ကို ကြည့်ပါ — input က WHERE clause ရဲ့ logic ကို ပြောင်းသွားပြီး row နှစ်ကြောင်းလုံး ကိုက်ညီသွားပါတယ်။

  3. Placeholderconst sql = "SELECT id, email FROM users WHERE email = $1";

    $1 က ပထမ value ရဲ့ နေရာပါ။ SQL text ထဲမှာ user data လုံးဝ မပါပါဘူး။

  4. Values travel separatelyconst safe = await db.query(sql, [typed]);

    Attack string တစ်ခုလုံးကို email value အဖြစ်ပဲ နှိုင်းယှဉ်လို့ ကိုက်ညီတဲ့ row မရှိပါဘူး။ ပုံမှန် email နဲ့ ခေါ်ရင်တော့ မှန်ကန်တဲ့ row တစ်ကြောင်း ရပါတယ်။

You should see
SELECT id, email FROM users WHERE email = 'nobody@example.com' OR '1'='1'
concatenated: 2 rows
parameterised: 0 rows
[ { id: 2, email: 'lin@example.com' } ]

SQL ထဲမှာပဲ parameter သုံးခြင်း: PREPARE နဲ့ EXECUTE

PostgreSQL မှာ driver မလိုဘဲ SQL ထဲမှာတင် prepared statement ကို ဆောက်လို့ ရပါတယ်။ PREPARE က parameter type နဲ့ statement ကို တစ်ကြိမ် parse လုပ်ပြီး EXECUTE က value တွေကို ပေးပါတယ်။ ဒုတိယ EXECUTE မှာ attack string ကို ပို့ထားပေမဲ့ email တစ်ခုအဖြစ်ပဲ နှိုင်းယှဉ်လို့ row သုည ပြန်လာပါတယ်။ PL/pgSQL function ထဲက dynamic SQL မှာလည်း EXECUTE ... USING နဲ့ value ကို ပေးပြီး identifier ကို format('%I', name) နဲ့ quote လုပ်ရပါတယ်။

prepare-execute.sql
PREPARE find_user (text) AS
    SELECT id, email FROM users WHERE email = $1;

EXECUTE find_user('lin@example.com');                     -- 1 row
EXECUTE find_user('nobody@example.com'' OR ''1''=''1');   -- 0 rows

Least privilege ကို SQL ထဲမှာ ဒီလို ပြင်ဆင်ပါတယ်: report အတွက် role တစ်ခု ဆောက်ပြီး users table ရဲ့ id နဲ့ email column ကိုပဲ SELECT ခွင့်ပေးပါတယ်။ အဲဒီ role အနေနဲ့ is_admin ကို ဖတ်ဖို့ ကြိုးစားရင် permission denied for table users နဲ့ fail ပါတယ်၊ ဒါကြောင့် injection တစ်ခု ပေါက်သွားရင်တောင် password hash ဒါမှမဟုတ် admin flag ကို ဖတ်လို့ မရပါဘူး။

least-privilege.sql
CREATE ROLE report_reader NOLOGIN;
GRANT SELECT (id, email) ON users TO report_reader;

SET ROLE report_reader;
SELECT id, email FROM users ORDER BY id;  -- allowed
-- SELECT is_admin FROM users;            -- permission denied for table users
RESET ROLE;

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ SQL security

SQL injection က OWASP Top 10 ရဲ့ 'Injection' အမျိုးအစားထဲမှာ နှစ်ပေါင်းများစွာ ပါနေခဲ့ပြီး data ပေါက်ကြားမှု အများကြီးရဲ့ အစ ဖြစ်ခဲ့ပါတယ်။ ORM တွေ (Django ORM, Prisma, SQLAlchemy) က ပုံမှန် query တွေမှာ parameter ကို အလိုအလျောက် သုံးပေးပေမဲ့ raw SQL escape hatch တွေ (raw(), $queryRawUnsafe, text() ကို f-string နဲ့) မှာ developer က ကိုယ်တိုင် concatenation လုပ်မိရင် ပြန်ပေါက်ပါတယ်။

  • Code review checklist တွေမှာ SQL string ထဲ + ဒါမှမဟုတ် template literal နဲ့ variable ထည့်ထားတာကို ရှာပါတယ်၊ Semgrep လို static analysis tool တွေကလည်း ဒီ pattern ကို အလိုအလျောက် ဖမ်းနိုင်ပါတယ်။
  • Sortable table, filter UI တွေက column နာမည်ကို query string ကနေ ယူလေ့ ရှိလို့ backend မှာ allow-list map နဲ့ စစ်ပြီး မသိတဲ့ value ကို 400 error နဲ့ ငြင်းပါတယ်။
  • Production မှာ app အတွက် role တစ်ခု၊ migration အတွက် table owner role တစ်ခု ခွဲထားလို့ app role က DROP TABLE ဒါမှမဟုတ် ALTER TABLE ကို လုပ်ခွင့် မရှိပါဘူး။

အမှားများ

SQL security မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

Column နာမည်ကို placeholder နဲ့ ပေးဖို့ ကြိုးစားခြင်း

ORDER BY $1 က column နာမည်ကို မဟုတ်ဘဲ constant string value တစ်ခုအဖြစ် ယူလို့ ဘာမှ sort မဖြစ်ပါဘူး။ ဒါကြောင့် developer တွေ concatenation ကို ပြန်သုံးမိတတ်ပါတယ်။ အဖြေက allow-list map ပါ၊ input ကို key အဖြစ်သာ သုံးပါ။

Plain object ကို allow-list အဖြစ် သုံးခြင်း

JavaScript မှာ SORTABLE[input] လို plain object lookup က 'constructor' လို input အတွက် function ကို ပြန်ပေးပြီး truthy ဖြစ်နေပါတယ်။ Map ဒါမှမဟုတ် Object.hasOwn() ကို သုံးမှသာ ကိုယ်တိုင်ထည့်ထားတဲ့ key တွေပဲ ကိုက်ညီပါတယ်။

LIKE ထဲ ထည့်တဲ့ parameter မှာ wildcard ကို မေ့ခြင်း

Parameter က injection ကို တားပေမဲ့ % နဲ့ _ က LIKE အတွက် wildcard အဖြစ် ဆက်အလုပ်လုပ်ပါတယ်။ User က % တစ်လုံးတည်း ရှာရင် row အားလုံး ပြန်လာပြီး table ကြီးတွေမှာ နှေးတဲ့ scan ဖြစ်စေပါတယ်။ Value ကို ပို့ခင် \, %, _ တွေကို escape လုပ်ပါ။

Mini challenge

Mini challenge: လုံခြုံတဲ့ user search

Admin page အတွက် searchUsers(db, emailContains, sortBy) function ကို ရေးပါ။ emailContains က ရှာမယ့် စာသားဖြစ်ပြီး sortBy က 'id', 'email', 'newest' သုံးခုထဲက တစ်ခု ဖြစ်ရပါမယ်။ Attack input တွေကိုလည်း စမ်းပြီး ရလဒ်ကို print ထုတ်ပါ။

  • Search စာသားကို $1 parameter နဲ့ ILIKE ထဲ ပို့ပါ
  • \, %, _ ကို escape လုပ်ပြီး literal စာလုံးအဖြစ် ရှာပါ
  • sortBy ကို Map allow-list နဲ့ SQL fragment အဖြစ် ပြောင်းပြီး မသိတဲ့ value ကို error နဲ့ ငြင်းပါ
  • 'max_', "' OR '1'='1" နဲ့ 'email; DROP TABLE users' တို့နဲ့ စမ်းပါ
search-users.mjs
import { PGlite } from "@electric-sql/pglite";

const db = new PGlite();
// TODO: create users (id, email, created_on) and insert four rows,
//       including 'max_power@example.com' and 'maxpower@example.com'

// TODO: a Map from the allowed sort names to fixed ORDER BY fragments
const SORTABLE = new Map();

function escapeLike(text) {
  // TODO: put a backslash before every \, % and _
}

async function searchUsers(db, emailContains, sortBy) {
  // TODO: look up sortBy; throw for anything not in the Map
  // TODO: query with ILIKE $1 and the escaped, %-wrapped search text
  // TODO: return just the email strings
}

// TODO: call searchUsers with normal and hostile inputs and print the results

ရလဒ်: 'newest' search က email လေးခုကို created_on အသစ်ဆုံးကနေ ပြပါတယ်။ 'max_' က max_power@example.com တစ်ခုတည်းကို ပြန်ပေးပြီး (escape မလုပ်ရင် maxpower ပါ ပါလာပါမယ်)၊ attack string က [] ကို ပြန်ပေးကာ DROP TABLE ပါတဲ့ sortBy ကို rejected လို့ print ထုတ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

text.replace(/[\\%_]/g, (ch) => "\\" + ch) က စာလုံးတစ်လုံးချင်းစီ ရှေ့မှာ backslash ထည့်ပါတယ်၊ PostgreSQL ရဲ့ LIKE က backslash ကို default escape character အဖြစ် သုံးပါတယ်။ Map.get() က မသိတဲ့ key အတွက် undefined ပြန်ပေးလို့ allow-list စစ်ဖို့ လွယ်ပါတယ်။

အဖြေ ကြည့်ရန်
search-users.mjs
import { PGlite } from "@electric-sql/pglite";

const db = new PGlite();
await db.exec(`
  CREATE TABLE users (
    id integer PRIMARY KEY, email text NOT NULL, created_on date NOT NULL
  );
  INSERT INTO users VALUES
    (1, 'ada@example.com', '2026-01-10'),
    (2, 'lin@example.com', '2026-03-02'),
    (3, 'max_power@example.com', '2026-02-14'),
    (4, 'maxpower@example.com', '2026-02-20');
`);

// Identifiers cannot be parameters, so map user choices to fixed SQL.
const SORTABLE = new Map([
  ["id", "id"],
  ["email", "email"],
  ["newest", "created_on DESC"],
]);

// Make \, % and _ match literally in LIKE / ILIKE.
function escapeLike(text) {
  return text.replace(/[\\%_]/g, (ch) => "\\" + ch);
}

async function searchUsers(db, emailContains, sortBy) {
  const orderBy = SORTABLE.get(sortBy);
  if (!orderBy) throw new Error("cannot sort by " + JSON.stringify(sortBy));
  const sql = "SELECT email FROM users WHERE email ILIKE $1 ORDER BY " + orderBy;
  const result = await db.query(sql, ["%" + escapeLike(emailContains) + "%"]);
  return result.rows.map((row) => row.email);
}

console.log(await searchUsers(db, "example.com", "newest"));
console.log(await searchUsers(db, "max_", "id"));
console.log(await searchUsers(db, "' OR '1'='1", "id"));
try {
  await searchUsers(db, "a", "email; DROP TABLE users");
} catch (err) {
  console.log("rejected:", err.message);
}

Quick Quiz 1 / 3

ဒီ code မှာ ဘာမှားနေလဲ? const sql = `SELECT * FROM orders WHERE id = ${req.query.id}`; await db.query(sql);

Quick Quiz 2 / 3

User က sort လုပ်မယ့် column ကို ရွေးခွင့်ရှိပါတယ်။ ဘယ်နည်းက မှန်လဲ?

Quick Quiz 3 / 3

Search box က db.query("SELECT * FROM products WHERE name LIKE $1", ["%" + q + "%"]) ကို သုံးပါတယ်။ User က q = "%" လို့ ရှာရင် ဘာဖြစ်မလဲ?

မှတ်ထားရမယ့် အချက်များ

  • SQL string ထဲ input ဆက်ရေးတာ (+, template literal, % formatting) တိုင်းက injection ကို ဖွင့်ပေးပါတယ်။
  • Parameterized query က SQL text နဲ့ value ကို သီးခြား ပို့လို့ value က ဘယ်တော့မှ code မဖြစ်ပါဘူး။ PostgreSQL က $1, $2 ကို သုံးပါတယ်။
  • Table/column နာမည်နဲ့ ASC/DESC ကို parameter မလုပ်နိုင်လို့ allow-list map နဲ့ ကိုင်တွယ်ပါ။
  • LIKE wildcard ကို escape လုပ်ပြီး app role ကို လိုအပ်တဲ့ permission တွေပဲ ပေးပါ။

SQL Injection Prevention Cheat Sheet — OWASP

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။