Secure Coding Fundamentals
By CyberSecurity.pk · Oct 27, 2026 · updated Oct 27, 2026
The mindset
Assume every input is hostile and every component can fail. Security bugs are usually ordinary bugs with security consequences, so build checks into normal development.
Ten practical rules
- Validate input on the server (type, length, range, allow-lists).
- Use parameterised queries, never string-built SQL — see web application security fundamentals.
- Encode output for its context (HTML, attribute, URL, JavaScript).
- Enforce authorisation on every request; deny by default.
- Hash passwords with Argon2id or bcrypt, never roll your own crypto — see password security.
- Keep secrets out of code and git history; use environment variables or a secrets manager. Our Secrets in History challenge shows why.
- Handle errors safely: fail closed, log details privately, show generic messages to users.
- Least privilege for database accounts, services and API keys.
- Update dependencies and watch advisories — vulnerable components cause many incidents (see OWASP Top 10).
- Log security events, but never secrets.
Example: a safer file name check
import re
ALLOWED = re.compile(r"^[A-Za-z0-9._-]{1,64}$")
def safe_name(name: str) -> str:
if not ALLOWED.fullmatch(name):
raise ValueError("invalid file name")
return name
Allow-lists are safer than trying to block "bad" characters.
Tools that help
| Need | Tool |
|---|---|
| Static analysis | Semgrep, Bandit (Python) |
| Secret scanning | Gitleaks, TruffleHog |
| Dependency scanning | pip-audit, Dependency-Check |
Add them to CI so issues are found before release.
Keep learning
The OWASP Secure Coding Practices Quick Reference Guide and ASVS give detailed checklists.
Related tool: Gitleaks
Use techniques only on systems you are authorised to test.
