Podcast
Questions and Answers
What is a key reason for validating input in modern C++ programming?
What is a key reason for validating input in modern C++ programming?
- To allow greater flexibility in user input
- To prevent unauthorized input from being processed (correct)
- To ensure the program runs faster
- To minimize compiler warnings
Which of the following is NOT listed as a top secure coding practice?
Which of the following is NOT listed as a top secure coding practice?
- Heed compiler warnings
- Implement encryption for all data (correct)
- Sanitize data sent to other systems
- Validate input
What approach should be employed to handle situations when input fails validation?
What approach should be employed to handle situations when input fails validation?
- Log the error and continue processing
- Provide feedback to the user and reject the input (correct)
- Ignore the input and proceed
- Accept the input with warnings
Which principle emphasizes restricting access rights to the least necessary for users and applications?
Which principle emphasizes restricting access rights to the least necessary for users and applications?
What is recommended to prevent security attacks in input validation?
What is recommended to prevent security attacks in input validation?
What does allow-list validation involve?
What does allow-list validation involve?
Why is deny-list validation considered difficult to perform correctly?
Why is deny-list validation considered difficult to perform correctly?
What does the phrase 'Garbage IN, Garbage Out' imply in the context of input validation?
What does the phrase 'Garbage IN, Garbage Out' imply in the context of input validation?
What is the primary purpose of input validation?
What is the primary purpose of input validation?
Which of the following best describes the principle behind allow-list validation?
Which of the following best describes the principle behind allow-list validation?
What is the recommended timing for performing input validation in an information system?
What is the recommended timing for performing input validation in an information system?
What should input validation primarily focus on to mitigate security risks?
What should input validation primarily focus on to mitigate security risks?
Which of the following statements about std::cin is true?
Which of the following statements about std::cin is true?
What potential issue arises from accepting user input that is not validated?
What potential issue arises from accepting user input that is not validated?
When should regular expressions be used according to the guidelines?
When should regular expressions be used according to the guidelines?
How many backslashes are typically needed in a regular expression to represent a single backslash in a normal string?
How many backslashes are typically needed in a regular expression to represent a single backslash in a normal string?
What is the preferred approach to input validation that focuses on defining acceptable input values?
What is the preferred approach to input validation that focuses on defining acceptable input values?
What common issue arises from using a blacklist for input validation?
What common issue arises from using a blacklist for input validation?
Which of the following should NOT be included in a allow-list for input validation?
Which of the following should NOT be included in a allow-list for input validation?
Which of the following regular expression symbols indicates that the previous expression is optional?
Which of the following regular expression symbols indicates that the previous expression is optional?
What does the notation '[A-Za-z0-9]' in a regular expression match?
What does the notation '[A-Za-z0-9]' in a regular expression match?
What type of expression does the '+' symbol represent in regular expressions?
What type of expression does the '+' symbol represent in regular expressions?
Flashcards
String
String
A fundamental programming concept that refers to a sequence of characters, like a word or phrase, stored in a variable.
User Input
User Input
A sequence of characters treated as potential danger. It needs to be carefully examined for security risks before being processed by a program.
Input Validation
Input Validation
The process of checking user input to ensure it adheres to predefined rules and prevents malicious content from entering a program.
Input Validation's Importance
Input Validation's Importance
Signup and view all the flashcards
Allow-list for Input Validation
Allow-list for Input Validation
Signup and view all the flashcards
Allow List Validation
Allow List Validation
Signup and view all the flashcards
Deny List Validation
Deny List Validation
Signup and view all the flashcards
Recovering from Input Validation Failure
Recovering from Input Validation Failure
Signup and view all the flashcards
Failing the Action and Reporting an Error
Failing the Action and Reporting an Error
Signup and view all the flashcards
All input is evil, until proven otherwise
All input is evil, until proven otherwise
Signup and view all the flashcards
Garbage IN, Garbage OUT
Garbage IN, Garbage OUT
Signup and view all the flashcards
Validate input that crosses trust boundaries
Validate input that crosses trust boundaries
Signup and view all the flashcards
Get input as a string and then validate
Get input as a string and then validate
Signup and view all the flashcards
Validate inputs against expected data
Validate inputs against expected data
Signup and view all the flashcards
Syntactic and Semantic Validation
Syntactic and Semantic Validation
Signup and view all the flashcards
Allowlist is More Secure
Allowlist is More Secure
Signup and view all the flashcards
What is Input Validation?
What is Input Validation?
Signup and view all the flashcards
Inline Input Validation
Inline Input Validation
Signup and view all the flashcards
Post-Entry Input Validation
Post-Entry Input Validation
Signup and view all the flashcards
When to use regular expressions?
When to use regular expressions?
Signup and view all the flashcards
Raw strings for regular expressions?
Raw strings for regular expressions?
Signup and view all the flashcards
When to use STL for text manipulation?
When to use STL for text manipulation?
Signup and view all the flashcards
What do regular expressions excel at?
What do regular expressions excel at?
Signup and view all the flashcards
What are some typical use cases for regular expressions?
What are some typical use cases for regular expressions?
Signup and view all the flashcards
Whitelist
Whitelist
Signup and view all the flashcards
Blacklist
Blacklist
Signup and view all the flashcards
Untrusted User Input
Untrusted User Input
Signup and view all the flashcards
CWE-20
CWE-20
Signup and view all the flashcards
Regular Expressions
Regular Expressions
Signup and view all the flashcards
PCRE (Perl-compatible Regular Expression)
PCRE (Perl-compatible Regular Expression)
Signup and view all the flashcards
Metacharacters
Metacharacters
Signup and view all the flashcards
Study Notes
Modern C++ Input Validation Using Regular Expressions
- Course: CSC 2045
- Topic: Secure coding practices in Modern C++ utilizing regular expressions for input validation
Objectives
- Conduct secure coding practices for secure software development
- Identify common software vulnerabilities
- Understand allow-listing and deny-listing approaches to input validation
- Validate all user input against a strict, allow-list-based regular expression pattern
Agenda
- Modern C++ Overview
- Top 10 Secure Coding Practices (Validate input, Heed compiler warnings, Architect and design for security policies, Keep it simple, Default deny, Adhere to least privilege, Sanitize data, Practice defense in depth, Use effective quality assurance techniques, Adopt a secure coding standard, Define security requirements, Model threats)
- Input Validation Defined
- Handling Input Validation Failures (recovering/failing)
- Input Validation Tips & Cheatsheet
- Allow and Deny Lists
std::string
and related mechanisms (accessing strings withstd::cin
andstd::getline
)- Input & Regular Expressions
- Regular Expression Rules
- Steps for Regular Expressions
Input Validation Defined
- Any program input can be a source of security vulnerabilities.
- All input should be treated as potentially dangerous.
- Preventing attacks early, in the input processing phase, is crucial.
- Input validation can detect unauthorized input before the application processes it.
What to Do When Input Fails Validation
- Recover and Continue: If possible, sanitize or fix the invalid input so processing can continue. This approach often involves modifying the input to conform to expectations.
- Failing the Action and Reporting an Error: If the invalid input cannot be fixed, halt the action and report the error to the user. This approach is crucial for maintaining application integrity and user experience, especially in cases where corrupted or malformed data is detected.
Input Validation Tips & Cheatsheet
- "All input is evil, until proven otherwise."
- Garbage In = Garbage Out, and validate inputs at boundaries.
- Use
std::string
and convert if needed to ensure correct data types. - Validate input against expected data (format, length, type, range) before processing it.
Allow Lists
- Allow-list validation defines authorized input.
- Reject any input not on the allow list (anything unexpected). This method is highly recommended to prevent dangerous inputs from entering the application.
Deny Lists
- Deny-list validation defines blocked input. This is less secure than allow-listing. It can be vulnerable to attack vectors that weren't previously identified. Preventing unexpected or harmful input is less secure.
- Knowing all possible attacks is extraordinarily difficult.
- Using deny lists alone is often insufficient for protecting against security threats.
C++ and std::string
std::string
- Strings are sequences of characters.
std::string
object (variable) declaration.- Using
std::string
member functions for various operations (e.g., capacity, modifiers, iterators, element access, non-member functions). - Retrieving input using
std::operator>>
for simple input andstd::getline
when whitespace is not the delimiter.
Handling Invalid Input
- Always consider how users may misuse the program, especially with text input, which might include invalid or unexpected input.
- For each input point, validate for:
- Extraction failure from input streams
- Input exceeding expected size constraints
- Meaningless input
- Input overflow errors to prevent buffer overruns
- Ensure validation occurs consistently at multiple points in the application
Regular Expressions
- Regular expressions are a powerful tool for pattern matching which can be used to perform input validation and other verification actions.
- Review the blog and demo to learn regular expression rules.
- Using regular expressions typically involves three steps:
- Define the regular expression (pattern) using specific characters and symbols.
- Store the result of the match search using functions like
std::regex_search
orstd::regex_match
for string-based validation, and matching, checking if a string matches the pattern. - Analyze the result to determine whether the input conforms to the pattern, and take necessary actions based on the results for appropriate handling.
Additional Information
- The supplied blog post URL (https://www.modernescpp.com/index.php/regular-expressions/) provides further details on using regular expressions for input validation and other related topics.
- Further examples and rules for Regular Expressions are found at the URL.
- Regular expressions can be used to enforce specific formats, lengths, and other constraints on user input.
- Regular expression denial of service (ReDoS) attacks exploit regular expressions to consume excessive resources. Use regular expressions carefully to prevent this type of attack in your applications.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.