Secure code review

Reviewing defensive code paths and unsafe application behaviours

Secure implementation: code, settings, gate arranged to illustrate reviewing defensive code paths and unsafe application behaviours.

Secure coding requires deliberate trust boundaries, defensive behaviour and safe handling of shared state. Understanding defensive code paths and unsafe application behaviours helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

Security architecture describes assets, identities, trust boundaries and the controls expected between components. Documentation is useful when it reflects real deployment and helps reviewers test important assumptions, rather than simply listing products or broad intentions.

This section covers vulnerability types, including type juggling, prototype pollution, and others, which result from using insecure coding patterns in a particular language. Some may not be relevant to all languages, whereas others will have language-specific fixes or may relate to how a particular language or framework handles a feature such as HTTP parameters. It also considers the risk of not cryptographically validating application updates.

It also considers the risks associated with using objects to represent data items and accepting and returning these via external APIs. In this case, the application must ensure that data fields that should not be writable are not modified by user input (mass assignment) and that the API is selective about what data fields get returned. Where field access depends on a user's permissions, this should be considered in the context of the field-level access control requirement in the Authorisation chapter.

Why it matters

Consider a realistic failure: two individually valid code paths combine to violate an important application permission or transaction assumption.

An undocumented trust assumption can leave a feature depending on a check that no component actually performs. Gaps may span applications, services and operational processes, making ownership and verification especially important.

A code audit connects a stated requirement to the implementation responsible for enforcing it. Source review and targeted behavioural checks should explain the same boundary and any relevant exceptions.

How penetration testers check for it

Review the specific control expectations below against the code, deployment and observable application behaviour.

  • Confirm that the application only returns the required subset of fields from a data object. For example, it should not return an entire data object, as some individual fields should not be accessible to users.
  • Confirm that where the application backend makes calls to external URLs, it is configured to not follow redirects unless it is intended functionality.
  • Confirm that the application has countermeasures to protect against mass assignment attacks by limiting allowed fields per controller and action, e.g., it is not possible to insert or update a field value when it was not intended to be part of that action.
  • Confirm that all proxying and middleware components transfer the user's original IP address correctly using trusted data fields that cannot be manipulated by the end user, and the application and web server use this correct value for logging and security decisions such as rate limiting, taking into account that even the original IP address may not be reliable due to dynamic IPs, VPNs, or corporate firewalls.
  • Confirm that the application explicitly ensures that variables are of the correct type and performs strict equality and comparator operations. This is to avoid type juggling or type confusion vulnerabilities caused by the application code making an assumption about a variable type.
  • Confirm that JavaScript code is written in a way that prevents prototype pollution, for example, by using Set or Map instead of object literals.
  • Confirm that the application has defenses against HTTP parameter pollution attacks, particularly if the application framework makes no distinction about the source of request parameters (query string, body parameters, cookies, or header fields).
  • Compare documented trust relationships with deployment and code.
  • Record assumptions, exceptions and evidence of enforcement.

How to fix it

Implement the required boundary in the component that actually performs the sensitive operation and retain focused regression coverage.

  • Make security requirements concrete and assign owners.
  • Keep diagrams and inventories aligned with releases.
  • Reduce unnecessary trust and privilege between components.
  • Verify important requirements through focused review and testing.

A diagram or checklist is planning evidence, not proof that a control works. Assurance comes from connecting the stated requirement to implementation and observed behaviour.

Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.

Read about our methodology or contact pentested.com to discuss the security of your application code.

See your first test report this week.

Book a 30 minute demo. We will verify one of your targets with you and run a test against it, so you can judge the results on your own systems.

Book a demo