@innovatorved/code-lab 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/ARCHITECTURE.md +4 -2
  2. package/CHANGELOG.md +25 -0
  3. package/README.md +15 -5
  4. package/dist/code-lab.css +52 -2
  5. package/dist/code-lab.d.ts +1 -1
  6. package/dist/code-lab.esm.js +1247 -377
  7. package/dist/code-lab.js +1246 -376
  8. package/dist/diagnostics.d.ts +5 -0
  9. package/dist/extensions/cpp.esm.js +51 -9
  10. package/dist/extensions/cpp.js +51 -9
  11. package/dist/extensions/python.esm.js +116 -103
  12. package/dist/extensions/python.js +116 -103
  13. package/dist/index.d.ts +1 -1
  14. package/dist/preview-sw.js +34 -17
  15. package/dist/runtime.esm.js +38 -31
  16. package/dist/runtime.js +38 -31
  17. package/dist/schemas.d.ts +1 -1
  18. package/dist/scm.d.ts +13 -2
  19. package/dist/search.d.ts +5 -1
  20. package/dist/shell.esm.js +72 -16
  21. package/dist/shell.js +72 -16
  22. package/dist/terminal.esm.js +183 -54
  23. package/dist/terminal.js +183 -54
  24. package/dist/workbench/index.d.ts +1 -1
  25. package/dist/workbench/styles.d.ts +1 -1
  26. package/dist/zip.d.ts +4 -3
  27. package/package.json +1 -1
  28. package/src/code-lab.css +52 -2
  29. package/src/csp.ts +7 -10
  30. package/src/diagnostics.ts +42 -3
  31. package/src/extensions/cpp.ts +47 -9
  32. package/src/extensions/python.ts +128 -116
  33. package/src/index.ts +1 -1
  34. package/src/preview-sw.js +25 -5
  35. package/src/runtime.ts +36 -29
  36. package/src/schemas.ts +1 -1
  37. package/src/scm.ts +106 -19
  38. package/src/search.ts +71 -27
  39. package/src/shell.ts +70 -14
  40. package/src/terminal.ts +181 -52
  41. package/src/workbench/diff-editor.ts +41 -13
  42. package/src/workbench/drawer.ts +17 -4
  43. package/src/workbench/guided-pane.ts +12 -2
  44. package/src/workbench/share.ts +37 -8
  45. package/src/workbench/styles.ts +2 -1
  46. package/src/workbench/toolbar.ts +15 -4
  47. package/src/workbench.ts +482 -79
  48. package/src/zip.ts +29 -7
@@ -4,6 +4,15 @@
4
4
 
5
5
  import { CL_ICONS } from './icons';
6
6
 
