Skip to content

Commit e2cee7e

Browse files
committed
code: Derive CSP string from asExternalUri
In addition to running the preview uri from the server through `vscode.env.asExternalUri` we need to make sure that the CSP for the embedded iframe is given the correct origin.
1 parent a767743 commit e2cee7e

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

code/src/node/preview.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export class PreviewManager {
149149
vscode.env.asExternalUri(uri).then(
150150
extUri => {
151151
this.logger.debug(`${uri.toString(true)} -> asExternalUri -> ${extUri.toString(true)}`)
152+
153+
panel.webview.html = this.getWebViewHTML(`${extUri.scheme}://${extUri.authority}`)
152154
panel.webview.postMessage({ 'show': extUri.toString(true) })
153155
},
154156
err => {
@@ -193,18 +195,43 @@ export class PreviewManager {
193195
{ enableScripts: true, retainContextWhenHidden: true }
194196
)
195197

198+
// The webview will notify us when the page has finished loading.
199+
// Which should also mean the websocket connection is up and running.
200+
// Try and sync up the view to the editor.
201+
this.panel.webview.onDidReceiveMessage(message => {
202+
if (!message.ready) {
203+
return
204+
}
205+
206+
let editor = findEditorFor(this.currentUri)
207+
if (editor) {
208+
this.scrollView(editor)
209+
}
210+
})
211+
212+
this.panel.onDidDispose(() => {
213+
this.panel = undefined
214+
this.currentUri = undefined
215+
})
216+
217+
return this.panel
218+
}
219+
220+
private getWebViewHTML(origin: string): string {
221+
196222
let scriptNonce = getNonce()
197223
let cssNonce = getNonce()
224+
this.logger.debug(`Generating HTML for origin: ${origin}`)
198225

199-
this.panel.webview.html = `
226+
return `
200227
<!DOCTYPE html>
201228
<html>
202229
203230
<head>
204231
<meta charset="UTF-8">
205232
<meta name="viewport" content="width=device-width, initial-scale=1.0">
206233
<meta http-equiv="Content-Security-Policy"
207-
content="default-src 'none'; style-src 'nonce-${cssNonce}'; script-src 'nonce-${scriptNonce}'; frame-src http://localhost:*/" />
234+
content="default-src 'none'; style-src 'nonce-${cssNonce}'; script-src 'nonce-${scriptNonce}'; frame-src ${origin}/" />
208235
209236
<style nonce="${cssNonce}">
210237
* { box-sizing: border-box;}
@@ -336,7 +363,7 @@ export class PreviewManager {
336363
}
337364
338365
// Control messages coming from the webpage being shown.
339-
if (event.origin.startsWith("http://localhost:")) {
366+
if (event.origin.startsWith("${origin}")) {
340367
if (message.ready) {
341368
status.style.display = "none"
342369
noContent.style.display = "none"
@@ -349,27 +376,6 @@ export class PreviewManager {
349376
350377
</html>
351378
`
352-
353-
// The webview will notify us when the page has finished loading.
354-
// Which should also mean the websocket connection is up and running.
355-
// Try and sync up the view to the editor.
356-
this.panel.webview.onDidReceiveMessage(message => {
357-
if (!message.ready) {
358-
return
359-
}
360-
361-
let editor = findEditorFor(this.currentUri)
362-
if (editor) {
363-
this.scrollView(editor)
364-
}
365-
})
366-
367-
this.panel.onDidDispose(() => {
368-
this.panel = undefined
369-
this.currentUri = undefined
370-
})
371-
372-
return this.panel
373379
}
374380
}
375381

0 commit comments

Comments
 (0)