Skip to content

Commit f64e212

Browse files
committed
Secure website fetching and preview by adding HTML sanitization and private IP checks
└── apps └── editor ├── package.json └── src ├── extension.ts └── views └── prompt ├── backend │ ├── message-handlers │ │ ├── handle-open-website.ts │ │ ├── handle-paste-url.ts │ │ └── index.ts │ ├── prompt-view-provider.ts │ └── utils │ ├── persistent-text-storage.ts │ ├── symbols │ │ ├── replace-symbols.ts │ │ └── website │ │ └── replace-website-symbol.ts │ └── website-fetcher.ts ├── frontend │ ├── Main │ │ ├── Main.tsx │ │ └── MainView │ │ └── MainView.tsx │ ├── Prompt.tsx │ └── hooks │ └── use-prompt │ └── use-prompt.ts └── types └── messages.ts
1 parent c0dea3f commit f64e212

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎apps/editor/src/views/prompt/backend/message-handlers/handle-open-website.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as vscode from 'vscode'
22
import { OpenWebsiteMessage } from '../../types/messages'
33
import * as fs from 'fs'
44
import { marked } from 'marked'
5+
import { JSDOM } from 'jsdom'
6+
import createDOMPurify from 'dompurify'
57
import { PromptViewProvider } from '../prompt-view-provider'
68
import { PersistentTextStorage } from '../utils/persistent-text-storage'
79

@@ -44,13 +46,16 @@ export const handle_open_website = async (
4446

4547
if (id && fs.existsSync(file_path)) {
4648
const markdown = fs.readFileSync(file_path, 'utf8')
47-
const html = await marked.parse(markdown)
49+
const raw_html = await marked.parse(markdown)
50+
const window = new JSDOM('').window
51+
const DOMPurify = createDOMPurify(window as any)
52+
const html = DOMPurify.sanitize(raw_html)
4853

4954
const webview_panel = vscode.window.createWebviewPanel(
5055
'websitePreview',
5156
title,
5257
vscode.ViewColumn.One,
53-
{ enableScripts: true }
58+
{}
5459
)
5560

5661
webview_panel.iconPath = new vscode.ThemeIcon('globe') as any
@@ -104,11 +109,8 @@ export const handle_open_website = async (
104109
<html lang="en">
105110
<head>
106111
<meta charset="UTF-8">
107-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${csp} 'unsafe-inline' https://cdnjs.cloudflare.com; script-src 'unsafe-inline' https://cdnjs.cloudflare.com; font-src https://cdnjs.cloudflare.com;">
112+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${csp} 'unsafe-inline';">
108113
<meta name="viewport" content="width=device-width, initial-scale=1.0">
109-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/vs2015.min.css">
110-
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script>
111-
<script>document.addEventListener('DOMContentLoaded', () => hljs.highlightAll());</script>
112114
<title>${escaped_title}</title>
113115
<style>
114116
body {

‎apps/editor/src/views/prompt/backend/utils/website-fetcher.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import axios from 'axios'
22
import { JSDOM } from 'jsdom'
3+
import * as dns from 'dns/promises'
34
import { Readability, isProbablyReaderable } from '@mozilla/readability'
45
import createDOMPurify from 'dompurify'
56
import TurndownServiceJoplin from '@joplin/turndown'
@@ -86,11 +87,35 @@ const create_turndown_service = () => {
8687
return turndown_service
8788
}
8889

90+
const is_private_ip = (ip: string): boolean => {
91+
if (/^127\./.test(ip)) return true
92+
if (/^10\./.test(ip)) return true
93+
if (/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(ip)) return true
94+
if (/^192\.168\./.test(ip)) return true
95+
if (/^169\.254\./.test(ip)) return true
96+
if (/^0\./.test(ip)) return true
97+
if (/^255\.255\.255\.255$/.test(ip)) return true
98+
if (ip === '::1' || ip === '::') return true
99+
if (/^fe80:/i.test(ip)) return true
100+
if (/^fc[0-9a-f]{2}:/i.test(ip) || /^fd[0-9a-f]{2}:/i.test(ip)) return true
101+
return false
102+
}
103+
89104
export const fetch_website = async (
90105
url: string,
91106
abort_signal?: AbortSignal
92107
): Promise<{ content: string; title: string } | null> => {
93108
try {
109+
const parsed_url = new URL(url)
110+
if (parsed_url.protocol !== 'http:' && parsed_url.protocol !== 'https:') {
111+
return null
112+
}
113+
114+
const { address } = await dns.lookup(parsed_url.hostname)
115+
if (is_private_ip(address)) {
116+
return null
117+
}
118+
94119
const response = await axios.get(url, {
95120
headers: {
96121
'User-Agent':

0 commit comments

Comments
 (0)