API security

CORS and cookie authentication risks in browser-facing APIs

Browser origins: browser, code, gate arranged to illustrate cors and cookie authentication risks in browser-facing apis.

Browser-facing APIs need deliberate origin and session controls when credentials are sent automatically. Understanding CORS and cookie authentication risks in browser-facing APIs helps establish which security property the feature needs and where that property is actually enforced.

What does this involve?

Authentication establishes who is making a request and the strength and freshness of that evidence. Enrolment, recovery, alternate channels and changes to authenticators are part of the same boundary. An impressive login screen provides little assurance if another path grants equivalent access with weaker checks.

When accepting a request to sensitive functionality on the server side, the application needs to ensure the request is initiated by the application itself or by a trusted party and has not been forged by an attacker.

Sensitive functionality in this context could include accepting form posts for authenticated and non-authenticated users (such as an authentication request), state-changing operations, or resource-demanding functionality (such as data export).

Why it matters

Consider a realistic failure: an unrelated website causes a signed-in browser to perform a protected operation.

Account access can lead to disclosure, unwanted changes or misuse of a trusted identity. The consequence depends on the account's permissions and the actions available after authentication, rather than the appearance of the login mechanism.

An API review follows the server-side behaviour behind the interface. Direct requests, alternate methods and background operations can reveal boundaries that the normal frontend journey does not exercise.

How penetration testers check for it

Assess allowed origins, credentialed requests, CSRF protections and session cookies together.

  • Verify that, if the application does not rely on the CORS preflight mechanism to prevent disallowed cross-origin requests to use sensitive functionality, these requests are validated to ensure they originate from the application itself. This may be done by using and validating anti-forgery tokens or requiring extra HTTP header fields that are not CORS-safelisted request-header fields. This is to defend against browser-based request forgery attacks, commonly known as cross-site request forgery (CSRF).
  • Verify that, if the application relies on the CORS preflight mechanism to prevent disallowed cross-origin use of sensitive functionality, it is not possible to call the functionality with a request which does not trigger a CORS-preflight request. This may require checking the values of the 'Origin' and 'Content-Type' request header fields or using an extra header field that is not a CORS-safelisted header-field.
  • Confirm that HTTP requests to sensitive functionality use appropriate HTTP methods such as POST, PUT, PATCH, or DELETE, and not methods defined by the HTTP specification as "safe" such as HEAD, OPTIONS, or GET. Alternatively, strict validation of the Sec-Fetch-* request header fields can be used to ensure that the request did not originate from an inappropriate cross-origin call, a navigation request, or a resource load where this is not expected.
  • Confirm that separate applications are hosted on different hostnames to leverage the restrictions provided by same-origin policy, including how documents or scripts loaded by one origin can interact with resources from another origin and hostname-based restrictions on cookies.
  • Confirm that messages received by the postMessage interface are discarded if the origin of the message is not trusted, or if the syntax of the message is invalid.
  • Confirm that JSONP functionality is not enabled anywhere across the application to avoid Cross-Site Script Inclusion (XSSI) attacks.
  • Confirm that data requiring authorisation is not included in script resource responses, like JavaScript files, to prevent Cross-Site Script Inclusion (XSSI) attacks.
  • Confirm that authenticated resources can be loaded or embedded on behalf of the user only when intended. This can be accomplished by strict validation of the Sec-Fetch-* HTTP request header fields to ensure that the request did not originate from an inappropriate cross-origin call, or by setting a restrictive Cross-Origin-Resource-Policy HTTP response header field to instruct the browser to block returned content.
  • Check expiry, reuse and the binding of challenges to their intended action.
  • Follow successful authentication into the operation that trusts it.

How to fix it

Use precise origin policies and enforce independent protection for sensitive browser actions.

  • Apply consistent verification at trusted boundaries.
  • Protect enrolment, replacement and recovery as carefully as login.
  • Require appropriate fresh evidence for sensitive changes.
  • Record important authentication events without retaining secrets.

Authentication and authorisation answer different questions. Establishing identity does not grant unrestricted permission to records or privileged functions.

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 your APIs and integrations.

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