Skip to content

fix(security): harden prop and URL validation against bypasses - #373

Merged
farnabaz merged 18 commits into
mainfrom
fix/security-prop-url-validation
Aug 25, 2026
Merged

fix(security): harden prop and URL validation against bypasses#373
farnabaz merged 18 commits into
mainfrom
fix/security-prop-url-validation

Conversation

@atinux

@atinux atinux commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

What

Stacks on #371's base (branch fix/html-attribute-escaping, PR pending). Rejects markdown-authored innerHTML/dangerouslySetInnerHTML/textContent props, holds the as prop to the same tag filters, and closes four URL-validation bypasses: entity stripping, scheme-relative //host URLs, lookalike-host prefix matches, and JSON-quoted or dot-path bindings validated only in their unresolved form.

Why

A security scan showed the plugin's documented guarantees were defeatable: ::div{innerHTML="<img …>"} executed in all four framework renderers even with the sanitizer enabled, javascript&#58;alert(1) survived because entities were stripped instead of decoded, and :href="frontmatter.home" could resolve to javascript: after parse-time checks had run. The render-time hard floor in resolveAttributes() is intentionally unconditional (blocks javascript:/data:text/html in binding-resolved href/src even without the plugin) — literal attributes keep their current behavior and remain the plugin's responsibility.


🤖 Prepared by an AI agent (OpenCode) from a security-audit findings list; commits are signed by the repository owner's key.

atinux added 11 commits August 20, 2026 19:47
Attribute values were interpolated into key="value" with no escaping,
so a double quote in any attribute value (raw HTML, component props,
fence info strings, image alt text) terminated the attribute early and
injected attacker-chosen attributes such as onmouseover handlers into
the rendered HTML. This bypassed comark/plugins/security, which
sanitizes the AST but cannot prevent quote breakout at serialization.

- Escape &, ", < and > in all attribute values emitted by
  htmlAttributes()
- Serialize object values as entity-escaped JSON (backslash escaping is
  not valid inside HTML attribute values)
- Drop attribute names outside the safe HTML name charset
- Update SPEC expectations that encoded the broken output
Handler maps are plain object literals, so a markdown component named
`constructor` resolved Object.prototype.constructor through the
prototype chain and was invoked as the node handler, emitting child
text without escaping. Names like `__proto__` or `valueOf` instead
threw and aborted the whole render.

Look up user and default handlers with Object.hasOwn so only real
handlers match.
Footnote labels were interpolated raw into href="#fn-<label>" and
id="fnref-<label>". A label containing a double quote broke out of
the attribute when rendered to HTML, injecting event-handler
attributes.

Encode any character outside [a-zA-Z0-9_-] as -<hex>- so references
and definitions still match and the emitted values stay inert.
The fence info language regex admitted quotes and angle brackets even
though the value lands in the language attribute and language-* class
of the rendered HTML. Stop the language at those characters; any
remainder still parses as meta.
Markdown-authored innerHTML / dangerouslySetInnerHTML / textContent
props reached Vue h(), React createElement, Svelte spreads and the
Angular DOM renderer verbatim, injecting raw HTML even when
comark/plugins/security was enabled (validateProp only rejected on*,
srcdoc and formaction).

- Add innerhtml, dangerouslysetinnerhtml and textcontent to
  unsafeAttributes so the security plugin strips them at parse time
- resolveAttributes() (the shared prop-resolution point for all four
  framework renderers) now drops those keys unconditionally — raw HTML
  already has its own explicit path via the html plugin
- Remove the Angular renderer's dedicated el.innerHTML assignment
The as prop makes framework renderers resolve a different component
than the element's own tag, but the plugin only checked element[0]
against blockedTags/allowedTags. [x]{as="AdminPanel"} instantiated
any resolvable app component even under a strict allowedTags list.

