Secure coding basics: the web flaws attackers use most, and how to prevent them


Illustration: a shield with a code symbol

At a glance

Audience
Developers & coders
Difficulty
Beginner
Time needed (minutes)
25 min
Last reviewed
Nov 22, 2026

What you need

  • An application you write or maintain (any language or framework)
  • Access to its code repository

Attackers rarely need a clever new trick. Year after year, the same few families of mistakes cause most of the serious flaws found in web applications. Build the eight habits below into your team's everyday work and you will close the doors attackers try first.

🔐

Check every request

Who is asking, and may they touch this record? Decide on the server, every time.

🧱

Keep data and code apart

Parameters and escaping, never strings glued together.

📦

Know what you ship

Patched dependencies, safe settings, no secrets in the code.

The list behind this guide is the OWASP Top 10, the ranking of web application risks published by the Open Worldwide Application Security Project. You do not need to memorize it: the habits are what matter.

1. Check access on the server, every time

A user changes an ID in an address, from /invoices/1042 to /invoices/1043, and reads someone else's invoice. Or they call an admin-only API because the button was only hidden. This is broken access control, the most common serious flaw in web applications.

The habit

Every request that reads or changes data checks, on the server, that this user may do this to this record. Deny by default, put the rule in one shared place (a policy, a middleware or your framework's permissions), and write a test that tries to open another user's record and expects a refusal.

2. Never glue user input into queries or pages

When input is pasted into an SQL query, a shell command or an HTML page, an attacker can turn data into code: read the whole database, run commands on your server, or run script in other people's browsers. This is injection, including cross-site scripting.

❌ Vulnerable: the input becomes part of the query

$sql = "SELECT * FROM users WHERE email = '" . $_GET['email'] . "'";

✅ Safe: the input is only ever data

$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$_GET['email']]);
  • Use parameterized queries or your ORM's query builder, in every language.
  • Let your template engine escape output (Twig, Blade, JSX and Razor do it by default) and review every "raw" or "unescaped" output.
  • Avoid the shell; if you must call it, pass arguments as a list, never as one string.
  • Add a Content Security Policy header, so injected script is blocked even if one slips through.

3. Do not write your own login

Passwords stored with fast hashes, no limit on guessing, sessions that never end, reset links that can be reused: weak authentication hands accounts to attackers.

The habit

Use your framework's authentication or a proven identity provider. Hash passwords with Argon2id, bcrypt or scrypt. Limit login attempts, offer multi-factor authentication or passkeys, make reset links single-use and short-lived, and mark session cookies Secure, HttpOnly and SameSite.

4. Ship with safe settings

Debug pages left on, detailed errors shown to visitors, default admin accounts, public storage buckets, missing security headers: misconfiguration needs no skill to exploit.

  1. Turn off debug output in production. Show visitors a generic error; log the details privately.
  2. Remove samples and defaults. Example files, test accounts and default passwords go before launch.
  3. Keep storage private. Cloud buckets and file folders are private unless something must be public.
  4. Keep configuration as code. Then the safe settings are the ones that get deployed, every time.

5. Know and patch what you depend on

Most applications are mostly other people's code. A known flaw in any library you include is a flaw in your application. Commit your lock file, run your ecosystem's audit tool on every build, and update on a schedule. Our guide How to find and fix vulnerable dependencies in your code walks you through it.

6. Protect data in transit and at rest

Serve everything over HTTPS with modern TLS, use your platform's built-in encryption for stored data, and never invent your own cryptography. Above all, collect only the personal information you need and delete it when you no longer do.

Data you do not keep cannot leak.

7. Keep secrets out of the code

API keys and passwords committed to a repository will be found, often within minutes if it is public. Load them from the environment or a secrets manager and scan every commit. Our guide How to keep secrets and API keys out of your code shows how.

8. Log what matters, and read it

Many breaches go unnoticed for months because nobody recorded, or read, the failed logins and refusals that came first. Log sign-ins, access refusals and admin actions (never passwords, tokens or full personal information), send the logs where an attacker on the server cannot erase them, and set a few alerts a person actually reads.

Make it routine

Add these five questions to your pull request template, so every reviewer asks them:

Review checklist

  • Does every new endpoint check, on the server, who may use it?
  • Is all user input handled with parameters or escaping, never concatenation?
  • Did the dependency audit pass, and the secret scan find nothing?
  • Are errors generic for users and detailed only in private logs?
  • Does the change collect or keep new personal information, and is that necessary?

⚠ Why it matters in Canada

A flaw that exposes personal information is also a legal matter. Under the federal Personal Information Protection and Electronic Documents Act (PIPEDA), an organization must report a breach of security safeguards that creates a real risk of significant harm to the Office of the Privacy Commissioner of Canada and notify the people affected. In Quebec, Law 25 sets similar duties for confidentiality incidents. Secure coding is the cheapest way never to file that report.

Keep learning

Next: find the vulnerable libraries in your project, and keep your keys out of Git.

All guides for developers →

  • Secure coding
  • OWASP Top 10
  • Web applications
  • PIPEDA

Sources

This guide was drafted with the help of AI from the public sources listed above, then checked and edited by the newsroom. See how we use AI.

About the author

Editor's Picks

The best of cybersecuritynews.ca, delivered to you weekly.

On Display
Illustration: a key with a padlock

How to keep secrets and API keys out of your code

Illustration: an open package with a warning sign

How to find and fix vulnerable dependencies in your code

Bell Canada

Bell Canada says an “agentic” hacker stole enterprise customer data from its circuit-ordering system