All articles

Technical

Configuring Content Security Policy (CSP) for TWA Apps

October 5, 2026 · 6 min read

A Trusted Web Activity (TWA) runs your Progressive Web App inside a fully functional, secure instance of Google Chrome. Because it uses the actual system browser engine rather than an isolated, feature-stripped WebView, the entire web platform security model remains fully active. For developers, this means that a robust Content Security Policy is not optional; it is actively enforced by the user's browser inside your Android wrapper. If your CSP is configured incorrectly, your Android application may suffer from silent breakages, blocked asset deliveries, or failed network requests.

Implementing a CSP for a TWA requires balancing maximum application security with the unique network and operational demands of a wrapped mobile application. This guide outlines how to configure a highly secure CSP that remains fully compatible with Android integrations, Digital Asset Links, and the runtime requirements of Google Play.

The Core Relationship Between CSP and TWAs

A Content Security Policy is a security mechanism delivered via an HTTP response header or a HTML meta tag. It restricts the resources (such as JavaScript, CSS, Images, and WebSockets) that the browser is allowed to load for a given page. When your PWA is opened through a TWA, Chrome reads these headers and restricts execution accordingly.

Unlike hybrid framework wrappers that load code from local storage using custom file protocols, a TWA loads your application directly from your web server over HTTPS. This architectural difference means your CSP works identically on the web and inside the mobile app shell. However, the mobile context introduces specific external integrations, such as payment gateways, analytics collection, push notification services, and deep linking, which must be explicitly white-listed to prevent application failure.

Essential CSP Directives for TWA Stability

To ensure your TWA works seamlessly while maintaining strong defense against cross-site scripting and data injection attacks, you must address several critical directives within your policy.

1. Script Sources

The script-src directive controls where executable scripts can be loaded from. In a modern PWA, you must avoid the use of unsafe inline scripts. If your TWA relies on external SDKs for analytics, customer support chat, or user state management, their domains must be explicitly defined here. For example, if you use Google Tag Manager or Firebase, their script endpoints must be permitted.

2. Connect Sources

The connect-src directive governs target URLs for APIs, WebSockets, and data fetches. If your TWA needs to communicate with external APIs, authentication servers, or synchronise databases via WebSockets, these domains must be specified. Blocking these causes immediate failure of login systems and dynamic content loading.

3. Image and Media Sources

Using img-src and media-src directs how visual assets load. Since mobile networks can be highly variable, many PWAs use content delivery networks to serve optimised image formats. Ensure all CDN subdomains are present in these directives.

Recommended CSP Directives for TWA Environments

DirectiveRecommended Source ConfigurationPurpose in TWA Apps
default-src'self'Acts as a fallback for other undefined fetch directives.
script-src'self' https://apis.google.comAllows local PWA scripts and critical Google authentication APIs.
connect-src'self' https://api.yourdomain.com wss://socket.yourdomain.comSecures backend REST API and WebSocket connection channels.
manifest-src'self'Permits loading of the Web App Manifest required for Android installation.
worker-src'self'Allows the Service Worker to run, which powers offline functionality.

Addressing the Digital Asset Links Exception

One common concern among web developers is whether the Digital Asset Links verification process requires specific entries in the CSP. When Google Play verifies the relationship between your Android package and your web domain, it requests the .well-known/assetlinks.json file directly from your server.

Because this verification request is initiated by the Android operating system at the system level rather than by the web browser instance, your web application's CSP headers do not affect the asset validation process. The Android OS reads the JSON file directly. Therefore, you do not need to modify your CSP to allow Google's verification crawlers to read your Asset Links file. However, your web server must still serve the JSON file with the correct MIME type over an active HTTPS connection.

How to Implement CSP Without Breaking Third-Party Redirects

TWA apps frequently redirect users to external authentication providers (such as Google OAuth, Apple ID, or Okta) or payment gateways (like Stripe or PayPal). When these external sites attempt to redirect the user back to your validated web scope, a strict CSP can sometimes intercept and block the transition if not correctly structured.

To prevent authentication loops and broken redirects:

  • Ensure that your form-action directive includes the domains of your third-party auth and payment providers. This allows your app to submit forms to external entities safely.
  • Use the frame-src directive if your payment providers load secure elements inside iframes (though opening external targets in a Custom Tab is highly recommended for security and UX).
  • Utilise modern token-based auth flows that exchange temporary tokens via standard query strings, rather than passing state through complex cross-document messaging schemes that might violate strict sandbox restrictions.

Debugging CSP Violations in Your TWA

When a CSP directive is violated inside your TWA, the browser will block the action, and the application might freeze or fail silently. Because there is no visible developer console on a physical mobile device, debugging these errors requires specific tools.

The most effective way to debug is via Chrome's remote debugging capabilities. Connect your physical Android test device to your development computer using a USB cable, enable USB debugging in the device's developer options, and open chrome://inspect on your desktop browser. This allows you to view the real-time Console logs of your running TWA. Any blocked assets or script execution blocks will be clearly logged as CSP violation errors with detailed source filenames and line numbers.

For production monitoring, it is highly recommended to implement the report-to or report-uri directives. These instruct the browser running inside your TWA to send JSON-formatted violation reports to a designated server endpoint whenever a security rule is triggered. This ensures you can identify and resolve edge-case asset blocks before they negatively impact your application rating in the Google Play Store.

Ready to ship your Android app?

Paste your PWA URL, get a signed APK and a Google Play ready AAB in minutes.

Build my app