7
+ function escapeHtml(str: unknown): string {
8
+ return String(str ?? '')
9
+ .replace(/&/g, '&')
10
+ .replace(/</g, '&lt;')
11
+ .replace(/>/g, '&gt;')
12
+ .replace(/"/g, '&quot;')
13
+ .replace(/'/g, '&#39;');
14
+ }
15
+
7
16
  export interface DrawerTabOptions {
8
17
  workbench: any;
9
18
  drawerTab: string;
@@ -118,11 +127,15 @@ export function renderProblemsPanelHtml(workbench: any): string {
118
127
  ${items.map((item: any) => {
119
128
  const icon = item.severity === 'error' ? '✗' : (item.severity === 'warning' ? '⚠' : 'ℹ');
120
129
  const cls = item.severity === 'error' ? 'codelab-problem-item--error' : (item.severity === 'warning' ? 'codelab-problem-item--warning' : 'codelab-problem-item--info');
130
+ const safeFile = escapeHtml(item.file);
131
+ const safeMsg = escapeHtml(item.message);
132
+ const safeLine = Number(item.line) || 1;
133
+ const safeCol = Number(item.col) || 1;
121
134
  return `
122
- <div class="codelab-problem-item ${cls}" data-cl-problem-file="${item.file}" data-cl-problem-line="${item.line}" data-cl-problem-col="${item.col || 1}">
135
+ <div class="codelab-problem-item ${cls}" data-cl-problem-file="${safeFile}" data-cl-problem-line="${safeLine}" data-cl-problem-col="${safeCol}">
123
136
  <span style="font-weight:bold;">${icon}</span>
124
- <span>${item.message}</span>
125
- <span class="codelab-problem-item__location">${item.file}:${item.line}:${item.col || 1}</span>
137
+ <span>${safeMsg}</span>
138
+ <span class="codelab-problem-item__location">${safeFile}:${safeLine}:${safeCol}</span>
126
139
  </div>
127
140
  `;
128
141
  }).join('')}
@@ -131,7 +144,7 @@ export function renderProblemsPanelHtml(workbench: any): string {
131
144
  }
132
145
 
133
146
  export function renderStdinPanelHtml(workbench: any): string {
134
- const val = workbench.customStdin || '';
147
+ const val = escapeHtml(workbench.customStdin || '');
135
148
  return `
136
149
  <div class="codelab-stdin-panel">
137
150
  <div class="codelab-stdin-panel__header">
@@ -4,6 +4,15 @@
4
4
 
5
5
  import { CL_ICONS } from './icons';
6
6
 
7
+ function escapeHtml(str: unknown): string {
8
+ return String(str ?? '')
9
+ .replace(/&/g, '&amp;')
10
+ .replace(/</g, '&lt;')
11
+ .replace(/>/g, '&gt;')
12
+ .replace(/"/g, '&quot;')
13
+ .replace(/'/g, '&#39;');
14
+ }
15
+
7
16
  export interface GuidedStepData {
8
17
  id: string;
9
18
  title: string;
@@ -21,10 +30,11 @@ export function renderStepPillsHtml(steps: GuidedStepData[], activeIndex: number
21
30
  const isPassed = completedStepIds.has(step.id);
22
31
  const isActive = idx === activeIndex;
23
32
  const cls = `codelab-step-pill ${isActive ? 'active' : ''} ${isPassed ? 'passed' : ''}`;
33
+ const safeTitle = escapeHtml(step.title);
24
34
  return `
25
- <button type="button" role="tab" aria-selected="${isActive}" class="${cls}" data-cl-step="${idx}" title="Step ${idx + 1}: ${step.title}">
35
+ <button type="button" role="tab" aria-selected="${isActive}" class="${cls}" data-cl-step="${idx}" title="Step ${idx + 1}: ${safeTitle}">
26
36
  <span class="codelab-step-pill__num">${isPassed ? CL_ICONS.check : idx + 1}</span>
27
- <span class="codelab-step-pill__label">${step.title}</span>
37
+ <span class="codelab-step-pill__label">${safeTitle}</span>
28
38
  </button>
29
39
  `;
30
40
  }).join('')}
@@ -61,6 +61,8 @@ export async function compressWorkspaceSnapshot(payload: WorkspaceSharePayload):
61
61
  return bufferToBase64Url(rawBytes);
62
62
  }
63
63
 
64
+ const MAX_DECOMPRESSED_SNAPSHOT_BYTES = 5 * 1024 * 1024; // 5 MB
65
+
64
66
  /**
65
67
  * Decompresses a Base64url string into workspace snapshot payload.
66
68
  */
@@ -68,28 +70,55 @@ export async function decompressWorkspaceSnapshot(encoded: string): Promise<Work
68
70
  if (!encoded || typeof encoded !== 'string') return null;
69
71
  try {
70
72
  const bytes = base64UrlToBuffer(encoded);
73
+ if (bytes.byteLength > MAX_DECOMPRESSED_SNAPSHOT_BYTES) {
74
+ throw new Error('Snapshot payload exceeds maximum allowed size');
75
+ }
71
76
 
72
77
  if (typeof DecompressionStream !== 'undefined') {
73
78
  try {
74
79
  const ds = new DecompressionStream('deflate-raw');
75
80
  const writer = ds.writable.getWriter();
76
- writer.write(bytes as any);
77
- writer.close();
81
+ const writeInput = new Uint8Array(bytes.byteLength);
82
+ writeInput.set(bytes);
83
+ writer.write(writeInput).catch(() => {});
84
+ writer.close().catch(() => {});
78
85
 
79
- const response = new Response(ds.readable);
80
- const decompressed = await response.arrayBuffer();
86
+ const reader = ds.readable.getReader();
87
+ const chunks: Uint8Array[] = [];
88
+ let totalLength = 0;
89
+ while (true) {
90
+ const step = await reader.read();
91
+ if (step.done) break;
92
+ const chunk = step.value;
93
+ if (chunk) {
94
+ totalLength += chunk.byteLength;
95
+ if (totalLength > MAX_DECOMPRESSED_SNAPSHOT_BYTES) {
96
+ await reader.cancel().catch(() => {});
97
+ throw new Error('Decompressed snapshot exceeds 5 MB safety limit');
98
+ }
99
+ chunks.push(chunk);
100
+ }
101
+ }
102
+ const decompressed = new Uint8Array(totalLength);
103
+ let offset = 0;
104
+ for (const chunk of chunks) {
105
+ decompressed.set(chunk, offset);
106
+ offset += chunk.byteLength;
107
+ }
81
108
  const text = new TextDecoder().decode(decompressed);
82
- return JSON.parse(text);
109
+ return JSON.parse(text) as WorkspaceSharePayload;
83
110
  } catch (err) {
111
+ if (err instanceof Error && err.message.includes('safety limit')) {
112
+ throw err;
113
+ }
84
114
  // Fallback for uncompressed raw JSON payload
85
- void err;
86
115
  const rawText = new TextDecoder().decode(bytes);
87
- return JSON.parse(rawText);
116
+ return JSON.parse(rawText) as WorkspaceSharePayload;
88
117
  }
89
118
  }
90
119
 
91
120
  const rawText = new TextDecoder().decode(bytes);
92
- return JSON.parse(rawText);
121
+ return JSON.parse(rawText) as WorkspaceSharePayload;
93
122
  } catch (err) {
94
123
  if (typeof console !== 'undefined' && console.warn) {
95
124
  console.warn('[CodeLab Share Snapshot Restore Error]:', err);
@@ -35,7 +35,8 @@ export const WORKBENCH_STYLES = `
35
35
  padding: 4px 0;
36
36
  display: none;
37
37
  }
38
- .codelab-split-menu.open {
38
+ .codelab-split-menu.open,
39
+ .codelab-split-menu.active {
39
40
  display: block;
40
41
  }
41
42
  .codelab-split-menu__item {
@@ -4,6 +4,15 @@
4
4
 
5
5
  import { CL_ICONS } from './icons';
6
6
 
7
+ function escapeHtml(str: unknown): string {
8
+ return String(str ?? '')
9
+ .replace(/&/g, '&amp;')
10
+ .replace(/</g, '&lt;')
11
+ .replace(/>/g, '&gt;')
12
+ .replace(/"/g, '&quot;')
13
+ .replace(/'/g, '&#39;');
14
+ }
15
+
7
16
  export interface ToolbarRenderOptions {
8
17
  workbench: any;
9
18
  title: string;
@@ -42,7 +51,9 @@ export function renderLanguageSwitcherHtml(workbench: any): string {
42
51
  export function renderToolbarHtml(opts: ToolbarRenderOptions): string {
43
52
  const { workbench, title, backLabel, hasGuidedLab, mode, activeFile, hasWebContent, activeServerCount } = opts;
44
53
  const isGuided = Boolean(hasGuidedLab && mode === 'guided');
45
- const brandLabel = title || 'code-lab';
54
+ const brandLabel = escapeHtml(title || 'code-lab');
55
+ const safeBackLabel = escapeHtml(backLabel);
56
+ const safeActiveFile = escapeHtml(activeFile || 'active');
46
57
 
47
58
  const testFileExists = Boolean(
48
59
  workbench._findEntryFile && workbench._findEntryFile('test') !== null
@@ -52,9 +63,9 @@ export function renderToolbarHtml(opts: ToolbarRenderOptions): string {
52
63
  <div class="codelab-topbar codelab-toolbar">
53
64
  <div class="codelab-topbar__left">
54
65
  ${workbench.onClose ? `
55
- <button type="button" class="codelab-btn codelab-btn--back" data-cl-action="close-lab" title="${backLabel}">
66
+ <button type="button" class="codelab-btn codelab-btn--back" data-cl-action="close-lab" title="${safeBackLabel}">
56
67
  ${CL_ICONS.arrowLeft}
57
- <span>${backLabel}</span>
68
+ <span>${safeBackLabel}</span>
58
69
  </button>
59
70
  ` : ''}
60
71
  <span class="codelab-brand ${workbench.onClose ? 'codelab-brand--with-back' : ''}" title="${brandLabel}">
@@ -118,7 +129,7 @@ export function renderToolbarHtml(opts: ToolbarRenderOptions): string {
118
129
  <span>▶ Run Entry / Demo</span> <kbd style="opacity:0.6">⌘↵</kbd>
119
130
  </button>
120
131
  <button type="button" class="codelab-split-menu__item" data-cl-action="run-active-file">
121
- <span>▶ Run Active File (${activeFile || 'active'})</span> <kbd style="opacity:0.6">⇧⌘↵</kbd>
132
+ <span>▶ Run Active File (${safeActiveFile})</span> <kbd style="opacity:0.6">⇧⌘↵</kbd>
122
133
  </button>
123
134
  ${testFileExists ? `
124
135
  <button type="button" class="codelab-split-menu__item" data-cl-action="run-tests">