Secure coding practices and standards

8 min 5 sections 5 practice questions 15 sources citedISC2 objective 8.5

Most flaws in software are the same few mistakes made again by different people. Secure coding practices are the habits that prevent them. Check what comes in, control what goes out, fail safely, keep secrets out of the code, and give the program no more power than it needs. A coding standard writes those habits down so that every developer follows them and a reviewer can check. No programming background is assumed.

By the end of this unit you will be able to:

  • Explain why input is validated on the server against an allow list, after it is put into its simplest form
  • Explain why output encoding and parameterised queries are still needed after validation
  • Choose how a program should report and log an error, and what it does when a check fails
  • Name the main secure coding standards and what each is for
  • Place a flaw in its OWASP Top 10 category

The error page that said too much

An illustrative example

A chain of opticians lets customers book eye tests online. One evening a customer types a date of birth in the wrong format and presses Book.

Instead of a polite message, the page fills with grey text. It shows the name of the database, the folder where the booking program lives, the software version and the exact line of code that failed. The customer takes a screenshot and posts it online with a joke about computers.

Nothing was stolen and nobody broke in. The security manager still opens a ticket marked urgent.

No data was lost. Why does the security manager treat the error page as a flaw to fix at once?

Checking what comes in: validation on the server

Input validation is checking that data arriving from outside is what the program expects before it is used. It runs on the server. A check in the browser or the app gives the user quick feedback, but anyone can send a request without it.

Validation asks two questions. The first is about form: is this the right type, length and pattern, such as a date in the expected format. The second is about sense: does the value make sense here, such as a return date after the outward date.

Allow list and deny list as ways to validate input
PointAllow listDeny list
What it describesWhat is acceptable, such as up to 50 letters, spaces, hyphens and apostrophesKnown bad input, such as particular words or symbols
Everything elseRefusedAccepted
New attack tricksRefused, because they were never on the listAccepted until someone adds them
Effect on honest usersFew, if the list is drawn from real dataOften blocks real names such as O'Brien

Attackers disguise input by writing the same characters in other forms, such as percent codes in a web address. Canonicalisation turns input into its single simplest form before it is checked, so the check sees what the program will really use. after checking lets a disguised value slip through.

Common misconception

“Thorough input validation stops injection and cross-site scripting.”

Validation narrows what arrives, but a valid surname can still contain an apostrophe. The defences against injection are applied where data is used: parameterised queries for the database and output encoding for the browser.

Controlling what goes out: encoding and parameterised queries

Data that is safe to store can be dangerous where it is used. Two practices keep it as data at the point of use.

converts characters that mean something to the receiver into a form that is only displayed. The right encoding depends on where the data lands: in the text of a page, inside an attribute of a page element, inside a script, or inside a web address. Each has its own rules, so encoding is applied by context and as late as possible.

A sends a database command and its values separately, so a value is never read as part of the command. A stored procedure, a query kept inside the database and called by name, is equally safe only if it also binds its values. One that builds a command by joining text is as open to injection as code that does the same.

Validate what comes in, encode what goes out, and never build a command by joining text.

Errors, safe failure and what to log

The opticians' page showed the database name, the program's folder, the software version and the failing line. Each detail tells an attacker what to try next. Good error handling shows the user a plain message and a reference number, and writes the detail to a protected log for staff. One handler for the whole program catches errors nobody predicted.

When a check itself fails, the program must fail securely. If it cannot reach the service that decides permissions, it refuses the request instead of allowing it. Code that grants access unless an error occurs has it backwards: access is granted only when a check positively succeeds.

Log
Sign-in successes and failures, access refused, input that failed validation, and changes to permissions and settings.
Never log
Passwords, session tokens, , database connection strings, card numbers and other sensitive . Mask or remove them first.

Logs can be attacked too. Log injection puts line breaks and fake entries into a value that is later written to a log, so a forged line appears genuine. Encoding values before they are logged, and logging in a structured format, prevents it.

Least privilege, secrets, signing and clean releases

  • for the program itself: its database account may read and write the tables it needs and nothing else, and it never runs as an administrator.
  • in code: each layer checks for itself, so one missed check is not fatal.
  • Secure defaults: a new account, setting or feature starts in its most restrictive state.
  • Vetted libraries for and session handling. Home-made and session schemes are a common source of flaws.
  • Thorough memory and concurrency handling, or a language and runtime that do it.

A password or key written into source code is a hard-coded secret. Everyone who can read the code, now or in future copies, can read the secret, and changing it means releasing new code. Secrets belong in a secrets manager, a protected store the program asks at run time, and are rotated regularly. Scanners check code for secrets before it is saved to the shared store.

Code signing lets users confirm that a release came from its publisher and has not changed. Obfuscation rewrites code so that it is hard for a person to read. It slows reverse engineering and removes no flaw. Debug code, test accounts and comments that reveal internal workings are removed before release, because a forgotten debug switch can skip the password check.

Coding standards and the OWASP Top 10

A secure coding standard is a written set of rules that every developer follows and every reviewer checks against. The best known are these.

