Secure development

Implementing precise cross-origin access policies

Secure implementation: code, settings, gate arranged to illustrate implementing precise cross-origin access policies.

Implementing precise cross-origin access policies connects a security requirement to the behaviour of a real feature. The relevant controls need to remain effective across custom code, integrations and the production configuration. Understanding precise cross-origin access policies helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

Browser security is influenced by origins, embedded content, messaging and the way returned data is interpreted. A server can make a correct decision while client-side code later exposes the result to an unintended page or execution context.

Browsers stop a page from reading responses from another origin unless that origin agrees. Cross-Origin Resource Sharing (CORS) is how a server agrees: it sends headers such as Access-Control-Allow-Origin and Access-Control-Allow-Credentials that tell the browser which other origins may read its responses (Fetch Standard: CORS protocol).

Compare the Origin request header against a fixed allowlist of full origins (scheme, host and port), and only echo it back in Access-Control-Allow-Origin if it matches exactly. Most CORS vulnerabilities come from getting this check wrong (PortSwigger: Errors parsing Origin headers):

Why it matters

Consider a realistic failure: a cross-origin exception intended for one integration permits credentialed access from unrelated websites.

A browser trust failure can affect signed-in users, private responses or actions taken with a user's session. The assessment should identify the relevant browser behaviour and the server-side permissions that remain in force.

Implementation guidance should be applied to a real feature and its deployment. Framework defaults, custom handlers and integrated services all influence whether the intended protection remains effective.

How penetration testers check for it

Trace the relevant feature from its entry point to the data or operation it protects, including custom code and alternate paths.

  • Map trusted origins, embedded resources and messaging relationships.
  • Review response policies and actual browser behaviour.
  • Trace untrusted data into rendering and navigation.
  • Check credentialed requests and sensitive state changes.

How to fix it

Use the appropriate maintained mechanisms and verify the result in the actual production configuration.

  • CORS is not access control. The browser enforces it, and only for scripts running in the browser. Scripts can't change the Origin header (MDN: Forbidden request header), but any other client can send whatever Origin it likes. Authenticate and authorize every request on the server as if CORS didn't exist.
  • Not sending CORS headers is the safe default. Only add them to endpoints that another origin actually needs to read.
  • Don't reflect the Origin header without checking it. Combined with Access-Control-Allow-Credentials: true, this lets any website read authenticated responses.
  • Don't match prefixes, suffixes or loose regular expressions. A suffix check for example.com also allows attackerexample.com, and a prefix check allows example.com.attacker.net. In regular expressions, escape the dots and anchor both ends.
  • Validate messaging origins and intended recipients.
  • Retain independent server-side identity and permission checks.

A policy finding needs application context. The absence of a particular header or browser feature is not, by itself, proof that protected data can be accessed.

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 secure development and code review.

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