Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Security

Secure Coding Best Practices

Opens with credits

A practical guide to secure coding for developers who write server-side code in TypeScript, Node.js or Python. It covers trust boundaries and input validation, injection of every kind, output encoding, safe parsing and deserialization, file uploads, path traversal, server-side request forgery, error handling that fails closed, and dependency hygiene, mapped to the OWASP Top 10:2025 and ASVS 5.0.

Written by Tauseef Fayyaz6 chapters, 22 practicesAbout 21 min21 quiz questionsReviewed 13 October 2026

What you will learn

6 chapters · about 21 min

  1. 1Treat all input as untrustedWhere input comes from, how to validate it at the boundary, and the canonicalization and size limits that have to happen before any check means anything.4 practices · 5 min
  2. 2Injection: keep data out of commandsInjection happens whenever data is concatenated into something an interpreter runs. The fix is the same for every interpreter: use an API that keeps data and commands apart.5 practices · 4 min
  3. Output encoding and sanitizationData that is safe in a database can be dangerous in HTML, a URL, a log line or a CSV. Encode for where the data lands, at the moment it lands there.3 practices · 3 minOpens with credits
  4. Safe parsing and deserializationSome data formats can create objects, run code or fetch files while being parsed. Choose formats and parser settings that only ever produce data.3 practices · 2 minOpens with credits
  5. Files, paths and outbound requestsUploads, file paths and server-side fetches let input reach the file system and the internal network. Each needs a fixed, allowlisted shape.3 practices · 3 minOpens with credits
  6. Errors, failure modes and dependenciesCode that fails open, error pages that explain your internals, and packages nobody reviewed. The last chapter covers what happens when things go wrong and what you ship that you did not write.4 practices · 4 minOpens with credits

About this guide

Secure coding is the habit of writing code that still does the right thing when the input is hostile. Most vulnerabilities that reach production are not exotic. They are a query built with string concatenation, a filename used as a path, a URL fetched without checking where it points, or an exception that skips a permission check. Each one is a small decision a developer made while thinking about the happy path. These secure coding best practices are about making the safe decision the default one.

The OWASP Top 10:2025 shows where those decisions go wrong. Injection (A05) maps 37 weaknesses, including SQL injection and cross-site scripting. Broken Access Control (A01) now includes server-side request forgery. Software Supply Chain Failures (A03) and Software or Data Integrity Failures (A08) cover the packages you install and the data you deserialize, and the new Mishandling of Exceptional Conditions (A10) is about code that fails open. The OWASP Application Security Verification Standard (ASVS 5.0) turns those risks into requirements, with chapters on encoding and sanitization (V1), validation and business logic (V2), file handling (V5), secure coding and architecture (V15) and logging and error handling (V16).

This guide follows the path data takes through your code. It starts at the trust boundary, where input arrives, moves through injection and output encoding, then parsing, files and outbound requests, and ends with errors and dependencies. Every practice explains why it matters, what to do and what to avoid, with examples in TypeScript on Node.js and in Python. Authentication, authorization and browser defenses each have their own guide, so this one stays on the code you write between them.

Common questions

What are secure coding practices?

Secure coding practices are habits that keep untrusted data from changing what your code does. The core ones are validating input against an allowlist, using parameterized queries and argument lists instead of string building, encoding output for its context, parsing only data formats, controlling file paths and outbound requests, failing closed on errors, and keeping dependencies reviewed and scanned.

Is input validation enough to prevent SQL injection and XSS?

No. OWASP says validation reduces attack surface but is not the primary defense. SQL injection is stopped by parameterized queries, and XSS by context-aware output encoding and sanitization. Use validation as well, because it rejects bad data early and makes the other defenses easier.

What is the OWASP Top 10:2025?

It is the 2025 edition of OWASP's list of the most critical web application security risks: Broken Access Control, Security Misconfiguration, Software Supply Chain Failures, Cryptographic Failures, Injection, Insecure Design, Authentication Failures, Software or Data Integrity Failures, Security Logging and Alerting Failures, and Mishandling of Exceptional Conditions.

What is the difference between the OWASP Top 10 and ASVS?

The Top 10 is an awareness document that ranks risk categories. ASVS is a verification standard with detailed, testable requirements grouped into chapters and three levels. Use the Top 10 to learn what goes wrong and ASVS to check that a feature is built correctly.

How do I prevent SSRF in a feature that fetches URLs?

Prefer an allowlist of destinations and build the URL yourself. If you must accept arbitrary URLs, resolve the host, reject private, loopback, link-local and metadata addresses for every answer, connect to the address you checked, disable redirects, and restrict outbound traffic at the network level.

Is it safe to use pickle or yaml.load in Python?

Not on data that came from outside your system. Unpickling and full YAML loading can construct objects and execute code. Use JSON or yaml.safe_load for untrusted input, and sign any serialized data you need to accept back from a client.

Sources (8)

Checked against these, last on 13 October 2026.

  1. OWASP Top 10:2025, OWASPtop10.owasp.org/2025/
  2. A10:2025 Mishandling of Exceptional Conditions, OWASPtop10.owasp.org/2025/A10_2025-Mishandling_of_Exceptional_Conditions
  3. OWASP Application Security Verification Standard, OWASPgithub.com/OWASP/ASVS
  4. Input Validation Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html
  5. SQL Injection Prevention Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
  6. Deserialization Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html
  7. File Upload Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html
  8. Server Side Request Forgery Prevention Cheat Sheet, OWASP Cheat Sheet Seriescheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com