Adopt SES / Hardened JS lockdown() as the systematic control for artifact isolation

Open
#86 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Quiet
Domain
frontend, security

Research direction

This is a design proposal rather than a bug, so start by reading the companion script v1.js and find where it relies on built-in intrinsics for trust decisions, plus the artifact CSP that currently blocks unsafe-eval. Done means a agreed decision on adopting SES lockdown() and a compatibility assessment against existing artifacts, not a patch. Expect to discuss scope with maintainers before writing code.

Written by the indexing model from the issue text.

Description

triage

Summary

Primer's companion runtime is trusted code that runs in the same JavaScript realm as the untrusted artifact it serves. Today it defends itself against a hostile artifact with targeted, per-property hardening. This issue proposes basing artifact isolation on a catalogued, maintained control — SES / Hardened JS lockdown() — instead of on fixes added one exploit variant at a time.

Background

Artifacts run in a sandboxed, opaque-origin iframe (no allow-same-origin), and a strict CSP blocks network access. That boundary is strong and browser-enforced. But the companion (v1.js) has to run inside the artifact's realm, because much of its job — comment anchoring, hover/selection, DOM geometry — requires reading and decorating the artifact's live DOM, which the cross-origin parent page cannot do. So the companion is a trusted component that shares primordials (prototypes, built-in functions, global intrinsics) with untrusted code.

That shared-realm arrangement exposes the companion to a well-known class of attacks: prototype pollution, per-instance property shadowing, tampering with built-in functions, and dynamic code evaluation. A hostile artifact can redefine or shadow the objects and functions the companion relies on to make trust decisions.

Why per-property hardening does not scale

The current approach freezes the specific intrinsics the companion depends on. This works for the properties it targets, but it addresses instances, not the class:

  • Closing one tampering vector (for example, a redefined prototype getter) does not close the equivalent variant (shadowing the same property on a specific instance, or tampering with the reflection primitive used to read it). Each variant is the same underlying weakness surfacing in a new place, and each must be found and patched separately.
  • At least one member of the class — dynamic code evaluation via new Function / eval — is currently closed only incidentally, by the artifact CSP omitting unsafe-eval. The companion's own defenses do not address it. The script-src has already been broadened to permit inline scripts and several CDN hosts; if unsafe-eval is ever added for a legitimate artifact need, this vector reopens silently.

Because it depends on anticipating each variant, per-property hardening cannot be shown to be complete.

Recommendation

Adopt SES / Hardened JS (lockdown(), from the Endo project) as the isolation control:

  1. Run lockdown() before any artifact script executes. This freezes the realm's shared intrinsics, closing prototype pollution and built-in tampering across the whole class in one audited step rather than property by property. It also tames the dynamic evaluators (Function, eval), removing the reliance on CSP for that vector.
  2. Make it a platform rule that artifacts may not mutate the base environment. This is consistent with the existing strict-CSP posture; a self-contained review artifact has no legitimate need to patch built-in prototypes or generate code at runtime.
  3. For the narrow cases lockdown() does not cover — reading a security-relevant value off a dynamically-created DOM instance, which can still be shadowed per-instance — read through a native accessor captured before any artifact code runs. With intrinsics frozen, that capture is trivially safe.
Tradeoffs to weigh
  • Compatibility. lockdown() breaks artifacts that rely on two patterns: writing to properties inherited from now-frozen prototypes (the "override mistake," tunable via lockdown's overrideTaming option), and runtime code generation. Both should be rare in self-contained artifacts and are arguably things the platform should forbid, but a compatibility pass against the existing artifact backlog is needed before enabling it.
  • Cost. SES adds a dependency to the injected companion bundle and a per-load cost to freeze intrinsics.
  • Maintenance. It is a real dependency and audit surface — but a maintained, catalogued one, versus continuing to hand-roll and re-verify isolation logic in-house.
Note on LavaMoat

LavaMoat is built on SES but targets supply-chain risk (isolating npm dependencies under per-package policy). The primitive relevant to artifact isolation is SES lockdown() / Compartments, not LavaMoat's package-policy layer. LavaMoat may be worth separate consideration for Primer's own build dependencies.

Scope

This is a hardening and architecture proposal, not an active incident — the specific tampering vectors found so far are either already fixed or currently mitigated by CSP. The goal is to make artifact isolation rest on a catalogued, maintained control, so that completeness no longer depends on anticipating each exploit variant individually.

Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
13h 38m
Merged PRs (30d)
24

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 0xPolygon/primer

All issues in 0xPolygon/primer

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.