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.
Secure coding practices and standards
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.
| Point | Allow list | Deny list |
|---|---|---|
| What it describes | What is acceptable, such as up to 50 letters, spaces, hyphens and apostrophes | Known bad input, such as particular words or symbols |
| Everything else | Refused | Accepted |
| New attack tricks | Refused, because they were never on the list | Accepted until someone adds them |
| Effect on honest users | Few, if the list is drawn from real data | Often 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.
| 2025 | 2021 counterpart |
|---|---|
| A01 Broken Access Control | A01, now including server-side request forgery (2021 A10) |
| A02 Security Misconfiguration | A05 |
| A03 Software Supply Chain Failures | Widens A06 Vulnerable and Outdated Components |
| A04 Cryptographic Failures | A02 |
| A05 Injection | A03, which includes cross-site scripting |
| A06 Insecure Design | A04 |
| A07 Authentication Failures | A07 Identification and Authentication Failures |
| A08 Software or Data Integrity Failures | A08 |
| A09 Security Logging and Alerting Failures | A09 Security and Monitoring Failures |
| A10 Mishandling of Exceptional Conditions | New 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?
Decide on your answer, then open the reasoning
- 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.
- Rule out b. Anyone can send a request without the browser's check, and a longer deny list still misses tricks nobody has listed.
- Rule out d. Stripping characters changes real names such as O'Brien and still does not make the data safe where it is used.
- 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.
- 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
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
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.
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.
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.
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.
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.
Sources
- OWASP Cheat Sheet Series: Input Validation, OWASP FoundationServer-side validation because client checks can be bypassed, syntactic and semantic validation, allow lists over deny lists, decoding and normalising before validation, and validation not replacing parameterised queries or output encoding.
- Cross Site Scripting Prevention Cheat Sheet, OWASP FoundationOutput encoding chosen by the context in which data is placed.
- OWASP Cheat Sheet Series: SQL Injection Prevention, OWASP FoundationParameterised queries, and stored procedures being safe only when they do not build SQL dynamically.
- Error Handling Cheat Sheet, OWASP FoundationGeneric error messages to users, details logged on the server, no stack traces or implementation details, and a global error handler.
- SP 800-53 Rev. 5, Security and Privacy Controls for Information Systems and Organizations, NISTError messages that give what is needed for correction without revealing exploitable information such as stack traces (SI-11), and secure failure (SA-8(24)).
- Logging Cheat Sheet, OWASP FoundationEvents to log, data to exclude from logs such as passwords, session identifiers, keys, connection strings and card data, and log injection with its defences.
- Secrets Management Cheat Sheet, OWASP FoundationHard-coded secrets in source code, central secret stores, rotation, and detecting secrets before they reach the code repository.
- Security Considerations for Code Signing (NIST Cybersecurity White Paper), National Institute of Standards and TechnologyCode signing proving the code was not modified and identifying who signed it.
- CWE-489: Active Debug Code, MITRE (CWE programme, sponsored by CISA)Debug code left active in a release, such as a debug parameter that skips authentication, and its removal before deployment.
- SEI CERT Coding Standards, Software Engineering Institute, Carnegie Mellon UniversityThe SEI CERT coding standards for C, C++, Java, Perl, Android and others from the Software Engineering Institute at Carnegie Mellon University.
- OWASP Application Security Verification Standard v5.0.0, OWASPASVS as a framework of security requirements for designing, developing and testing web applications and services, version 5.0.0 published 30 May 2025.
- OWASP Secure Coding Practices Quick Reference Guide, OWASP FoundationThe Secure Coding Practices Quick Reference Guide, now archived with its checklists moved into the OWASP Developer Guide.
- 2025 CWE Top 25 Most Dangerous Software Weaknesses, MITRE (CWE programme, sponsored by CISA)The 2025 CWE Top 25 Most Dangerous Software Weaknesses, built from CVE records, opening with cross-site scripting, SQL injection and cross-site request forgery.
- OWASP Top 10:2025, OWASPThe ten categories of the 2025 list and their changes from 2021: SSRF folded into broken access control, supply chain failures widening vulnerable components, renamed authentication and logging categories, and exceptional conditions as new.
- OWASP Top 10:2021, OWASP FoundationThe ten categories of the 2021 list and their numbers.