Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Alpine.js Content Security Policy (CSP) Mode Specification
Summary
This document specifies the design and implementation of Alpine.js's Content Security Policy (CSP) compliant mode, which allows Alpine.js to function in environments where
unsafe-evalis prohibited. The CSP mode provides a subset of Alpine's functionality through a custom expression parser that avoids runtime code evaluation while maintaining the developer experience that makes Alpine attractive.1. Background and Motivation
1.1 The Problem
Alpine.js's core appeal lies in its ability to write JavaScript expressions directly in HTML attributes:
These expressions are currently evaluated at runtime using
new Function()or similar constructs, which violates Content Security Policy directives that prohibitunsafe-eval. This limitation prevents Alpine.js adoption in:1.2 Current Limitations
The existing CSP implementation is overly restrictive, only supporting:
This severely limits Alpine's utility and developer experience.
2. Design Goals
2.1 Primary Goals
eval(),new Function(), or any dynamic code evaluation2.2 Non-Goals
3. Supported Expression Types
3.1 Core Expression Categories
3.1.1 Literals
3.1.2 Object Literals
3.1.3 Array Literals
3.1.4 Variable and Property Access
3.1.5 Function Calls
3.1.6 Basic Operators
3.1.7 Conditional Expressions
3.2 Context-Specific Support
3.2.1 x-data
3.2.2 x-on (Event Handlers)
3.2.3 x-show/x-if
3.2.4 x-text/x-html
3.2.5 x-model
4. Parser Implementation Strategy
4.1 Architecture
4.2 Parser Phases
4.3 AST Node Types
Supported node types:
5. Error Handling
5.1 Error Categories
5.1.1 Parsing Errors
5.1.2 Unsupported Feature Errors
5.2 Error Message Format
6. Migration Guide
6.1 Common Patterns and Alternatives
Pattern: Inline Methods
Pattern: Complex Conditionals
6.2 CSP Mode Activation
7. Performance Considerations
7.1 Parsing Cache
7.2 Optimization Strategies
8. Security Considerations
8.1 Scope Isolation
8.2 Input Validation
9. Testing Strategy
9.1 Test Categories
9.2 Test Coverage Goals
10. Documentation Requirements
10.1 User Documentation
10.2 Developer Documentation
11. Future Considerations
11.1 Potential Enhancements
11.2 Community Feedback Integration
12. Decision Points
12.1 Open Questions Requiring Decision
Constructor Calls: Should
new Date()be supported?Increment/Decrement: Should
count++be supported?Template Literals: Should backtick strings be supported?
Method Definitions: Should object method shorthand be supported?
Error Strictness: How strict should parsing be?
13. Implementation Roadmap
Phase 1: Core Functionality (MVP)
Phase 2: Enhanced Support
Phase 3: Advanced Features
14. Success Metrics
Appendix A: Complete Expression Support Matrix
Legend: ✅ Supported | ❌ Not Supported | 🔄 Under Consideration
Appendix B: Example Error Messages
Appendix C: Security Threat Model
Attack Vectors Considered
Security Boundaries