Skip to content

Repl: send rich text as structured data instead of HTML - #1023

Open
Akshay-2007-1 wants to merge 7 commits into
source-academy:masterfrom
Akshay-2007-1:fix/repl-richtext-allowlist
Open

Akshay-2007-1 wants to merge 7 commits into
source-academy:masterfrom
Akshay-2007-1:fix/repl-richtext-allowlist

Conversation

@Akshay-2007-1

@Akshay-2007-1 Akshay-2007-1 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What

rich_repl_display output 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 with dangerouslySetInnerHTML, 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 (&lt;, &gt;, &amp;, &quot;, &apos;, &nbsp;, numeric) are decoded, since the tab used to show them decoded.
  • Tab 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.
  • ESLint: tabs may not use dangerouslySetInnerHTML, innerHTML/outerHTML assignment, insertAdjacentHTML, createContextualFragment, setHTMLUnsafe or document.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_evaluator warning 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.

REPL output, old vs new
REPL output, old vs new, continued

Known differences:

  • Only the character references listed above are decoded; others (e.g. &copy;) now show as typed.
  • Mixed versions keep working. Each rich-text entry also carries the same output as HTML in 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

  • Bundle: 62 tests (segment output for every style, conflicts, reference decoding, rejected input, escaped HTML for older tabs).
  • Tab: 35 tests (segment validation, rendered markup for rich/plain/legacy entries, the no-evaluator warning, hostile segments, entries that are not objects).
  • tsc and lint pass for the bundle and the tab; lint:tabs has 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.

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.
@Akshay-2007-1
Akshay-2007-1 marked this pull request as ready for review December 3, 2026 13:04
@martin-henz

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T00:25:23.786308Z 65b040a Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: e3115ed40c

ℹ️ 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".

@Akshay-2007-1

Copy link
Copy Markdown
Contributor Author

@martin-henz, mergable?
Codex didnt find any issues!

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 (&lt; 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.
@Akshay-2007-1 Akshay-2007-1 changed the title Repl tab: only render rich text in the shape the bundle produces Repl: send rich text as structured data instead of HTML Dec 3, 2026
@Akshay-2007-1

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/bundles/repl/src/index.ts Resolved
- 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).
@Akshay-2007-1

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 65b040ab0a

ℹ️ 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".

@Shrey5132 Shrey5132 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AaravMalani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, I'd rather a couple minor things be fixed though

});
}

function escapeHtml(text: string): string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Comment thread eslint.config.js
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this to the lint plugin, please

Comment on lines +169 to +171
// 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] });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants