diff --git a/README.md b/README.md index 82576c2..9d1dbb8 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ Working from scratch? Create an untitled document and choose CSV or TSV as its l CSV supports VS Code 1.70+. Switching saved files between table and text requires 1.100+, or 1.120+ when there are unsaved changes. [Editing and compatibility details](docs/user-guide.md#open-and-edit-a-file). +CSV also works in [Restricted Mode](https://code.visualstudio.com/docs/editing/workspaces/workspace-trust) (untrusted workspaces). It never runs code from the workspace, and settings that reach the table view are sanitized, so a workspace's `.vscode/settings.json` can't inject content into it. + ## Your files stay in your workspace CSV requires no account or upload service and has no extension-specific analytics client. Processing runs in your VS Code environment, including the remote extension host when you use a remote workspace. [Privacy and data handling](PRIVACY.md). diff --git a/package.json b/package.json index fe0d1f7..998ff12 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,11 @@ "Programming Languages", "Other" ], + "capabilities": { + "untrustedWorkspaces": { + "supported": true + } + }, "activationEvents": [ "onStartupFinished", "onLanguage:csv", diff --git a/src/CsvEditorProvider.ts b/src/CsvEditorProvider.ts index 640c9f4..5f9a726 100644 --- a/src/CsvEditorProvider.ts +++ b/src/CsvEditorProvider.ts @@ -2567,7 +2567,12 @@ class CsvEditorController { visibleRowIds: number[]; filterMetadata: { session: string; filterColumnsRevision: number; filters: ColumnFilter[]; columns: FilterColumn[]; matchedRows: number; totalRows: number; maxFilters: number; revision: number; filterDraft?: FilterDraft }; }): string { - const { webview, nonce, fontFamily, fontSize, cellPadding, separator, tableHtml, chunksJson, extraColumnColorCss, nextChunkStart, hasRemoteChunks, headerEnabled, mouseWheelZoomEnabled, mouseWheelZoomInvert, columnSizes, frozenPanes, firstVisibleRow, realRowCount, visibleRowIds, filterMetadata } = args; + const { webview, nonce, separator, tableHtml, chunksJson, extraColumnColorCss, nextChunkStart, hasRemoteChunks, headerEnabled, mouseWheelZoomEnabled, mouseWheelZoomInvert, columnSizes, frozenPanes, firstVisibleRow, realRowCount, visibleRowIds, filterMetadata } = args; + // These come from settings, which an untrusted workspace can set to any JSON value. + // Sanitize them so they can't break out of the CSS or HTML they're interpolated into. + const fontFamily = CsvEditorController.sanitizeFontFamily(args.fontFamily); + const fontSize = CsvEditorController.normalizeFontSize(args.fontSize) ?? 14; + const cellPadding = CsvEditorController.normalizeCellPadding(args.cellPadding); const themeKind = vscode.window.activeColorTheme.kind; const isDark = themeKind === vscode.ColorThemeKind.Dark || themeKind === vscode.ColorThemeKind.HighContrast; const scriptUri = webview.asWebviewUri( @@ -2618,7 +2623,7 @@ class CsvEditorController { --csv-menu-background: #2d2d2d; --csv-menu-hover-background: #3d3d3d; } - body { font-family: ${this.escapeCss(fontFamily)}; font-size: ${fontSize}px; margin: 0; padding: 0; user-select: none; } + body { font-family: ${fontFamily}; font-size: ${fontSize}px; margin: 0; padding: 0; user-select: none; } .table-container { overflow: auto; height: 100vh; } table { border-collapse: collapse; width: max-content; } th, td { padding: ${cellPadding}px 8px; border: 1px solid var(--csv-table-border, ${isDark ? '#555' : '#ccc'}); font-size: inherit; } @@ -2647,7 +2652,7 @@ class CsvEditorController { display: none; align-items: stretch; color: #d4d4d4; - font-family: ${this.escapeCss(fontFamily)}; + font-family: ${fontFamily}; font-size: inherit; } #findReplaceWidget.open { display: flex; } @@ -2803,7 +2808,7 @@ class CsvEditorController { font-size: inherit; } #findReplaceWidget .fr-overflow-item:hover { background: rgba(255,255,255,0.05); } - #contextMenu { position: fixed; display: none; width: max-content; box-sizing: border-box; overflow: auto; overflow-wrap: anywhere; background: var(--csv-menu-background, ${isDark ? '#2d2d2d' : '#ffffff'}); border: 1px solid var(--csv-table-border, ${isDark ? '#555' : '#ccc'}); z-index: 10000; font-family: ${this.escapeCss(fontFamily)}; font-size: inherit; } + #contextMenu { position: fixed; display: none; width: max-content; box-sizing: border-box; overflow: auto; overflow-wrap: anywhere; background: var(--csv-menu-background, ${isDark ? '#2d2d2d' : '#ffffff'}); border: 1px solid var(--csv-table-border, ${isDark ? '#555' : '#ccc'}); z-index: 10000; font-family: ${fontFamily}; font-size: inherit; } #contextMenu div { padding: 4px 12px; cursor: pointer; } #contextMenu div:hover { background: var(--csv-menu-hover-background, ${isDark ? '#3d3d3d' : '#eeeeee'}); } @@ -3045,9 +3050,25 @@ class CsvEditorController { return ` title="${this.escapeHtml(text)}"`; } - private escapeCss(text: string): string { - // conservative; ok for font-family lists - return text.replace(/[\\"]/g, m => (m === '\\' ? '\\\\' : '\\"')); + // Font-family lists only need names, commas, spaces and quotes. Drop anything that could end + // the declaration or the '); + assert.ok(!html.includes('https://evil.example'), 'injected URL reached the page'); + assert.ok(!html.includes('/g)?.length, html.match(/