Secure Coding Techniques
Summary
PDF p.291Secure coding techniques are essential to ensure that new programming technologies are safe and reliable before deployment. Modern development practices integrate security throughout the software development lifecycle, focusing on preventing vulnerabilities and ensuring compliance with security standards.
In plain words
Supplementary — not from your PDFSecure development builds security into the whole lifecycle (Microsoft SDL, OWASP SAMM). Key techniques: input validation (allow lists, type and range checks, regex, and encoding so special characters aren't treated as code); secure cookies (Secure, HttpOnly, SameSite, short expiry); static code analysis (SonarQube, Fortify, Coverity); and code signing, which proves who published code and that it hasn't changed, though not that it's safe.
Detailed explanation
PDF p.291Security Development Lifecycle
- Modern Practices: Incorporate security considerations alongside functionality and usability.
- Examples: Microsoft's Security Development Lifecycle (SDL) and OWASP's Software Assurance Maturity Model (SAMM) and Security Knowledge Framework.
Input Validation
- Importance: Prevents untrusted input from manipulating application behavior.
- Vulnerabilities: Without input validation, applications are susceptible to injection attacks like SQL injection, code injection, and cross-site scripting (XSS).
-
Methods
- Allowlisting: Permits only approved inputs.
- Blocklisting: Blocks known harmful inputs.
- Data Type Checks: Ensures input is of the expected type.
- Range Checks: Validates numeric inputs fall within expected ranges.
- Regular Expressions: Matches input to expected patterns.
- Encoding: Prevents special characters from being interpreted as commands.
Secure Cookies
- Function: Store session states, user preferences, and other settings.
-
Security Measures
- Secure Attribute: Ensures cookies are sent over HTTPS.
- HttpOnly Attribute: Prevents client-side scripts from accessing cookies.
- SameSite Attribute: Limits when cookies are sent to mitigate cross-site request forgery.
- Expiration Limits: Restricts the usable life of cookies.
Static Code Analysis
- Purpose: Identifies vulnerabilities, errors, and noncompliant coding practices before deployment.
- Tools: SonarQube, Coverity, Fortify.
- Benefits: Early detection of bugs, improved code quality, and education on common coding errors.
Code Signing
- Function: Uses digital signatures to verify the integrity and authenticity of software code.
- Process: Signer uses a private key to encrypt a hash of the code, forming a digital signature.
- Certificate Authority (CA): Issues certificates to verify the signer's identity.
- Benefits: Ensures software has not been tampered with and confirms the publisher's identity.
- Limitations: Does not guarantee the safety or security of the code itself.
Important terms
taken from the text above- Modern Practices
- Incorporate security considerations alongside functionality and usability.
- Allowlisting
- Permits only approved inputs.
- Blocklisting
- Blocks known harmful inputs.
- Data Type Checks
- Ensures input is of the expected type.
- Range Checks
- Validates numeric inputs fall within expected ranges.
- Regular Expressions
- Matches input to expected patterns.
- Encoding
- Prevents special characters from being interpreted as commands.
- Secure Attribute
- Ensures cookies are sent over HTTPS.
- HttpOnly Attribute
- Prevents client-side scripts from accessing cookies.
- SameSite Attribute
- Limits when cookies are sent to mitigate cross-site request forgery.
- Expiration Limits
- Restricts the usable life of cookies.
- Certificate Authority (CA)
- Issues certificates to verify the signer's identity.
Examples & real-world scenarios
Supplementary — not from your PDF- Rejecting an age field that isn't a number between 0 and 130.
- Setting session cookies with HttpOnly, Secure and SameSite.
- Running SonarQube in the build pipeline.
Scenario
A developer's form accepts any text and inserts it into pages. Adding server-side validation and output encoding prevents injected scripts from running in other users' browsers.
Common mistakes
Supplementary — not from your PDF- Thinking code signing proves the code is secure. It proves origin and integrity only.
- Relying on block lists for input instead of allow lists.
Practical skills
Supplementary — not from your PDF- Name the cookie attributes and what each prevents.
- Choose input validation methods for a form field.
What I should remember
Key Points PDF p.291-
Security Development Lifecycle
- Modern Practices: Integrate security with functionality and usability.
- Examples: Microsoft's SDL, OWASP SAMM.
-
Input Validation
- Importance: Prevents manipulation of application behavior.
- Methods: Allowlisting, blocklisting, data type checks, range checks, regular expressions, encoding.
-
Secure Cookies
- Function: Store session states and preferences.
- Security Measures: Secure, HttpOnly, SameSite attributes, expiration limits.
-
Static Code Analysis
- Purpose: Identifies vulnerabilities and errors.
- Tools: SonarQube, Coverity, Fortify.
- Benefits: Early bug detection, improved code quality.
-
Code Signing
- Function: Verifies integrity and authenticity of code.
- Process: Uses digital signatures and certificates.
- Benefits: Ensures code integrity, confirms publisher identity.
- Limitations: Does not guarantee code safety.