Files
Teffen Ellis 32ae96fb8b web/flows: render CAPTCHA widgets without an iframe wrapper (#26143)
* web/flows: render CAPTCHA widgets without an iframe wrapper

The CAPTCHA stage rendered every interactive widget inside an iframe it
generated itself, as a blob URL or a `document.write`-populated
`about:blank` document. The wrapper existed to stop two providers
colliding over their `window` globals, since a flow can present more than
one CAPTCHA stage and each vendor installs a global under a fixed name.

The collision was real, but the iframe addressed the symptom. The cause is
that `CaptchaController.discover()` chose a provider by sniffing `window`
in a fixed preference order rather than by looking at the script the
challenge actually asked for. Provider scripts are appended to
`document.head` and never removed, so by the second stage several globals
are present and the first one in the list wins regardless of which vendor
the challenge named.

Providers are now resolved with `matchesURL()` against the challenge's
`jsUrl`, which is unambiguous, and widgets render in place through each
vendor's own explicit render API. Falling back to global discovery is kept
for proxied script URLs that no host pattern can anticipate, and that path
now logs which mechanism it used.

Removing the wrapper removes everything built on top of it: the
`postMessage` bridge, the generated document template, the `document.write`
fallback, blob URLs, and the MutationObserver/ResizeObserver pair that
synchronised the wrapper's height. Widgets participate in normal layout
instead, and `CaptchaStage` loses more than half its length.

This also un-blocks a strict Content-Security-Policy: the generated
document required `script-src 'unsafe-inline'` for its callbacks and
`frame-src blob:` for its own URL, neither of which is needed now. The
directives a policy still has to allow are per-provider and derived from
the stage's configured URLs; `script-loader.ts` documents them.

Along the way:

- Reload the widget when `challenge` changes, not only on first render.
  The executor reuses one `<ak-stage-captcha>` element across stages, so a
  flow with two CAPTCHA stages previously kept showing the first stage's
  solved widget and never advanced.
- Talk to `grecaptcha.enterprise` when the challenge names `enterprise.js`.
  Enterprise site keys are rejected by the classic entry point.
- Give the "Provider Type", "Order" and "Stage" form controls accessible
  names. The first two had labels that could not associate across a shadow
  root, and the stage picker had no accessible name at all.
- Await the typed filter in `FormFixture.selectSearchValue`. The floating
  promise meant the option lookup searched an unfiltered list, which finds
  nothing once a collection outgrows the picker's first page.

Tests: `test/unit/captcha-provider-resolution.test.ts` covers provider
resolution for all four vendors, including Cap, with no network. The
browser suite drives a real flow per vendor against the vendors' published
test keys; it is tagged `@vendor-network` since it depends on third-party
availability.

web/flows: render CAPTCHA widgets in the document tree

Removing the wrapper iframe broke reCAPTCHA. The widget rendered, but the
checkbox spun forever and the flow never advanced: reCAPTCHA resolves its
own elements through `document`, so a container whose `getRootNode()` is a
`ShadowRoot` can never finish verifying. Rendering in place had put the
container inside the flow executor's shadow root.

So the wrapper was load-bearing for a second reason nobody wrote down. It
was not only keeping two providers from colliding over their `window`
globals — it was also handing reCAPTCHA a document to work in.

Confirmed with a controlled test: same page, same script, same site key,
same callback, ~20 lines of vanilla JS and no authentik code. Rendered into
`document.body` the token arrives; rendered into an `attachShadow()` root it
never does. hCaptcha and Turnstile both solve either way.

The container now attaches to the nearest ancestor that sits in the document
tree — the flow executor, which the Django template writes directly into the
page — and is projected back into the card through a chain of forwarded
slots. Slotting moves no nodes, so the container keeps `getRootNode() ===
document` while appearing exactly where it did before. The executor forwards
into whichever stage it renders, and the identification stage forwards again
into its embedded CAPTCHA; a forwarded slot with no matching name in the
child's shadow root renders nothing, so no stage needs special-casing.

The widget frames also needed clipping. Providers size them with the
`height` content attribute, and that hint does not survive on authentik's
pages — a plain `<iframe height="78">` computes to the 150px
replaced-element default in both the flow and admin apps, while identical
markup computes to 78px elsewhere — so the frame spilled below the widget as
a blank band. Inside the wrapper iframe no surrounding stylesheet reached
it. The cause is not yet identified; clipping to the box the provider
declares is safe because every provider renders its expanded challenge as an
overlay on `document.body`, never inside the container.

Also collects the vendor constants that had spread across the controllers —
host patterns, globals and URL matching now hang off a single `CaptchaVendor`
spine, so a controller declares its vendor and inherits the rest. The
provider presets in the admin form point at the same spine instead of
carrying a second copy of the Cap URL test. Test credentials live in `e2e/`
rather than `src/`: esbuild drops an unused object only when its initializer
is free of property accesses, so a record referencing the spine can never be
tree-shaken out of the bundle.

Tests: each vendor now has a solve-and-advance case, which is what the suite
was missing — every case asserted a widget rendered, none that it could be
completed, so reCAPTCHA stayed green while broken. Verified by reverting the
container to shadow placement and watching the reCAPTCHA case fail.

web/flows: keep CAPTCHA frames at their declared size and backdrop

Now that vendor widgets render in the document tree, the flow interface's
stylesheets reach them. Two regressions from that, both on reCAPTCHA:

PatternFly's base reset (`img, embed, iframe, ... { height: auto }`) beats
the presentational `width`/`height` attributes vendors size their anchor
frames with, so the frame computed to the 150px replaced-element default
and showed 72px of blank body below the widget. The stage now observes the
container and pins each arriving frame to its declared size inline. Turnstile
already sizes its frame inline and is left alone.

The frame element inherits `color-scheme: dark` from the theme while the
anchor document declares none, and browsers paint an opaque canvas behind an
embedded document whose scheme disagrees with its element's. That was the
white behind the widget's rounded corners and along its 2px inset. Vendors
can now adjust their own frames through `CaptchaController.decorateFrame`;
reCAPTCHA declares its frame `light` to match the document.

Also passes `theme` to the invisible reCAPTCHA render, gives test flows
names that can be told apart in the admin list, and asserts the rendered
frame size matches the declared one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vf7S2QdjCToGGTwz7uxPaD

Clarify comment.

Re-render after theme change.

Clean up.

* packages/fonts: add a no-op build script

The npm publish workflow runs `pnpm run build` for every package, and
fonts had no build script, so its publish failed with
ERR_PNPM_NO_SCRIPT.

* packages: bump tsconfig to 2.1.0 and docusaurus-config to 3.2.0

Both changed without a version bump, so publishing them would fail
with a 403 for the already-published version. tsconfig's empty build
script also failed with ERR_PNPM_NO_SCRIPT.

* web: keep Storybook docs in sync with theme changes

createUIThemeEffect skipped a change when html[data-theme] already
matched, but another listener sharing the document may have set it
first, so later listeners (Storybook's docs container) never updated.
Each listener now tracks the last theme it handled. The Storybook
preview styles follow html[data-theme] instead of the OS color scheme.

* web/test/visual: keep a separate report per suite

Each suite now writes its HTML report to .visual/report/<suite>/ and
its artifacts to test-results/visual/<suite>/, so running one suite no
longer replaces the other's report.

* packages/tsconfig: split into base, web, and node configs

@goauthentik/tsconfig is now an environment-agnostic base with two
presets:

- `@goauthentik/tsconfig/web` for bundled code: bundler resolution,
  `module: preserve`, the DOM libraries, JSX, and declaration-only emit.
- `@goauthentik/tsconfig/node` for scripts Node runs from source:
  `noEmit`, erasable syntax only, `verbatimModuleSyntax`, and `.ts`
  import extensions.

The base no longer includes Node types or JSX, so this is a major
version. web and website move to the web preset; the root, scripts,
web/packages/core, and geo's scripts and tests move to the node preset;
packages that stay on the base and need Node declare its types
themselves. web, web/packages/core, and website/api link the local
package instead of a published version.

* packages/theme: emit the spacing category file

The spacing category matched the `global--space--` prefix, but the
tokens are named `--ak-global--spacer--*`, so the build skipped it and
the exported ./dist/spacing.css was never written.

* web/admin: back-fill the created stage in the bind wizard

The stage wizard's binding step passed only the new stage's pk, so the
stage picker could only select it when it happened to be on the first
page of results. Beyond that the binding saved with a null stage. Pass
the stage object too, so the picker always offers and selects it.

The captcha e2e fixture relies on the back-fill instead of searching,
and scopes the Create or bind button to the toolbar, since an empty
flow shows a second one.

* web/flows: harden the CAPTCHA stage's load lifecycle

- Discard a mount that finishes after a newer load has started, instead
  of making it the active controller and leaving its widget behind.
- Rebuild the widget for every new challenge object rather than a
  jsUrl/siteKey/interactive signature, so two identical CAPTCHA stages
  in a row no longer reuse the first stage's solved widget.
- Forget the loaded challenge and cancel in-flight loads on disconnect,
  and reload on reconnect.
- Skip theme and locale rebuilds once a token has been issued, since a
  rebuilt widget would be unsolved while its token was already handed on.

Also rewords the file's comments.
2026-09-25 16:59:39 +01:00

30 lines
980 B
JSON

// TypeScript Project Configuration
{
"extends": "@goauthentik/tsconfig/node",
"compilerOptions": {
"ignoreDeprecations": "6.0"
},
"watchOptions": {
"excludeDirectories": [
"**/.git", // Git
"**/.yarn", // Yarn
"**/.vscode", // VS Code
"**/.vscode-test-web", // VS Code Web Test
"**/dist", // Distributed build files
"**/out", // Output build files
"**/.drafts", // Drafts
"**/.github", // GitHub
"**/node_modules" // Node modules
]
},
// The root project has no sources of its own. By setting `files` to an empty
// list, TS won't automatically include all sources below root (the default).
"files": [],
"references": [
// Note that references are in the order we want them to be built.
// TODO: Left blank until TypeScript workspaces are complete.
{ "path": "./scripts/node" }
]
}