Repository navigation
Repl: send rich text as structured data instead of HTML - #1023
Akshay-2007-1 wants to merge 7 commits into
Conversation
The repl bundle builds rich-text output inside the evaluator worker, next to the student's program, and the tab inserted it with dangerouslySetInnerHTML on the main page. Check each entry in the tab before inserting it: it must be a single <span style="...">text</span> using only the styles the bundle can emit, with no markup characters in the text. Anything else is shown as plain text. Output from rich_repl_display is unchanged.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@martin-henz, mergable? |
The repl bundle runs in the evaluator worker next to student code, but its richtext entries were HTML strings that the Repl tab inserted on the main page with dangerouslySetInnerHTML (guarded only by a regex allowlist). Rich entries now carry `segments`: a list of text spans (text plus bold/italic/underline, fontSizePx 14/20/25/50, #rrggbb color and backgroundColor) and line breaks. rich_display.ts builds a span from the pair structure (innermost style wins on conflicts, matching the old CSS declaration order), and the two no-evaluator messages are defined as segments in protocol.ts. The tab re-validates every segment, builds the style object itself and renders text as text nodes; the HTML allowlist and dangerouslySetInnerHTML are removed. The bundle's existing input checks on rich text are kept unchanged.
Tabs run on the main Source Academy page and display data from bundles running alongside student code. Ban dangerouslySetInnerHTML and assignment to innerHTML/outerHTML under src/tabs/**.
- Decode numeric and common named character references in rich text, which the tab showed decoded when it inserted HTML (< was the only way to show <). - If an older cached bundle sends rich text as an HTML string, show it as plain text instead of an empty line; coerce plain-text content to a string. - Move entry rendering into renderOutputEntry and test it. - Lint: also ban innerHTML/outerHTML/dangerouslySetInnerHTML object keys and insertAdjacentHTML, createContextualFragment, document.write(ln), setHTMLUnsafe.
…electors - An output message whose entry is not an object is dropped instead of crashing the tab's render. - document.write/writeln only (not any .write()), and destructuring an innerHTML property is no longer flagged.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b65f7fc2a6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
- Rich-text entries also carry the same output as HTML in content, with every text escaped, for a Repl tab from before segments (e.g. a cached tab asset next to a newer bundle). Current tabs render segments and ignore it. - The tab tests use react-dom/server, so the tab now lists @types/react-dom like the other tabs that do (tsc failed in CI, where only the tab's own dependencies are installed).
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Shrey5132
left a comment
There was a problem hiding this comment.
Reviewed structured rich-text production, tab-side validation and React rendering, style precedence, entity decoding, and both mixed-version paths. The new legacy content field addresses the earlier cached-tab finding while escaping all text. No blocking findings.
Local validation: immutable install; 62 bundle tests and 35 tab tests pass; Repl tab typecheck, bundle/tab lint and tab build pass. GitHub CI is green. No interactive browser test was run.
AaravMalani
left a comment
There was a problem hiding this comment.
Mostly LGTM, I'd rather a couple minor things be fixed though
| }); | ||
| } | ||
|
|
||
| function escapeHtml(text: string): string { |
There was a problem hiding this comment.
[Nit] If you have time, I think a cleaner approach would be to keep the bundle frontend-agnostic, so the HTML escaping takes place on the tab side.
| // Tabs run on the main Source Academy page, while the data they display comes from bundles | ||
| // running alongside student code. Build elements with React instead of inserting HTML strings. | ||
| // Note: this is the only no-restricted-syntax setting for tabs; a later block that sets the | ||
| // same rule for these files replaces these entries, so add to this list instead. |
There was a problem hiding this comment.
Add this to the lint plugin, please
| // Entries come from the worker; one that is not an object would crash rendering. | ||
| if (typeof message.entry === 'object' && message.entry !== null) { | ||
| this.__setState({ outputs: [...this.__state.outputs, message.entry] }); |
There was a problem hiding this comment.
Normalize output entries at the channel boundary
This guard admits arbitrary objects into state typed as ReplOutputEntry, leaving validation and legacy normalization to rendering. Because editor updates rerender the output history, existing segments are repeatedly sanitized. Decode the entry once here into a validated view model, including the legacy plaintext fallback, and let the renderer consume that type. This also avoids the test helper's as any cast.
What
rich_repl_displayoutput is built by the repl bundle, which runs in the evaluator worker alongside the student's program. The Repl tab used to insert that output on the main page withdangerouslySetInnerHTML, and the only check (xssStringCheck) ran in the worker.The bundle now sends rich text as structured data instead of HTML, and the tab builds the elements with React, so no HTML from the worker is ever parsed:
protocol.ts: a rich-text entry is a list of segments,{ kind: 'text', text, bold?, italic?, underline?, fontSizePx?: 14 | 20 | 25 | 50, color?, backgroundColor? }or{ kind: 'break' }. The no-evaluator spacer and warning are segment lists too.rich_display.ts: produces a segment instead of a<span style>string (same styles, same "innermost wins" rule for conflicts). Common character references (<,>,&,",', , numeric) are decoded, since the tab used to show them decoded.richText.tsx: re-validates every segment (6-digit hex colours, the four font sizes, boolean flags only; anything else is dropped) and renders text as text nodes. Output messages without an entry object are ignored rather than crashing the tab.dangerouslySetInnerHTML,innerHTML/outerHTMLassignment,insertAdjacentHTML,createContextualFragment,setHTMLUnsafeordocument.write. No existing tab uses any of them.Behaviour
Output looks the same as before. I compared old and new side by side in a browser (dev server, real bundle code feeding the real tab): all styles alone and combined, colours, line breaks, quotes/ampersands and the
set_evaluatorwarning have identical text, computed styles and heights. Text from the worker that contains markup is shown literally.Left: old rendering (HTML inserted by the tab, as on master). Right: the new tab rendering the same output from structured segments.
Known differences:
©) now show as typed.content, with every text escaped, so an older cached tab still shows styled output from the new bundle (current tabs ignore it). If an older cached bundle runs with the new tab, its rich text shows as plain text (markup visible) until the bundle refreshes.Testing
tscandlintpass for the bundle and the tab;lint:tabshas no errors across all tabs.Deployment note
This fixes the tab for programs that load modules through Conductor, from this repository's build. A deployment that serves modules for the Source chapters (through js-slang) from an older, separately built copy of this repository is affected until that copy is rebuilt from a version that includes this change.