Fix CSP Compliance - Unsafe Inline Scripts
Key Rules
- NEVER delete inline script content. Always extract it to an external
.jsfile and replace the inline<script>...</script>with<script src="filename.js"></script>. Even trivial config objects, debug flags, or seemingly unused code must be externalized — removal is a functional regression. - File naming: use a descriptive name matching the content's purpose (e.g.,
appConfig.jsfor configuration,init.jsfor initialization).
This skill fixes Content Security Policy (CSP) compliance issues that the UI5 linter detects but cannot auto-fix because they require restructuring code into external files.
Linter Rule Handled
When to Use
Apply this skill when you see linter output like:
Background: Why CSP Matters
Content Security Policy (CSP) is a security feature that helps prevent:
- Cross-Site Scripting (XSS) attacks
- Data injection attacks
- Unauthorized script execution
Inline scripts are considered unsafe because an attacker who manages to inject HTML can also inject malicious JavaScript. CSP-compliant apps use script-src 'self' which blocks inline scripts.
Documentation: Content Security Policy
Detection
The linter flags <script> tags that:
- Have NO
srcattribute AND - Have inline JavaScript content
Flagged:
Not flagged:
Fix Strategy
The fix for every inline script is the same: move it to an external file and add a <script src="..."> tag in its place. The content of the inline script is preserved — just in a separate .js file instead of inside the HTML. Never delete inline script content; always externalize it.
1. Basic Inline Script → External File
Problem: Inline JavaScript in HTML.
Fix Strategy: Move inline scripts to external files.
2. UI5 Bootstrap with Inline Init → data-sap-ui-on-init
Problem: Inline script after UI5 bootstrap.
Fix Strategy: Use data-sap-ui-on-init attribute.
3. Configuration Data → JSON or Module
Problem: Inline configuration object.
Fix Strategy A: External JSON file loaded at runtime.
Fix Strategy B: UI5 module with configuration.
4. Inline Event Handlers → External Scripts
Problem: Inline event handlers in HTML attributes.
Fix Strategy: Use external script with event listeners.
5. Test HTML Files
Problem: QUnit test files with inline scripts.
Fix Strategy: Use Test Starter (also fixes prefer-test-starter).
6. Dynamic Script Content
Problem: Script content generated dynamically.
Fix Strategy: Use data attributes or meta tags.
Implementation Steps
-
Identify all inline scripts from linter output
-
Categorize each script:
- UI5 initialization → Use
data-sap-ui-on-init - Configuration → External JS file
- Event handlers → External script with
addEventListener - Test boilerplate → Use Test Starter
- UI5 initialization → Use
-
Create external files for the script content
-
Update HTML to reference external files
-
Test the application to ensure functionality is preserved
Common Patterns
Notes
- CSP compliance is a warning (not error) because some environments may not require it
- The
data-sap-ui-on-initattribute acceptsmodule:path/to/moduleformat for AMD modules - For server-rendered dynamic values, use
<meta>tags ordata-*attributes - Test files should use Test Starter for both CSP compliance and best practices
- Some third-party libraries may require CSP adjustments - check their documentation
- JSONP callbacks may need special handling in CSP configurations


