Secure Coding, API Security and Software-Defined Security
Secure coding practices prevent common flaws, APIs need specific protections, and security itself is increasingly defined in code. This lesson covers all three.
Learning outcomes
After completing this lesson, you will be able to:
- Explain core secure coding practices.
- Describe the main API security risks and controls.
- Explain software-defined security and policy as code.
Secure coding practices
Core secure coding practices include input validation on the server side, preferring allowlists of expected formats; output encoding appropriate to the context (HTML, JavaScript, URL) to prevent cross-site scripting; parameterised queries or prepared statements to prevent SQL injection; secure authentication and session management; least privilege for code and service accounts; error handling that fails securely without revealing details; vetted cryptographic libraries; and keeping secrets out of code.
Guidance includes the OWASP Application Security Verification Standard (ASVS), the SEI CERT coding standards and NIST's Secure Software Development Framework (SSDF, SP 800-218). Practices are enforced through training, coding standards, code review and automated testing. Client-side validation improves usability but provides no security, because attackers bypass the client.
Each practice defeats a specific weakness by keeping data and code apart. A parameterised query sends the query structure and the user's input to the database separately, so the database always treats the input as a value to compare, never as part of the query's logic; the injection string from the previous lesson simply becomes an unusual user name that matches nobody. Output encoding converts characters that have special meaning in a web page, such as angle brackets and quotes, into harmless representations before display, so a script submitted to a forum is shown as text instead of being executed. Encoding must match the context, because the characters that are dangerous inside HTML differ from those dangerous inside JavaScript or a URL.
Input validation is a complementary layer rather than a complete defence. An allowlist that accepts only digits for a quantity field rejects most malicious input immediately, but a free-text comment field must accept quotes and symbols, so it relies on parameterisation and encoding for protection. Anti-CSRF tokens, unpredictable values included in each form and checked by the server, together with the SameSite cookie attribute, stop forged requests. The SSDF organises these practices into four groups: prepare the organisation, protect the software, produce well-secured software and respond to vulnerabilities.
The following diagram compares a client-side check with parameterised queries.

Practice against weakness
API security
APIs expose application functions and data directly to other programs, often over the internet. The OWASP API Security Top 10 is led by broken object-level authorisation, in which changing an identifier in a request returns another user's data, followed by risks such as broken authentication, broken object property level authorisation, unrestricted resource consumption and broken function-level authorisation.
Controls include strong authentication (OAuth 2.0 tokens, mutual TLS for service clients), authorisation checks on every object and function, schema-based input validation, rate limiting and quotas, returning only the fields needed, an inventory of all APIs and versions, API gateways and comprehensive logging.
The API risks differ in what they let an attacker reach. Broken object-level authorisation concerns which records a caller may access, such as another customer's order. Broken object property level authorisation concerns which fields: an API that lets users update their profile may also accept a field such as "role" and let a user make themselves an administrator, a flaw called mass assignment. Broken function-level authorisation concerns which operations: an ordinary user who calls an administrative endpoint directly may find that it performs no role check. Unrestricted resource consumption lets a caller request millions of records or send requests without limit, causing denial of service or high costs.
An inventory matters because forgotten APIs are rarely protected. Old versions left running after a new version is released, test APIs exposed to the internet and undocumented endpoints used by a mobile app are common sources of breaches, because nobody remembers to apply the controls that protect the current production API.
Which API risk?
Software-defined security
Software-defined security implements and manages security controls through software and APIs. Firewall rules, network segmentation, identity policies and compliance rules are written as code (policy as code), stored in version control, reviewed and deployed automatically, so controls scale with the infrastructure they protect. Examples include security groups defined in infrastructure-as-code templates and automated guardrails that block non-compliant deployments. The benefits are consistency, auditability and speed; the risk is that a single faulty policy change spreads everywhere instantly, so policy code needs the same review and testing as application code.
A policy-as-code guardrail
An organisation decides that no storage bucket may be publicly readable and no database may be created without encryption. How can policy as code enforce these rules?
The organisation writes a policy stating that no storage bucket may be publicly readable and no database may be created without encryption. The policy is stored in a repository, reviewed like any other code and evaluated automatically in two places: in the deployment pipeline, where a template that violates it fails the build with a clear message to the developer, and in the cloud environment, where any resource changed by hand to break the rule is flagged or corrected.
The security team no longer reviews every deployment by hand, yet every deployment is checked against the same rules, and the policy's version history shows exactly when each rule took effect.
Preventing SQL injection
A developer fixes a SQL injection flaw by adding JavaScript in the browser that rejects quotes in the search box. Is the application now protected?
No. Attackers can send requests directly to the server and bypass client-side checks. The correct fix is to use parameterised queries on the server, so input is always treated as data, supported by server-side input validation and a least-privilege database account. Client-side validation can remain for usability.
Which API risk occurs when changing an identifier in a request returns another user's data?
Show answer
Broken object-level authorisation.
Exam Tips
Remember for the exam
- Parameterised queries stop SQL injection; output encoding stops XSS.
- Validate on the server; client-side checks are for usability.
- Authorise every API request for every object.
- Review and test policy-as-code like application code.