Skip to content

fix: restrict HTML preview network access - #574

Open
Soumyajit2288 wants to merge 1 commit into
wonderwhy-er:mainfrom
Soumyajit2288:agent/html-preview-csp
Open

fix: restrict HTML preview network access#574
Soumyajit2288 wants to merge 1 commit into
wonderwhy-er:mainfrom
Soumyajit2288:agent/html-preview-csp

Conversation

@Soumyajit2288

@Soumyajit2288 Soumyajit2288 commented Jul 11, 2026

Copy link
Copy Markdown

Summary

  • inject a restrictive Content Security Policy into rendered HTML preview documents
  • preserve inline scripts and styles used by interactive local previews
  • block network connections, form submissions, external resources, and base-URL rewriting
  • add a renderer-level regression for the escaped iframe srcdoc

Security boundary

Preview content is attacker-controlled HTML. The iframe sandbox isolates its origin but does not prevent fetch, XHR, WebSocket, form, image, or script requests. The CSP now limits scripts and styles to inline preview content, images/media to data: and blob:, and denies outbound connections and form actions.

Fixes #507.

Validation

  • npm run build
  • node test/test-html-preview-csp.js
  • git diff --check

Summary by CodeRabbit

  • Security Enhancements

    • Added a restrictive Content Security Policy to sandboxed HTML previews.
    • Prevented preview content from making network connections or submitting forms.
  • Bug Fixes

    • Preserved existing fallback behavior, displaying source content if preview rendering fails.
  • Tests

    • Added coverage to verify security restrictions are applied to rendered HTML previews.

@Soumyajit2288

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 75b12f2b-e465-48dc-a60f-1989680435d0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

HTML previews now include a restrictive Content-Security-Policy meta tag in their sandboxed iframe documents. A new test verifies CSP directives, sandboxing, and transformed script output.

Changes

HTML Preview Security

Layer / File(s) Summary
CSP injection and validation
src/ui/file-preview/src/components/html-renderer.ts, test/test-html-preview-csp.js
Defines a restrictive CSP, injects it into generated preview documents, and verifies the directives, sandbox attribute, and escaped script output.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title concisely describes the main change: tightening HTML preview network access.
Linked Issues check ✅ Passed The CSP meta tag in html-renderer.ts and the regression test align with issue #507's requirements.
Out of Scope Changes check ✅ Passed The changes are limited to the renderer and its test, both directly related to the CSP fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/ui/file-preview/src/components/html-renderer.ts (1)

10-10: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider using the shared escapeHtml from src/ui/shared/escape-html.ts.

The escapeHtml import from ./highlighting.js appears functionally equivalent to the shared helper in src/ui/shared/escape-html.ts (both escape &, <, >, ", '). Using the shared module would improve consistency and reduce duplication across the codebase. This is a nice-to-have and can be deferred.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/ui/file-preview/src/components/html-renderer.ts` at line 10, Update the
html-renderer import to use the shared escapeHtml helper from the shared module
instead of the local highlighting export, preserving all existing call sites and
behavior.
test/test-html-preview-csp.js (1)

6-10: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add assertions for remaining CSP directives to prevent silent regressions.

The test covers connect-src and form-action but not default-src 'none', script-src 'unsafe-inline', style-src 'unsafe-inline', base-uri 'none', img-src, or media-src. Adding assertions for these would ensure all directives are locked in and prevent accidental removal in future edits.

♻️ Suggested additional assertions
 assert.ok(rendered.html.includes('Content-Security-Policy'));
+assert.ok(rendered.html.includes('default-src &`#39`;none&`#39`;'));
+assert.ok(rendered.html.includes('script-src &`#39`;unsafe-inline&`#39`;'));
+assert.ok(rendered.html.includes('style-src &`#39`;unsafe-inline&`#39`;'));
+assert.ok(rendered.html.includes('base-uri &`#39`;none&`#39`;'));
 assert.ok(rendered.html.includes('connect-src &`#39`;none&`#39`;'));
 assert.ok(rendered.html.includes('form-action &`#39`;none&`#39`;'));
 assert.ok(rendered.html.includes('sandbox="allow-scripts allow-forms allow-popups"'));
 assert.ok(rendered.html.includes('&lt;script&gt;fetch(&quot;https://example.com&quot;)&lt;/script&gt;'));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/test-html-preview-csp.js` around lines 6 - 10, Add assertions in the
HTML preview CSP test alongside the existing checks to verify the rendered
policy includes default-src 'none', script-src 'unsafe-inline', style-src
'unsafe-inline', base-uri 'none', img-src, and media-src. Match the rendered
HTML’s existing escaping format and preserve the current assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/ui/file-preview/src/components/html-renderer.ts`:
- Line 10: Update the html-renderer import to use the shared escapeHtml helper
from the shared module instead of the local highlighting export, preserving all
existing call sites and behavior.

In `@test/test-html-preview-csp.js`:
- Around line 6-10: Add assertions in the HTML preview CSP test alongside the
existing checks to verify the rendered policy includes default-src 'none',
script-src 'unsafe-inline', style-src 'unsafe-inline', base-uri 'none', img-src,
and media-src. Match the rendered HTML’s existing escaping format and preserve
the current assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1ca3f9ef-ca90-4189-9c3e-e9ab270aa399

📥 Commits

Reviewing files that changed from the base of the PR and between 78100e6 and 7ab3927.

📒 Files selected for processing (2)
  • src/ui/file-preview/src/components/html-renderer.ts
  • test/test-html-preview-csp.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Content-Security-Policy to the HTML-preview iframe

1 participant