Cyberstudy
2.5 4.1 In progress

Secure Coding Techniques

17 flashcards

Summary

Secure software comes from building security into every stage of development. Validating all input, protecting cookies, analyzing code for flaws and signing released code are core techniques that prevent common vulnerabilities and let users trust what they install.

In plain words

Secure 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

  • Secure development lifecycle (SDL): A development process that includes security requirements, threat modeling, secure coding, testing and review at every stage.
  • Input validation: Checking all input for type, length, format and range before using it.
    • Allow list validation: Accept only known-good input; stronger than block lists.
    • Server-side validation: Must always be done, because client-side checks can be bypassed.
  • Output encoding: Converting special characters so data is displayed rather than executed.

Secure cookies

  • Secure attribute: Cookie is sent only over HTTPS.
  • HttpOnly attribute: Scripts can't read the cookie, which limits XSS session theft.
  • SameSite attribute: Restricts sending cookies with cross-site requests, which helps against cross-site request forgery.
  • Expiration: Short lifetimes limit how long a stolen cookie is useful.

Verification

  • Static code analysis: Automated review of source code for flaws without running it. Also called SAST.
  • Code signing: Signing released software with a certificate so users can verify the publisher and that the code is unchanged; it doesn't prove the code is safe.

Important terms

taken from the text above
Secure development lifecycle (SDL)
A development process that includes security requirements, threat modeling, secure coding, testing and review at every stage.
Input validation
Checking all input for type, length, format and range before using it.
Allow list validation
Accept only known-good input; stronger than block lists.
Server-side validation
Must always be done, because client-side checks can be bypassed.
Output encoding
Converting special characters so data is displayed rather than executed.
Secure attribute
Cookie is sent only over HTTPS.
HttpOnly attribute
Scripts can't read the cookie, which limits XSS session theft.
SameSite attribute
Restricts sending cookies with cross-site requests, which helps against cross-site request forgery.
Expiration
Short lifetimes limit how long a stolen cookie is useful.
Static code analysis
Automated review of source code for flaws without running it. Also called SAST.
Code signing
Signing released software with a certificate so users can verify the publisher and that the code is unchanged; it doesn't prove the code is safe.

Examples & real-world scenarios

  • 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

  • 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

  • Name the cookie attributes and what each prevents.
  • Choose input validation methods for a form field.

What I should remember

  • Input validation: Validate all input on the server, preferably against an allow list.
  • HttpOnly: Stops scripts reading cookies.
  • Static code analysis: Finds flaws in source code.
  • Code signing: Proves publisher and integrity, not safety.