SEI CERT coding standards
Rules and recommendations for particular languages, including C, C++, Java, Perl and Android, from the Software Engineering Institute at Carnegie Mellon University.
OWASP Application Security Verification Standard (ASVS)
Testable security requirements for web applications and services, used to design, build, test and buy software. Version 5.0 was published in May 2025.
OWASP Secure Coding Practices checklist
A short, language-neutral checklist of coding practices, now kept in the OWASP Developer Guide.
CWE Top 25
MITRE's yearly list of the most dangerous software weaknesses, ranked from real vulnerability records. The 2025 list opens with cross-site scripting, SQL injection and cross-site request forgery.

The is an awareness list of the most serious to web applications. It is revised every few years, so a category's number depends on the year.

The OWASP Top 10 for 2025, with the 2021 category each continues
20252021 counterpart
A01 Broken Access ControlA01, now including server-side request forgery (2021 A10)
A02 Security MisconfigurationA05
A03 Software Supply Chain FailuresWidens A06 Vulnerable and Outdated Components
A04 Cryptographic FailuresA02
A05 InjectionA03, which includes cross-site scripting
A06 Insecure DesignA04
A07 Authentication FailuresA07 Identification and Authentication Failures
A08 Software or Data Integrity FailuresA08
A09 Security Logging and Alerting FailuresA09 Security and Monitoring Failures
A10 Mishandling of Exceptional ConditionsNew in 2025

A question, worked through

A charity is rebuilding its online donation form. The developer proposes to protect the name field by blocking a list of dangerous characters and words, checked in the donor's browser. The security manager is asked to approve the design before work starts.

Which approach to checking the name field is BEST?

  1. Validate on the server against the length and characters a name may contain
  2. Keep the browser check and add more dangerous characters to its list
  3. Keep the deny list and place a web application firewall in front
  4. Strip apostrophes and other symbols out of names before saving them
Decide on your answer, then open the reasoning
  1. The question asks how to check one input field. Two faults are in the proposal: the check runs in the browser, and it lists what is bad instead of what is allowed.
  2. Rule out b. Anyone can send a request without the browser's check, and a longer deny list still misses tricks nobody has listed.
  3. Rule out d. Stripping characters changes real names such as O'Brien and still does not make the data safe where it is used.
  4. Between a and c: a firewall in front of the site is a useful extra layer, but it is another deny list and leaves the form's own check weak. An allow list on the server refuses anything a name should not contain, including tricks nobody has seen.
  5. Option a. Parameterised queries and output encoding are still needed where the name is used.

Answer: A. Validate on the server against the length and characters a name may contain

Choosing between good answers

One option validates against what is allowed and another blocks what is known to be bad
choose the allow list. A deny list accepts every attack nobody thought to list.
A validation or security check runs only in the browser or the app
it is not a control. The answer repeats it on the server.
An error page shows the database, the code or the software version
the fix is a plain message to the user and the detail in a protected log.
A check cannot be completed because a service is down
the secure answer refuses the request. Code that allows access unless an error occurs fails open.
A question asks where a password used by a program should be kept
choose a secrets manager read at run time, with rotation. Never the source code or a configuration file in the code store.

Check your understanding

5 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 5

A web application asks a central service whether each user may open a record. When the service cannot be reached, the code lets the request through so that staff are not held up.

Which secure coding practice does this break?

What to remember

  • Input is validated on the server, against an allow list, after it is put into its simplest form.
  • Validation narrows input. Parameterised queries and output encoding by context stop injection where the data is used.
  • Users see plain error messages. Detail goes to a protected log that never holds passwords, tokens, keys or card numbers.
  • When a check fails, the program refuses: it fails securely.
  • Secrets live in a secrets manager, not in code. Debug code and test accounts are removed before release. Obfuscation slows reading and fixes nothing.
  • SEI CERT, OWASP ASVS, the OWASP Secure Coding Practices checklist and the CWE Top 25 are the standards and lists most used. The OWASP Top 10 is an awareness list.

Say it from memory

  1. List the main secure coding practices.

    Validate input on the server against an allow list after canonicalisation. Encode output for its context and use parameterised queries. Show plain errors, log detail without secrets, and fail securely. Give the program least privilege and secure defaults. Keep secrets in a secrets manager. Use vetted libraries, sign releases and remove debug code before release. Follow a standard such as SEI CERT or OWASP ASVS.

    Read this part again

  2. What two questions does input validation ask, and what is canonicalisation?

    The first is about form: the right type, length and pattern. The second is about sense: whether the value makes sense here, such as a return date after the outward date. Canonicalisation turns input into its single simplest form before it is checked, so the check sees what the program will really use.

    Read this part again

  3. Why is output encoding applied by context, and when is a stored procedure as safe as a parameterised query?

    Each place data lands, such as page text, an attribute, a script or a web address, has its own rules, so encoding is applied by context and as late as possible. A stored procedure is equally safe only if it also binds its values. One that builds a command by joining text is open to injection.

    Read this part again

  4. What does failing securely mean, and what must never be written to a log?

    If a check itself fails, such as being unable to reach the permissions service, the program refuses the request; access is granted only when a check positively succeeds. Never log passwords, session tokens, encryption keys, database connection strings, card numbers or other sensitive personal data.

    Read this part again

  5. What do the SEI CERT coding standards, OWASP ASVS and the CWE Top 25 each give?

    SEI CERT gives rules and recommendations for particular languages, such as C, C++ and Java. OWASP ASVS gives testable security requirements for web applications and services. The CWE Top 25 is MITRE's yearly list of the most dangerous software weaknesses, ranked from real vulnerability records.

    Read this part again

Sources