Skip to content

A blocked localStorage getter takes the whole workspace down instead of losing persistence #770

Closed

Description

What happened?

Three places read the localStorage global behind a typeof guard:

function browserStorage(): ContextStorage | undefined {
  if (typeof localStorage === "undefined") return
  return localStorage
}

localStorage is an accessor property on the global object, so both typeof localStorage and localStorage perform a [[Get]] that invokes the getter. A
typeof guard therefore runs the very same access and cannot protect the read.
When storage is blocked — a sandboxed frame without allow-same-origin, a denied
origin, dom.storage.enabled=false — that access throws SecurityError and
there is no try here.

The consequence is a blank app rather than a degraded one, because two of these
run at module scope:

  • atlas/store/ui.ts — const state = createContextState(), exported as
    uiStore and used by the global keys, the panes and the toolbar
  • artifacts/context.ts — export const artifactContext = createArtifactState()
  • atlas/store/sessionTabs.ts — reached during the session page's render

So the throw happens while evaluating the argument, before restore(storage)
(which handles undefined perfectly well) ever runs, and the import fails.

FdaBanner has the mirror-image asymmetry: its write is guarded

try {
  localStorage.setItem(DISMISS_KEY, "1")
} catch {}

but the read nine lines above, in the createSignal initializer, is not — so the
home page throws while rendering the chip.

This is a known class here, and the rest of the workspace already does it
correctly: pages/session-trace.ts wraps the global in try/catch, and
atlas/files/last-source.ts carries the note "reading globalThis.localStorage is
itself what throws in a sandboxed frame"
. atlas/files/artifact-boundaries.test.ts
already asserts a component "survives a localStorage getter that throws during
render"
. These three copies and the FdaBanner read were simply missed.

Steps to reproduce

  1. Open the workspace in a sandboxed frame (an iframe without
    allow-same-origin), or with dom.storage.enabled=false.
  2. Load the app.
  3. The import of the workspace store throws SecurityError and nothing renders.

The three regressions in atlas/store/blocked-storage.test.ts install a throwing
localStorage getter and fail on 3e94875c with exactly that error.

How did you install OpenScience?

From source (bun dev); also affects the packaged app and the desktop shell.

OpenScience version

Source commit 3e94875c.

Provider and model

Not applicable; the workspace never reaches a model call.

Operating system

Not OS-specific. It depends on the embedding context rather than the platform.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions