Security

    Is unsafe-inline safe to use in AI-built WebViews?

    A 2026 view of an AI-generated Content Security Policy containing unsafe-inline in a mobile WebView, with injected inline script reaching a JavaScript bridge to native functions

    AI app builders reach for unsafe-inline because it makes things work without fuss: inline scripts and styles just run, no configuration needed. That convenience is exactly the problem. unsafe-inline switches off one of the main protections a Content Security Policy provides, and inside a mobile WebView that can turn a small content injection into access to the data and bridges your app exposes. So the honest answer is that it is not safe, and an AI-generated app that ships it is carrying a weakness it did not warn you about. Here is what the directive does and how to remove it.

    Short answer

    No, unsafe-inline is not safe to rely on, especially in a WebView. It tells the browser or WebView to execute inline scripts and styles, which disables the Content Security Policy protection against injected inline code, the core defense against cross-site scripting. Per OWASP's CSP guidance, you should avoid unsafe-inline and instead allow specific inline code with a nonce or hash. AI builders often add it by default to make generated code run without setup, so an AI-built app frequently ships it. In a WebView that loads any remote or user-influenced content, that opens a path to script injection, so the fix is to remove it and use nonces or hashes for the few inline scripts you actually need.

    What you should know

    • It disables a key CSP defense: unsafe-inline lets any inline script or style run.
    • XSS is the risk: without it blocked, injected inline code executes.
    • AI builders default to it: generated code often includes it for convenience.
    • WebViews raise the stakes: injected script can reach app data and JS bridges.
    • Nonces or hashes replace it: allow only specific inline code, not all of it.

    What does unsafe-inline actually do?

    It tells the WebView to trust and run inline scripts and styles, the kind written directly in the HTML rather than loaded from a file. A Content Security Policy normally blocks inline execution precisely because that is how cross-site scripting attacks run injected code, so a strong policy refuses inline scripts unless they carry a matching nonce or hash. Adding unsafe-inline removes that refusal, allowing every inline script to execute, including any an attacker manages to inject. So the directive does not add a feature; it removes a safeguard. The reason it shows up so often is that making inline code work is easier than configuring nonces, which is exactly the tradeoff an AI builder optimizes for when it generates something that simply runs.

    Why is it riskier in a mobile WebView?

    Because a WebView is usually wired into your app, not sandboxed like a random browser tab. The table contrasts the exposure.

    ContextWhat injected script can reach
    WebView with a JS bridgeNative functions the bridge exposes to the page
    WebView holding a sessionTokens or cookies available to the loaded content
    WebView loading remote contentAnything that content can touch in the app's web context
    Plain static local HTML, no inputLimited, but still a weakened policy

    In a WebView that exposes native capabilities through a JavaScript bridge or holds a user session, script injected because unsafe-inline is on does not just deface a page, it can call into what the bridge offers or read what the web context holds. The more your WebView is connected to the app, the worse a successful injection becomes, which is why a weakened CSP matters more here than on an isolated web page.

    How do you remove unsafe-inline safely?

    Replace it with a nonce or hash for the specific inline code you genuinely need, and move the rest into separate script files. A nonce is a random value you put on both the CSP header and the allowed inline script tag for each response, so only your intended script runs; a hash pins the policy to the exact contents of a known inline script. For most apps, the cleaner path is to avoid inline scripts altogether by loading logic from files, which lets your CSP omit inline permission entirely. Pair that with avoiding unsafe-eval, restricting script-src to sources you control, and not loading untrusted remote content into a privileged WebView. The goal is a policy that permits only what you defined, rather than anything inline.

    What to watch out for

    The first trap is trusting an AI-generated CSP as-is, since builders add unsafe-inline and unsafe-eval by default and label nothing; read the policy your app actually ships. The second is assuming a WebView is safe because it loads your own pages, when remote content, query parameters, or user input can still introduce injected script. The third is leaving a JS bridge broadly exposed to a WebView with a weak CSP, which compounds the risk. A pre-submission scan such as PTKD.com (https://ptkd.com) reads the compiled APK, AAB, or IPA against OWASP MASVS and checks WebView and network configuration, surfacing a weak Content Security Policy and insecure WebView settings so you can see what your AI-built app shipped before you submit. Tightening the CSP itself is the change you then make in the code.

    What to take away

    • unsafe-inline is not safe to rely on; it disables the CSP defense against injected inline scripts, the core protection against cross-site scripting.
    • AI builders often add it by default, so an AI-built app frequently ships it without warning.
    • In a WebView with a JS bridge or a session, injected script can reach native functions and app data, making the weakness worse than on an isolated page.
    • Remove it, use nonces or hashes for needed inline code, and use a pre-submission scan such as PTKD.com to find a weak CSP in your build.
    • #unsafe-inline
    • #content-security-policy
    • #webview
    • #xss
    • #ai-coded-apps
    • #csp
    • #owasp-masvs

    Frequently asked questions

    Is unsafe-inline safe in a WebView?
    No. unsafe-inline disables the Content Security Policy defense that blocks injected inline scripts, which is the core protection against cross-site scripting. In a WebView wired into your app through a JavaScript bridge or holding a session, injected script can reach native functions and app data, so the risk is higher than on an isolated web page. Remove it and allow only specific inline code with a nonce or hash.
    Why do AI builders add unsafe-inline?
    Because it makes generated code run without configuration. Allowing inline scripts and styles is easier than setting up nonces or hashes, so an AI builder optimizing to produce something that simply works often includes unsafe-inline, and unsafe-eval, by default. The cost is a weakened CSP that the generated code does not flag. So read the policy your AI-built app actually ships rather than assuming it is secure.
    What should I use instead of unsafe-inline?
    A nonce or a hash for the inline code you genuinely need. A nonce is a random value placed on both the CSP and the allowed inline script for each response, so only your intended script runs; a hash pins the policy to a known script's exact contents. Cleaner still is moving logic into separate script files so your CSP can omit inline permission entirely, paired with avoiding unsafe-eval.
    Is unsafe-inline fine if the WebView only loads my own pages?
    Not necessarily. Even pages you control can introduce injected script through remote content, query parameters, or user input rendered into the page. A WebView is also usually connected to your app, so a successful injection reaches more than a stray browser tab would. So do not treat your own pages as automatically safe; a weak CSP plus any injection path is still a real exposure worth closing.
    How do I find a weak CSP in my app?
    Scan the build. A pre-submission scan such as PTKD.com reads the compiled APK, AAB, or IPA against OWASP MASVS and checks WebView and network configuration, surfacing a weak Content Security Policy and insecure WebView settings. That shows you whether your AI-built app shipped unsafe-inline or an over-exposed JavaScript bridge, so you can tighten the policy in code before you submit rather than discovering it later.

    Keep reading

    Scan your app in minutes

    Upload an APK, AAB, or IPA. PTKD returns an OWASP-aligned report with copy-paste fixes.

    Try PTKD free