0Pricing
Vibe Coding · บทเรียน

พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์

หลีกเลี่ยงความเสี่ยงที่ปัญญาประดิษฐ์มักมองข้าม

พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์ เป็นบทเรียน Vibe Coding ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vibe Coding และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

AI Writes Fast — Sometimes Unsafely

AI is brilliant at getting something working quickly. But "working" and "safe" aren't the same. AI often leaves real apps open to attacks because it optimizes for the happy path, not for hostile users.

The good news: a handful of habits catch most problems, and you can ask AI to apply them. This lesson covers the security risks vibe coders hit most.

Never Put Secrets in Code

The #1 mistake: pasting an API key or password directly into your code. If that code goes to GitHub, the key is public forever — bots scan for leaked keys within minutes.

Secrets belong in environment variables, never in the source. Spot the difference below.

// DANGER: key is hardcoded and will leak
const apiKey = 'sk-live-abc123realkey';

// SAFE: key comes from the environment, not the code
const apiKey = process.env.API_KEY;

if (!apiKey) {
  throw new Error('API_KEY is missing — set it in your .env file');
}

Keep Secrets Out of Git

Store secrets in a .env file, and make sure Git ignores that file so it never gets committed. One line in .gitignore does it.

Ask your AI tool to set this up correctly from the start.

Set up environment variables for this project:
- create a .env file with API_KEY=
- add .env to .gitignore so it's never committed
- create a .env.example with empty placeholders to commit instead
- show me where the code reads process.env.API_KEY

Never Trust User Input

The golden rule of security: anything a user can type, a user can lie about. Attackers send broken, oversized, or malicious input on purpose. Your code must validate it.

Here's a basic guard that checks input before using it.

function createUser(input) {
  if (typeof input.email !== 'string' || !input.email.includes('@')) {
    return { error: 'Invalid email' };
  }
  if (input.age < 0 || input.age > 150) {
    return { error: 'Invalid age' };
  }
  return { ok: true };
}

console.log(createUser({ email: 'bad', age: 30 }));
console.log(createUser({ email: 'a@b.com', age: 30 }));

SQL Injection: A Classic AI Slip

If AI builds your database query by gluing user input into a string, an attacker can inject their own commands and steal or delete data. This is SQL injection.

The fix is parameterized queries — the database treats input as data, never as commands. Always ask AI to use them.

// DANGER: user input glued straight into the query
db.query("SELECT * FROM users WHERE email = '" + email + "'");

// SAFE: parameterized — the ? is filled in safely
db.query('SELECT * FROM users WHERE email = ?', [email]);

Ask AI to Review for Security

You can turn the AI into a security reviewer. Paste your code and ask it to hunt for vulnerabilities specifically — it's much better at finding them when you ask directly.

Review this code for security problems. Specifically check for:
- hardcoded secrets or API keys
- unvalidated user input
- SQL injection
- missing authentication checks
List each issue, why it's risky, and the fix. Here is the code: <paste>

Protect What Should Be Private

AI often builds an endpoint that returns data without checking who is asking. That means anyone could read another user's data just by changing an ID in the URL.

Every sensitive action needs an authorization check: is this user allowed to do this?

// DANGER: anyone can read any order by guessing the id
app.get('/orders/:id', (req, res) => {
  res.json(getOrder(req.params.id));
});

// SAFE: only the owner can see their order
app.get('/orders/:id', (req, res) => {
  const order = getOrder(req.params.id);
  if (order.userId !== req.user.id) return res.status(403).end();
  res.json(order);
});

Keep Dependencies Updated

Your app uses many packages written by others. Some have known security holes. Outdated dependencies are one of the most common ways apps get hacked.

A quick command checks for known vulnerabilities, and AI can help you understand the results.

# Check installed packages for known vulnerabilities
npm audit

# Then ask AI:
# "npm audit reports 3 high-severity issues in <package>.
#  Explain the risk in plain English and tell me the safest way to fix it."

Don't Leak Errors to Users

When something breaks, AI-generated code sometimes shows the raw error — including file paths, database details, or stack traces. That's a gift to attackers.

Log details privately on the server, but show users a generic, friendly message.

try {
  doRiskyThing();
} catch (err) {
  console.error('Internal error:', err); // private log
  res.status(500).json({ error: 'Something went wrong. Please try again.' });
  // never send err.stack or err.message to the user
}

A Security Checklist You Can Reuse

Before you ship anything, run through this. Paste it to your AI and ask it to verify each item against your code.

  • No secrets in the code or Git history.
  • All user input validated.
  • Database queries are parameterized.
  • Sensitive routes check who is logged in and what they're allowed to do.
  • Dependencies audited and updated.
  • Errors logged privately, generic messages to users.

Security Is a Conversation, Not a One-Time Step

You won't catch everything, and that's fine. The mindset that protects you: assume AI code is insecure until reviewed, ask security questions every time you add a feature, and re-run your checklist before each deploy.

Treat the AI as a careful partner you actively interrogate — not an oracle you blindly trust.

Quick Check

Your AI assistant generates a login feature and hardcodes the database password right in the source file. What should you do?

Recap: Safe by Habit

AI ships fast but skips safety, so you supply the safety. You learned to: keep secrets out of code, validate all input, use parameterized queries, add authorization checks, audit dependencies, and hide raw errors.

Best of all, you can ask AI to review for these exact issues and run a checklist before every deploy. Next: keeping your code clean and maintainable.

คำถามที่พบบ่อย

บทเรียน “พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vibe Coding ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์”

หลีกเลี่ยงความเสี่ยงที่ปัญญาประดิษฐ์มักมองข้าม คุณปฏิบัติ Vibe Coding ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vibe Coding หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vibe Coding บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Vibe Coding นี้ได้ไหม

ได้ บทเรียน Vibe Coding ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเพิ่มการทดสอบด้วยปัญญาประดิษฐ์
  2. พื้นฐานความปลอดภัยสำหรับโค้ดปัญญาประดิษฐ์
  3. การรักษาโค้ดให้ดูแลต่อได้
  4. ประสิทธิภาพและต้นทุน
← กลับไปที่ Vibe Coding