Skip to content

fix(flame): run onLoad on every matching file when the filter has the g or y flag - #482

Merged
gitfromwildan merged 1 commit into
mainfrom
fix/plugin-onload-stateful-filter
Nov 6, 2026
Merged

gitfromwildan merged 1 commit into
mainfrom
fix/plugin-onload-stateful-filter

Conversation

@gitfromwildan

Copy link
Copy Markdown
Member

Summary

Found in a security audit of flame's plugin system. A plugin's onLoad handler was skipped on every other matching file when its filter had the g or y flag. The skipped files were built from their unchanged source and the build still succeeded. A plugin that removes internal content in onLoad would have shipped that content on half the pages.

Cause

runOnLoad called handler.filter.test(path) on the plugin's own RegExp. With g or y, RegExp#test resumes from lastIndex, so after a match the next path is tested from the wrong position and fails.

Reproduced with a redaction handler on /\.mdx$/g over four files:

["[redacted]", "SECRET", "[redacted]", "SECRET"]

Fix

  • onLoad copies the filter once at registration without g and y, so every path is matched from the start. The plugin's own RegExp is not mutated.
  • A filter that is not a RegExp now throws [plugin] onLoad requires a RegExp \filter`insetup(). Before, it threw a TypeErrorfromrunOnLoad` partway through the build.
  • PluginBuilder.onLoad JSDoc says the g and y flags are ignored.
  • Changeset: @docubook/flame patch.

Docs: required options belong in setup()

The plugin guide, the onStart hook reference and the onStart JSDoc all showed required-option checks as a throw in onStart. Execution hooks only log their errors, so that check never stopped the build.

  • The validator example in "Step 5: Error Handling" now checks build.config in setup(), where an error stops the build.
  • The Error Isolation table has a row for the plugin factory and setup(). A new paragraph says not to rely on an execution hook for something the site must never ship without.
  • The onStart examples now warn instead of throwing. The lifecycle diagram labels setup as the place to validate.

Runtime error handling is unchanged.

Verification

  • Two new tests in plugin.test.ts: a /\.mdx$/gy filter transforms all four files, and a string filter is rejected. Both fail on the builder from main and pass with this change.
  • flame: 847 tests in 42 files, oxlint, and tsc --noEmit pass. lint-staged (Prettier, oxlint), the pre-push hooks and scripts/check-release-plan.mjs (6 scenarios) pass.

Not in this PR

Other findings from the audit, left for separate changes:

  • Inline scripts from plugins. Inline <script> from injectHead/injectBody gets no nonce in the build, so the page's meta CSP blocks it in production. flame preview adds the nonce to every inline script through injectNonce, so such a script runs in preview and fails once deployed. The analytics example in the plugin guide is affected. A fix needs a decision: either flame adds the nonce to plugin injections only, or PageContext exposes the page nonce to plugins.
  • Strict mode for hook errors. An option, or CI behaviour, could fail the build when an execution hook throws, instead of logging the error and continuing.

… g or y flag

runOnLoad called handler.filter.test(path) on the plugin's own RegExp. With the g or y flag, test() resumes from lastIndex, so every other matching file skipped the handler and was built from its unchanged source. A redaction plugin registered with /\.mdx$/g left "SECRET" in docs/b.mdx and docs/d.mdx while the build still succeeded.

onLoad now copies the filter once without g and y, so each path is matched from the start. A filter that is not a RegExp is rejected in setup() instead of crashing the build in runOnLoad.

The plugin docs and JSDoc showed required-option checks as a throw in onStart. Execution hooks only log their errors, so that check never stopped the build. The examples now check options in setup(), where an error stops the build, and the Error Isolation section says not to rely on an execution hook for something the site must never ship without.
@vercel

vercel Bot commented Nov 6, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Nov 6, 2026 4:43am UTC

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

I reviewed the onLoad filter fix, its regression tests, and the doc and JSDoc changes that move required-option checks out of onStart (mode: senior, because this touches plugin execution). Verdict: READY.

  • Stateless onLoad matching: onLoad() now stores a copy of the filter without g/y, so runOnLoad's handler.filter.test(path) matches every path from index 0. The plugin's own RegExp is not mutated.
  • Filter type check at registration: a filter that is not a RegExp now throws a TypeError in setup(). Before, it threw from runOnLoad partway through the build. I checked that this error stops the build: plugin.setup(builder) is awaited without a try in build.ts, build.impl.ts, server.ts and server.impl.ts, so the throw propagates.
  • Regression tests: the /\.mdx$/gy test fails on main. With y and lastIndex at 0, docs/a.mdx never matches, so none of the four files are redacted. The string-filter test covers the new throw. I ran npx vitest run .docu/__tests__/plugin.test.ts in packages/flame after building the workspace deps: all 49 tests pass. oxlint is clean on the changed TS files.
  • Docs move validation to setup(): the claim that onStart errors are only logged matches runOnStart. Factory errors are rethrown by plugin-loader.ts, and build.config exists on PluginBuilder, so the new validator example works as written.
  • Changeset: @docubook/flame patch. This fits a bug fix with no API shape change.

One semantic note, not a blocker: removing y also removes its "match starting at index 0" behavior. A filter like /docs\//y used to match only paths that start with docs/, and now matches docs/ anywhere in the path. The old behavior was already broken for that case (it alternated between matching and not), and the JSDoc now says the g and y flags are ignored, so this looks like the right call. A filter that should only match at the start of the path needs ^.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

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.

1 participant