Secure Coding and Application Mitigations

Key Takeaways

  • Secure coding reduces vulnerabilities before deployment through design review, input handling, output encoding, authentication, and authorization.
  • Parameterized queries, allow-list validation, output encoding, and safe error handling address common web application risks.
  • Code review, SAST, DAST, SCA, fuzzing, and secrets scanning find different classes of weaknesses.
  • Application mitigations should be placed as close as possible to the root cause; WAF and RASP are useful but often compensating controls.
  • Secure SDLC practices include threat modeling, security requirements, testing gates, and dependency governance.
Last updated: April 2026

Secure Coding and Application Mitigations

Secure coding questions usually ask for the control that prevents a vulnerability class rather than only detecting attacks after release.

Mitigation Mapping

WeaknessBetter mitigationWhy
SQL injectionParameterized queriesSeparates data from SQL commands
Command injectionAvoid shell execution, use allow listsRemoves interpreter abuse path
XSSOutput encoding and sanitizationPrevents untrusted script execution
CSRFAnti-CSRF token and SameSite cookiesConfirms request intent
IDORObject-level authorization checksEnforces ownership on every object
Path traversalCanonicalize paths and use allow listsPrevents escaping intended directories
Hardcoded secretsSecret manager and rotationRemoves secrets from code and images
Verbose errorsGeneric user errors, detailed server logsReduces information disclosure
Insecure uploadType validation, scanning, storage isolationLimits malicious file execution
Race conditionLocking, atomic operations, idempotencyPrevents timing-based logic abuse

Testing and Review Methods

MethodBest at findingWhen used
Threat modelingDesign weaknesses and trust boundariesBefore or during design
Secure code reviewLogic flaws and risky patternsDuring development
SASTSource-level issuesCI pipeline or developer workflow
DASTRuntime web behaviorTest environment or staging
IASTRuntime insight with instrumentationTesting with app context
SCAVulnerable dependencies and licensesBuild and dependency updates
FuzzingCrashes and unexpected input handlingParsers, APIs, native components
Secrets scanningExposed keys and credentialsCommit, build, and registry checks

Worked Example: Fix the Root Cause

A developer builds this query by concatenating user input:

SELECT * FROM accounts WHERE owner = '$user' AND status = 'active'

Escaping input may reduce some risk, but parameterized queries are the stronger answer because the database receives the command structure separately from the input value. Least-privilege database accounts also reduce impact if another flaw exists, but they do not replace secure query construction.

Secure SDLC Controls

SDLC pointSecurity activity
RequirementsDefine authentication, logging, privacy, and data protection needs
DesignThreat model data flows and trust boundaries
DevelopmentSecure coding standards, code review, secrets scanning
BuildSAST, SCA, signed artifacts
TestDAST, fuzzing, abuse-case testing
DeployHardened configuration and secure secrets injection
OperateMonitoring, vulnerability management, incident feedback

Common Traps

TrapBetter exam reasoning
"Validate input" as the only XSS controlOutput encoding is usually central because context matters
WAF as permanent replacement for code fixesWAF can reduce exposure while code is fixed
Hiding buttons to enforce authorizationServer-side authorization must enforce access
Logging full secrets for debuggingLogs must not expose passwords, tokens, or sensitive data

Quick Drill

Choose the most direct mitigation:

  1. User-controlled search value changes SQL results unexpectedly: parameterized query.
  2. User changes JSON to include "role":"admin": allow-list bindable fields and enforce authorization.
  3. Error page reveals stack trace and database path: generic error to user, detailed protected server log.
  4. API accepts duplicate payment confirmation twice: idempotency key or atomic transaction handling.
Test Your Knowledge

Which control most directly mitigates SQL injection?

A
B
C
D
Test Your Knowledge

A web page displays user comments. Which mitigation is most directly associated with preventing stored XSS?

A
B
C
D
Test Your KnowledgeMulti-Select

Which tools help find vulnerable third-party libraries and exposed credentials in a pipeline? Select two.

Select all that apply

Software composition analysis
Secrets scanning
Jamming detector
Cable toner