-
Notifications
You must be signed in to change notification settings - Fork 0
Add Content Security Policy support with layered iframe isolation #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add 'csp' to observedAttributes - Implement _buildCSP method that creates default CSP policies - Auto-detect provider and add appropriate origins (Gemini API) - Inject CSP meta tags into both coordinator and renderer iframes - Pass CSP configuration through the generation pipeline - Default CSP: Lock down all external resources, only allow inline scripts/styles and blob URLs Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
- Add csp attribute to attributes table - Document default CSP policy - Explain provider-specific CSP configuration - Add example of custom CSP usage Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
- Add proper HTML escaping for CSP content to prevent injection - Use robust regex pattern matching for HTML tag detection - Handle case-insensitive tags and various HTML structures - Add _escapeHtml helper method using native DOM API - Document why unsafe-inline/unsafe-eval are necessary for AI-generated content Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
- Cache built CSP in _currentCsp to reuse across generation calls - Avoid rebuilding CSP on every triggerGeneration call Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
|
@copilot Chrome supports csp on the iframe element (https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/csp) I would like to make sure that we use that as a priority, and if not available the add to the html. |
- Prioritize iframe csp attribute (Chrome/Edge support) - Set csp attribute before src assignment per spec - Fall back to CSP meta tag injection for unsupported browsers - Update README to document implementation approach Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
- Improve feature detection by setting and checking attribute value - Avoid memory leak by not creating unused blob URLs - Remove unsupported csp attribute when falling back to meta tags - CodeQL verification: 0 alerts Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
Implemented iframe The implementation:
The demo shows fallback in action since the current browser environment doesn't support the iframe |
|
@copilot only the co-ordinator should be able to talk to the LLM (google in this case), therefore that part of the CSP should not be sent through to inner iframe UNLESS it was also included in the csp attribute on the custom element. |
- Coordinator gets CSP with LLM API connect-src (Gemini/Chrome AI) - Renderer gets CSP with connect-src 'none' (blocks external connections) - Custom CSP attribute still applies to both iframes - Addresses security concern: only coordinator should access LLM API Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
- Cache both coordinator and renderer CSP to avoid redundant computation - Add comments explaining custom CSP applies to both layers (user control) - Clarify security boundary: renderer blocked from external APIs by default Co-authored-by: PaulKinlan <45510+PaulKinlan@users.noreply.github.com>
Implemented CSP separation in commits 7e380ea and b85990e. Changes:
Security boundary: Only the coordinator can access the LLM API. The renderer iframe is isolated from making any external connections unless explicitly allowed via custom CSP. |


Implements CSP enforcement for the double-iframe architecture to lock down AI-generated content. CSP is automatically configured based on the LLM provider and can be overridden via attribute. Includes security layering to isolate renderer from LLM API access.
Changes
Core Implementation
cspattribute: Optional override for custom policiesgenerativelanguage.googleapis.comfor Gemini, blocks external connections for Chrome AIcspattribute priority: Uses modern iframecspattribute (Chrome/Edge support) as primary enforcement methodcspattribute supportDefault Policies
Coordinator iframe (Layer 2) - Can access LLM API:
Renderer iframe (Layer 3) - Isolated from external APIs:
unsafe-inline/unsafe-evalrequired for unpredictable AI-generated code. Sandbox isolation (allow-scriptswithoutallow-same-origin) prevents parent/storage access.Security Measures
cspattribute set beforesrcper HTML specSecurity Layering
The coordinator iframe (Layer 2) needs to communicate with the LLM API, while the renderer iframe (Layer 3) displays AI-generated content. By applying different CSP policies:
This prevents AI-generated code from making unauthorized API calls or exfiltrating data.
Usage
Files Modified
src/generate-html.js: CSP builder with separate policies for coordinator vs renderer, cachingsrc/coordinator.js: iframe csp attribute with fallback for rendererREADME.md: DocumentationOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.