5 min read

Securing a browser-based trading UI: CSP, CORS and entitlements

What CSP and CORS actually protect in a web trading front end, the WebSocket gap that CORS does not cover, and why entitlements and order validation belong on the server whatever the client does.


A web trading front end is a page that holds a live session with the authority to send orders, renders data from several internal services, and is built from a few hundred third-party packages. The threat model follows directly: script running in that page can trade as the user, and a different page in the same browser can try to borrow the session.

CSP, CORS and server-side authorisation each address a different part of that. They are often described loosely, and the loose versions lead to real holes, so this note is precise about what each one does.

CSP limits what the page can execute and contact

Content Security Policy is the control against injected script. A policy worth having for an application like this does not list trusted hosts for scripts; it trusts specific script elements by nonce:

Content-Security-Policy:
  default-src 'none';
  script-src 'nonce-{random-per-response}' 'strict-dynamic';
  style-src 'self';
  img-src 'self' data:;
  font-src 'self';
  connect-src 'self' wss://stream.example.com;
  frame-ancestors 'none';
  base-uri 'none';
  form-action 'none';
  require-trusted-types-for 'script'

The parts that matter most on a trading UI:

  • script-src with a nonce and 'strict-dynamic'. Only scripts the server stamped with this response's nonce run, plus what those scripts load. Inline event handlers and injected <script> tags do not. Host allowlists are weaker: any JSONP endpoint or old library on an allowed host is a bypass.
  • connect-src. This governs fetch, XHR, EventSource and WebSocket destinations. If an attacker does get script to run, a tight connect-src is what stops positions, orders and tokens being sent somewhere else. It is the exfiltration control and it is frequently left as *.
  • frame-ancestors 'none'. An order ticket that can be framed can be clickjacked.
  • Trusted Types. require-trusted-types-for 'script' makes assignments to sinks such as innerHTML throw unless the value went through a declared policy. It turns DOM XSS from something reviewers must spot into something the browser refuses. Check support against the browser build your desks actually run.

Roll a policy out with Content-Security-Policy-Report-Only first and read the reports; grids and charting libraries are where the surprises are.

CORS is not a defence

CORS is regularly described as protection against cross-site attacks. It is the opposite: the same-origin policy is the protection, and CORS is a controlled way to relax it. A server that sends no CORS headers is at its most restrictive.

Two consequences:

  • CORS controls who can read a response, not whether the request is sent. A "simple" cross-origin POST reaches the server and is executed, with cookies if SameSite allows; the calling page just cannot read the reply. State-changing endpoints need their own defence: SameSite cookies, a required custom header or content type that forces a preflight, or a CSRF token.
  • The dangerous configuration is reflecting the Origin header back with Access-Control-Allow-Credentials: true. It is what you get from a permissive CORS middleware default, and it lets any site read authenticated responses. Access-Control-Allow-Origin: * cannot be combined with credentials, which is why the reflection shortcut gets written. Compare against an explicit list of origins.

WebSockets are outside CORS

This is the gap that matters most for a streaming UI. The WebSocket handshake is not subject to CORS. Any page on any origin can open wss://stream.example.com, and the browser will attach that host's cookies to the handshake. If the server authenticates the socket by cookie alone, a malicious page gets a live, authenticated stream of the user's prices, orders and positions. This is cross-site WebSocket hijacking.

The server has to do the work:

  • Validate the Origin header in the upgrade request against an allowlist and reject everything else. Browsers always send it and page script cannot forge it.
  • Do not rely on ambient cookies for the socket. Authenticate with a short-lived, single-use ticket obtained over an authenticated HTTPS call, sent as the first message after connect.
  • Keep credentials out of the URL. The browser WebSocket API cannot set request headers, so tokens end up in query strings, and from there in proxy and access logs.
  • Re-authorise during the session. A socket opened at 07:00 is still open at 17:00. Expire it, and re-check permissions when they change.

Entitlements are enforced where the data leaves

Hiding a column or disabling a button is presentation. Whether a user may see a venue's depth, another desk's positions or a client's axes is decided by the server on every subscription, per instrument and per field, because market-data licences and information barriers are obligations and the browser is not a place to meet them. If the data reached the client, it has been disclosed, whatever the grid renders.

The same applies in the other direction. An order arriving from the UI is untrusted input from an authenticated user: the server validates instrument, side, quantity and price against that user's permissions and limits, and applies the same pre-trade controls as any other order source. Client-side validation exists for the user's convenience. Fat-finger checks, limits and four-eyes approvals that exist only in JavaScript do not exist.

Tokens and the supply chain

Two more points decide how bad an XSS or a compromised dependency would be.

Where the session lives. A token readable from JavaScript (in localStorage or a non-HttpOnly cookie) can be copied and used from elsewhere. An HttpOnly, Secure, SameSite cookie cannot be read, though injected script can still act through the page while it is open. Short lifetimes, binding to the client where possible, and step-up authentication for sensitive actions limit the window.

What runs in the page. A trading UI's dependency tree is its largest attack surface. Lockfiles with integrity hashes, a private registry or proxy, reviewed updates instead of floating ranges, and no third-party scripts loaded at runtime from someone else's CDN. The CSP above is the backstop: a compromised package can still run, but it cannot load more code from an unlisted host or send data to one.

Summary

CSP restricts what executes and where the page may connect. The same-origin policy isolates the page, and CORS only loosens it. WebSockets get neither for free, so the server checks Origin and authenticates the socket explicitly. Everything that decides what a user can see or do is enforced server-side, on the assumption that the client is hostile, because on a bad day it is.