0Pricing
Secure Coding & OWASP Top 10 for Backend · บทเรียน

กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม

พัฒนาขั้นตอนการตรวจสอบข้อมูลนำเข้าที่รัดกุม ซึ่งครอบคลุมการอนุญาตเฉพาะรายการ การทำข้อมูลให้อยู่ในรูปแบบมาตรฐาน และการบังคับใช้ชนิดข้อมูลอย่างเคร่งครัด เพื่อยับยั้งการโจมตีจากข้อมูลนำเข้าหลากหลายรูปแบบ

กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม เป็นบทเรียน Secure Coding & OWASP Top 10 for Backend ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Secure Coding & OWASP Top 10 for Backend และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Validate Input?

Input validation is the process of ensuring that data provided by a user or another system conforms to expected formats and constraints.

It's your first and most critical line of defense against many types of attacks, like injection, buffer overflows, and even simple logic errors.

Always assume external input is malicious until proven otherwise!

Whitelisting for Safety

When validating input, the safest approach is whitelisting. This means you define what is explicitly allowed, and reject everything else.

  • Whitelisting: "Only these characters/patterns are allowed."
  • Blacklisting: "These characters/patterns are forbidden."

Blacklisting is dangerous because attackers often find ways around forbidden patterns. Whitelisting is proactive and far more secure.

Simple Whitelist Check

Here's a simple Java example of whitelisting allowed characters for a username. Only letters, numbers, and underscore are permitted.

public class InputValidator {
  public static boolean isValidUsername(String username) {
    if (username == null || username.isEmpty()) {
      return false;
    }
    // Whitelist: only letters, numbers, and underscore
    return username.matches("^[a-zA-Z0-9_]+$");
  }

  public static void main(String[] args) {
    String user1 = "coddy_kit_123";
    String user2 = "bad user!";
    String user3 = "admin";

    System.out.println("User '" + user1 + "' is valid: " + isValidUsername(user1));
    System.out.println("User '" + user2 + "' is valid: " + isValidUsername(user2));
    System.out.println("User '" + user3 + "' is valid: " + isValidUsername(user3));
  }
}

Normalize Your Inputs

Canonicalization (or normalization) is the process of converting input data into a standard, simplified, or "canonical" form before validation.

This is crucial because attackers often try to bypass validation by encoding input in different ways (e.g., %2F for /, & for &). Canonicalization ensures all variations are reduced to a common representation.

Canonicalization in Action

This Java snippet shows how you might canonicalize a path by decoding URL encoding and simplifying path components (e.g., removing /./ or /../ if allowed, though typically ../ should be blocked).

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class PathCanonicalizer {
  public static String canonicalizePath(String path) {
    try {
      // 1. URL Decode the path
      String decodedPath = URLDecoder.decode(path, StandardCharsets.UTF_8.name());
      
      // 2. Normalize path separators (e.g., replace backslashes with forward slashes)
      decodedPath = decodedPath.replace("\\", "/");
      
      // 3. Remove redundant path elements (e.g., /./)
      decodedPath = decodedPath.replace("/./", "/");
      
      // Note: Full path traversal prevention requires more complex logic
      // and often involves resolving the path against a base directory.
      
      return decodedPath;
    } catch (Exception e) {
      return null; // Handle decoding errors
    }
  }

  public static void main(String[] args) {
    String input1 = "/usr/local/%2E%2E/etc/passwd";
    String input2 = "/app/data/./report.txt";

    System.out.println("Original: " + input1 + "\nCanonical: " + canonicalizePath(input1));
    System.out.println("\nOriginal: " + input2 + "\nCanonical: " + canonicalizePath(input2));
  }
}

Enforce Data Types

Beyond character sets, validating the data type of input is essential. If you expect an integer, ensure it's an integer. If you expect a boolean, ensure it's true or false.

Incorrect data types can lead to:

  • Application crashes
  • Unexpected behavior
  • Security vulnerabilities (e.g., type juggling attacks in some languages)

Type Check Example

This Java example demonstrates how to parse a string into an integer safely, catching potential NumberFormatExceptions.

public class DataTypeEnforcer {
  public static Integer parseIntegerSafely(String input) {
    if (input == null || input.trim().isEmpty()) {
      return null; // Or throw an IllegalArgumentException
    }
    try {
      return Integer.parseInt(input.trim());
    } catch (NumberFormatException e) {
      System.err.println("Error: '" + input + "' is not a valid integer.");
      return null; // Indicate failure
    }
  }

  public static void main(String[] args) {
    String validNum = "12345";
    String invalidNum = "abc";
    String negativeNum = "-50";

    System.out.println("Parsed '" + validNum + "': " + parseIntegerSafely(validNum));
    System.out.println("Parsed '" + invalidNum + "': " + parseIntegerSafely(invalidNum));
    System.out.println("Parsed '" + negativeNum + "': " + parseIntegerSafely(negativeNum));
  }
}

Limit & Format

Input validation also includes checking the length and format of data:

  • Length Validation: Prevent excessively long inputs that could cause buffer overflows or denial-of-service attacks. Set minimum and maximum lengths.
  • Format Validation: Use regular expressions (regex) to ensure input matches specific patterns, like email addresses, phone numbers, or UUIDs.

Combine these with whitelisting for robust checks.

Server-Side is Key

Remember, client-side validation (in the browser) is only for user experience. Attackers can easily bypass it.

All critical input validation must occur on the server-side. This ensures that even if a malicious user bypasses client-side checks, your backend remains secure.

Never trust input coming from the client!

Validate Your Knowledge

Which of the following are recommended best practices for comprehensive input validation?

Summary of Validation

In this lesson, we explored comprehensive input validation strategies:

  • Always use whitelisting to define what's allowed.
  • Perform canonicalization to normalize input and defeat encoding tricks.
  • Enforce strict data types to prevent unexpected behavior.
  • Validate length and format using regex.
  • Crucially, always perform validation on the server-side.

Robust input validation is a cornerstone of secure backend development!

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

บทเรียน “กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Secure Coding & OWASP Top 10 for Backend ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม”

พัฒนาขั้นตอนการตรวจสอบข้อมูลนำเข้าที่รัดกุม ซึ่งครอบคลุมการอนุญาตเฉพาะรายการ การทำข้อมูลให้อยู่ในรูปแบบมาตรฐาน และการบังคับใช้ชนิดข้อมูลอย่างเคร่งครัด เพื่อยับยั้งการโจมตีจากข้อมูลนำเข้าหลากหลายรูปแบบ คุณปฏิบัติ Secure Coding & OWASP Top 10 for Backend ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Secure Coding & OWASP Top 10 for Backend หรือไม่

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

บทเรียน “กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Secure Coding & OWASP Top 10 for Backend นี้ได้ไหม

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

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

  1. เทคนิค SQLi และ NoSQLi ขั้นสูง
  2. กลยุทธ์การตรวจสอบข้อมูลนำเข้าอย่างครอบคลุม
  3. นโยบายความปลอดภัยของเนื้อหา (CSP) สำหรับแบ็กเอนด์
  4. การป้องกันการแทรกคำสั่งระบบและ LDAP
← กลับไปที่ Secure Coding & OWASP Top 10 for Backend