Domain 8

Objective 8.5

Secure coding guidelines

What it is

Secure coding is a set of habits that keep ordinary features from becoming weapons. Validate input: treat everything from outside as untrusted and check it against what you expected. Encode output so a browser or a database does not treat data as instructions. Injection is when an attacker's text becomes a command, such as SQL injection, where database syntax is smuggled inside a form. Cross-site scripting, XSS, is when an attacker's script runs in a victim's browser because you echoed input as if it were safe page content. Broken access control is when the program fails to check that this user may touch this object. Do not hard-code secrets. Fail with errors that help operators without teaching attackers your layout. Give the application least privilege so a bug cannot do everything the database can do.

Why it matters

Guidelines turn personal taste into a team habit. They matter most on the bugs that repeat: injection, access control, and secrets in code. A framework that escapes output by default prevents whole classes of XSS. Parameterized queries prevent whole classes of SQL injection. Review and automated checks exist to make the guideline real. A PDF of rules that new code ignores is decoration.

A simple example

A cafe that writes orders on a ticket should not let a customer rewrite the price column by adding extra pen marks that the cashier obeys as instructions. Checking the order against the menu is input validation. Printing the note as words, not as a command to the till, is output encoding. The till login that can only take payments, not edit the recipe book, is least privilege. The safe combination written on the ticket is a hard-coded secret.

On the exam

Name the bug class from the story. User text executed by the database is injection. User text executed in a browser is XSS. A missing check on the object id is broken access control. Fixes are parameterized queries, contextual encoding, and server-side authorization, not “filter the word script” and not hiding the button. Secrets go in a proper store and are rotated if committed. A trap is client-side checks alone. The attacker does not have to use your screen.

Words worth keeping

Injection
Untrusted input that the program treats as a command, such as SQL or a shell.
XSS
Untrusted input that runs as script in someone else's browser.
Parameterized query
A database call that keeps user data separate from the command, so it cannot change the command.
Practice this section