Strip as when its value names a blocked or not-allowed tag; the
element then falls back to its own (already validated) tag.
Entities were stripped instead of decoded, so javascript&#58;alert(1)
became javascriptalert(1), failed URL parsing, and passed as a
'relative' URL — while browsers entity-decode the attribute back into
a live javascript: URL. Decode entities (numeric, hex, and the
scheme-relevant named ones) until stable before the protocol checks,
and stop throwing on malformed percent-encoding.
…ts by origin

Two allowlist bypasses in validateUrl:

- //evil.com and \\evil.com forms failed new URL() without a base and
  were returned as always-allowed 'relative' URLs, skipping the
  protocol and prefix checks entirely. Resolve against a fixed dummy
  origin — only inputs that stay on it are genuinely relative.
- allowedLinkPrefixes: ['https://myapp.com'] matched
  https://myapp.com.evil.com via raw startsWith. Absolute-URL prefixes
  now compare parsed origin plus a path-segment boundary.
Renderers JSON-decode :binding values at render time, but validateProp
inspected the raw pre-resolution string. A JSON-quoted value like
'"javascript:alert(1)"' failed URL parsing, was classified as a
relative URL, and became a live javascript: href after the renderer's
JSON.parse — bypassing the plugin's protocol block and prefix
allowlists.

Decode the binding's JSON string before running URL validation, and
keep the original value when safe so the binding still resolves.
…r time

The security plugin validates :href/:src bindings at parse time, but a
dot-path binding like :href="frontmatter.home" only shows the literal
path — frontmatter data it resolves to was never checked, letting
javascript: URLs through the documented sanitizer.

resolveAttributes() now applies the hard-floor unsafe-scheme check to
binding-resolved href/src/xlink:href values at render time,
independent of the security plugin. Literal attributes are untouched:
their parse-time validation remains the plugin's job.
@atinux
atinux requested a review from farnabaz as a code owner August 20, 2026 18:53
@vercel

vercel Bot commented Aug 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
comark Ready Ready Preview Aug 25, 2026 1:05pm
comark-json-render Ready Ready Preview Aug 25, 2026 1:05pm
comark-nextjs Ready Ready Preview Aug 25, 2026 1:05pm
comark-nuxt Ready Ready Preview Aug 25, 2026 1:05pm
comark-svelte Ready Ready Preview Aug 25, 2026 1:05pm
comark-sveltekit Ready Ready Preview Aug 25, 2026 1:05pm
comark-twoslash Ready Ready Preview Aug 25, 2026 1:05pm
comark-vue Ready Ready Preview Aug 25, 2026 1:05pm

@github-actions

Copy link
Copy Markdown
Contributor

📄 Docs preview

This pull request touches 1 documentation page.

Changed or added

@github-actions

Copy link
Copy Markdown
Contributor

Documentation previews

📚 Preview all documentation changes

@pkg-pr-new

pkg-pr-new Bot commented Aug 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

comark

npm i https://pkg.pr.new/comark@373

@comark/angular

npm i https://pkg.pr.new/@comark/angular@373

@comark/ansi

npm i https://pkg.pr.new/@comark/ansi@373

@comark/html

npm i https://pkg.pr.new/@comark/html@373

@comark/nuxt

npm i https://pkg.pr.new/@comark/nuxt@373

@comark/react

npm i https://pkg.pr.new/@comark/react@373

@comark/svelte

npm i https://pkg.pr.new/@comark/svelte@373

@comark/vue

npm i https://pkg.pr.new/@comark/vue@373

commit: 4975780

@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

✅ Bundle snapshot updated

Committed the new snapshot as 4975780, verified by re-running the bundle check against it.

GitHub suppresses the events a workflow commit would normally raise, so the ci check above may
still show the earlier failure. Re-run it to refresh the status — the snapshot itself is confirmed green.

Produced by this run.

@farnabaz
farnabaz merged commit a6aa2b9 into main Aug 25, 2026
14 checks passed
@farnabaz
farnabaz deleted the fix/security-prop-url-validation branch August 25, 2026 13:37
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.

2 participants