Skip to content

Widget: escape markdown text that renders to nothing - #571

Merged
neuromechanist merged 1 commit into
developfrom
fix/widget-markdown-raw-fallback
Sep 30, 2026
Merged

neuromechanist merged 1 commit into
developfrom
fix/widget-markdown-raw-fallback

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Security fix for the widget's markdown renderer, found by the second-pass review of the 0.8.16 release (#562).

The problem

  • markdownToHtml ended with return result || text. A reply that is only an opening fence renders no block, so its raw text went to innerHTML unescaped.
  • Input: a reply of the single line ```<img src=x onerror=alert(1)>. An HTML parse of the output gives an <img> with an onerror attribute.
  • The model, or a document that steers it, controls that text. Streaming can reach it transiently, because a chunk can end before the newline that would make the fence a code block.
  • The same line is on main, so production's widget has it today.

Also on main, already fixed in this release

On main, a < in an ordinary paragraph was passed through unescaped (the paragraph path's regex skips <), so Here is a tag: <img src=x onerror=alert(1)// also produced a live <img onerror>.
The paragraph path rewritten in #519 and #567 escapes it; this PR adds a test that pins that.

The fix

  • return result || escapeHtml(text).
  • Tests in frontend/test-markdown.js: the lone fence is shown escaped, a bare fence and an empty reply are unchanged, a < in a paragraph is escaped.

Testing

  • bun frontend/test-markdown.js: 31 passed; the two fence tests fail on the unfixed widget.
  • happy-dom parse of the rendered HTML for the two attack inputs and a plain one, on main, on develop and on this branch.

After release

Sites that embed the widget with a pinned integrity hash keep the old bytes until they re-pin to the release.

markdownToHtml ended with `return result || text`, so a reply that was
only an opening fence rendered no block and its raw text was returned as
HTML for innerHTML. A fence info string such as <img src=x onerror=...>
became a live element, and the model (or a document steering it) controls
that text; streaming could also reach it transiently. The text is now
escaped. The same file on main also passed a "<" in a paragraph through
unescaped; the paragraph path rewritten for this release already
escapes it, and a test now pins that too.

Tested: frontend/test-markdown.js 31 passed; the two new fence tests fail
on the unfixed widget. An HTML parse (happy-dom) of the output showed an
IMG with an onerror attribute for both inputs on main and for the fence
input on develop before this change, and none after.
@neuromechanist neuromechanist added security Security vulnerability or hardening widget Related to frontend widget labels Sep 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://fix-widget-markdown-raw-fall-demo.osc.earth
Branch fix/widget-markdown-raw-fallback
Commit 65713b8

This preview will be updated automatically when you push new commits.

@neuromechanist
neuromechanist merged commit b3ae2ff into develop Sep 30, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the fix/widget-markdown-raw-fallback branch September 30, 2026 23:23
neuromechanist added a commit that referenced this pull request Oct 1, 2026
…r check, long replies, docs (#573)

* Classify an Anthropic error that arrives mid-stream and langchain-aws's unsupported stream event

A stream that fails part way has already answered 200, so the SDK raises
APIStatusError with status_code 200 and the kind only in the body
(overloaded_error, rate_limit_error, ...). It classified as unknown: logged as an
unexpected error with a traceback and sent with no retryable. It now reads the
body's error type when the status says nothing.

langchain-aws's ValueError for a stream event it has no parser for was answered as
HTTP 400 'Invalid request' with the event dump; it is the provider's, so it now
classifies as an unavailable provider failure.

Tested: tests/test_core/test_model_errors.py 74 passed; the 10 new cases fail on the
old classifier. The mid-stream errors come from the SDK's own SSE decoder.

* Server: judge a streamed reply by its last run, keep long replies, record missing usage as NULL

Final review of release 0.8.16.

- A streamed reply was judged by the text of every model run, so a sentence written
  before a tool call made an empty last run look answered and the reader got a done
  holding only that sentence. It is now judged by the last run's text, as ModelRuns
  and the non-streamed paths already do; a last run cut off with no text is the
  'ran out of room' error, not a cut-off warning.
- A model's reply was held to the 10,000 characters a person may send, so a long reply
  (above all one cut off at the output limit) was refused after it was paid for: an
  error with no done when streamed, HTTP 500 when not. Replies may be 100,000
  characters (MAX_ASSISTANT_MESSAGE_LENGTH); the streamed branch that still refuses
  one now carries the request id and writes its metrics row.
- A non-streamed request with no reported usage recorded 0 tokens and a cost of 0.0;
  it now records NULL, as a streamed one does and as the warning says.
- A non-streamed /chat answered a provider's ValueError as HTTP 400 with the
  provider's text; it is now logged with a traceback and answered as the generic 500.
- A cut-off warning's metrics summary said 'with no answer' even with an answer.
- Comments and docstrings corrected: the single-warning-element rationale (pinned
  embeds only), the SSE event lists, the Bedrock-era routing notes.

Tested: the new cases fail on the old router and pass on this one. Full suite with
the local-environment causes removed: see the pull request.

* Widget: show the error reference on a non-streamed 502, fix stale comments

A 502 for an empty or cut-off reply carries error_id and request_id in its JSON body.
A streamed error showed the reference with a copy button; the non-streamed response
path (streamingEnabled: false, or a non-SSE answer) dropped it. responseError now
carries it on the error, so the persistent banner shows the same reference.

Comments: a cut-off reply with no text and no code is an error from this server, not
a warning plus done; 'centred' to 'centered'; the SSE event list in the header now has
warning (with codes), error (error_id, request_id, retryable) and session.

Tested: frontend/test-widget-paced-streaming.js 257 passed; the new reference checks
fail on the old widget.

* Test: the Bedrock deployment log checks cannot pass when nothing is logged

The loop over the logged records had no non-empty check, so a case that should log a
community could log nothing and still pass. Both cases now assert the logged community
ids equal the ids derived from the registry.

Tested: tests/test_api/test_bedrock_deployments.py passes.

* Docs: retitle the changelog 0.8.16, add the review-round changes and a security entry, fix the fallback wording

CHANGELOG: [Unreleased] becomes [0.8.16] with a new empty [Unreleased] above it.
Entries for what the two review rounds shipped and the changelog lacked: an empty or
cut-off reply is now an error or warning (HTTP 502 when not streamed, which a client
that retries on 5xx will re-bill), classified stream failures, the unanswered message
kept in the session, missing-usage logging, the CLI and widget changes, and the metrics
rows of failed streams. A Security entry for #571 (unescaped model text in innerHTML,
present in the 0.8.15 widget; pinned embeds stay exposed until re-pinned). Entries for
this round's own fixes. The Luna fallback is described as the code does it: the
deployment's Claude default with an error logged when the Bedrock key is missing, a
warning for a caller's own key, OpenRouter or HTTP 500 with no ANTHROPIC_API_KEY.
Luna is high by default, not never at maximum; the 27 s and 48 s figures are labeled.

Also ADR 0014 and 0015, the ADR index titles, .context/api-structure.md (pointer at the
top, fallback wording), the community config comments, deploy/Dockerfile, the
bedrock_region description and two docstrings in core/config/community.py. Comments and
documentation only; no config value changed.

Tested: ruff check and format; the config and routing tests pass.

* Review follow-ups: test the streamed session-limit branch, align the widget's reply bound, one usage rule

- A test for the streamed branch that still refuses a reply over the model limit: the
  error carries the request id, one 500 row is written with an error_message, the cost
  is kept, and nothing is stored. Removing the request id or the metrics call now fails it.
- The widget's history check dropped a reply of exactly 100,000 characters, which the
  server keeps; it now accepts up to and including that length.
- A non-streamed request's row is NULL when input plus output tokens are zero, the rule
  the streamed row and ModelRuns use, not the total field.
- Docstrings: reply_problem's reply_text is the last run's text; _select_model names the
  platform's OpenRouter fallback.
- Removed a duplicate TestRetiredEnvironmentVariables from test_bedrock_routing.py
  (tests/test_api/test_config.py has it); the hermetic fixture stays.

Tested: the affected Python files (402 passed) and the widget suites.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Security vulnerability or hardening widget Related to frontend widget

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant