Repository navigation
fix(nextly): reject duplicate plugin admin slugs at boot #747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bec4b50
fix(nextly): reject duplicate plugin admin slugs at boot
mobeenabdullah 12ada28
test(nextly): cover that resolvePlugins runs the slug check
mobeenabdullah 19e068c
fix(nextly): validate slugs where the list becomes addresses, not onl…
mobeenabdullah 9c85534
fix(nextly): validate the CLI's transformed plugin list too
mobeenabdullah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| "nextly": patch | ||
| "create-nextly-app": patch | ||
| "@nextlyhq/admin": patch | ||
| "@nextlyhq/admin-css": patch | ||
| "@nextlyhq/blocks-engine": patch | ||
| "@nextlyhq/blocks-react": patch | ||
| "@nextlyhq/ui": patch | ||
| "@nextlyhq/adapter-drizzle": patch | ||
| "@nextlyhq/adapter-postgres": patch | ||
| "@nextlyhq/adapter-mysql": patch | ||
| "@nextlyhq/adapter-sqlite": patch | ||
| "@nextlyhq/storage-s3": patch | ||
| "@nextlyhq/storage-uploadthing": patch | ||
| "@nextlyhq/storage-vercel-blob": patch | ||
| "@nextlyhq/plugin-form-builder": patch | ||
| "@nextlyhq/plugin-page-builder": patch | ||
| "@nextlyhq/plugin-seo": patch | ||
| "@nextlyhq/plugin-sdk": patch | ||
| "@nextlyhq/eslint-config": patch | ||
| "@nextlyhq/prettier-config": patch | ||
| "@nextlyhq/telemetry": patch | ||
| "@nextlyhq/tsconfig": patch | ||
| "@nextlyhq/builder": patch | ||
| "@nextlyhq/module-specifiers": patch | ||
| --- | ||
|
|
||
| Reject duplicate plugin admin slugs at boot. `pluginAdminSlug` collapses every | ||
| non-alphanumeric run to a single dash, so distinct package names can map to one | ||
| slug and the plugins then share a single admin address — one plugin's detail | ||
| page opens the other's, and host `pluginOverrides` apply to the wrong package. | ||
| No lookup downstream can detect this, because every lookup along that address | ||
| returns a plugin. `resolvePlugins` now refuses to start, naming both packages | ||
| and the slug they collide on. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * A plugin's admin slug is its address: the admin builds | ||
| * `/admin/plugins/<slug>` from it, core namespaces plugin admin routes with | ||
| * it, and host `pluginOverrides` are keyed by it. Two plugins reaching the | ||
| * same slug therefore share one address, and nothing downstream can notice — | ||
| * which is why this is checked at registration. | ||
| */ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { buildPluginAdminMeta } from "./admin-meta"; | ||
| import type { PluginDefinition } from "./plugin-context"; | ||
| import { validatePluginSlugs } from "./validate-slugs"; | ||
|
|
||
| function plugin(name: string): PluginDefinition { | ||
| return { name, version: "1.0.0", nextly: "*" } as PluginDefinition; | ||
| } | ||
|
|
||
| /** | ||
| * The thrown `NextlyError` carries a deliberately generic `message` | ||
| * ("Plugin configuration is invalid.") for the client; the specific failure | ||
| * lives in `logContext.reason`. Matching on `message` would pass for every | ||
| * plugin-resolution failure alike — an incompatible version included — so | ||
| * these assert the reason instead. | ||
| */ | ||
| function collisionReason(names: string[]): string | undefined { | ||
| try { | ||
| validatePluginSlugs(names.map(plugin)); | ||
| } catch (error) { | ||
| return (error as { logContext?: { reason?: string } }).logContext?.reason; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| describe("validatePluginSlugs", () => { | ||
| it("accepts plugins whose slugs differ", () => { | ||
| expect(() => | ||
| validatePluginSlugs([plugin("@acme/one"), plugin("@acme/two")]) | ||
| ).not.toThrow(); | ||
| // The control on `collisionReason` itself: it must report undefined when | ||
| // nothing collides, or every assertion above would pass on a helper that | ||
| // always returned the reason it was looking for. | ||
| expect(collisionReason(["@acme/one", "@acme/two"])).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("accepts an empty plugin list", () => { | ||
| expect(() => validatePluginSlugs([])).not.toThrow(); | ||
| }); | ||
|
|
||
| /** | ||
| * The separating cases, and the reason a check on the NAMES would not do. | ||
| * `pluginAdminSlug` lowercases and collapses each non-alphanumeric run to a | ||
| * single dash, so every pair below is two distinct, legal package names that | ||
| * produce one address. | ||
| */ | ||
| it.each([ | ||
| ["scope separator", "@acme/plugin-seo", "acme-plugin-seo"], | ||
| ["a dot for a dash", "@acme/plugin-seo", "@acme/plugin.seo"], | ||
| ["case", "@acme/plugin-seo", "@ACME/Plugin-SEO"], | ||
| ["underscores", "@acme/plugin-seo", "acme_plugin_seo"], | ||
| ["a doubled separator", "@acme/plugin-seo", "@acme//plugin--seo"], | ||
| ])("rejects two plugins colliding by %s", (_why, first, second) => { | ||
| expect(collisionReason([first, second])).toBe("duplicate-admin-slug"); | ||
| }); | ||
|
|
||
| /** | ||
| * The message is the entire remedy — the reader has to rename one package, | ||
| * and this is the only place the pair is ever stated together. Asserting it | ||
| * throws would pass on an error naming neither. | ||
| */ | ||
| it("names both plugins and the slug they collide on", () => { | ||
| let caught: unknown; | ||
| try { | ||
| validatePluginSlugs([ | ||
| plugin("@acme/plugin.seo"), | ||
| plugin("acme_plugin_seo"), | ||
| ]); | ||
| } catch (error) { | ||
| caught = error; | ||
| } | ||
|
|
||
| const message = (caught as { logMessage?: string })?.logMessage ?? ""; | ||
| expect(message).toContain("@acme/plugin.seo"); | ||
| expect(message).toContain("acme_plugin_seo"); | ||
| expect(message).toContain("acme-plugin-seo"); | ||
| expect( | ||
| (caught as { logContext?: { reason?: string } })?.logContext?.reason | ||
| ).toBe("duplicate-admin-slug"); | ||
| }); | ||
|
|
||
| /** | ||
| * The same package listed twice is the same collision as far as addressing | ||
| * goes, and it is the likelier mistake — a plugin registered by both the app | ||
| * and a preset. It must not be waved through as "identical, so harmless". | ||
| */ | ||
| it("rejects the exact same name registered twice", () => { | ||
| expect(collisionReason(["@acme/one", "@acme/one"])).toBe( | ||
| "duplicate-admin-slug" | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * The seam, as distinct from the boot check. | ||
| * | ||
| * `buildPluginAdminMeta` is the one place in core that turns a plugin list | ||
| * into addresses — the slug it derives is the admin's URL for that plugin and | ||
| * the key its host override is read by. Two paths reach it without the boot | ||
| * check having run on the list it receives: `createDynamicHandlers` | ||
| * initializes services lazily and serves the public admin-meta endpoint first, | ||
| * and a `setup` transformer can rewrite `config.plugins` after | ||
| * `resolvePlugins` has already validated the original. | ||
| */ | ||
| describe("buildPluginAdminMeta", () => { | ||
| it("addresses plugins whose slugs differ", () => { | ||
| const meta = buildPluginAdminMeta( | ||
| [plugin("@acme/one"), plugin("@acme/two")], | ||
| undefined | ||
| ); | ||
|
|
||
| // The positive control: it really does produce metadata for both, so the | ||
| // rejection below is about the collision rather than about a function that | ||
| // refuses everything. | ||
| expect(meta.map(m => m.name)).toEqual(["@acme/one", "@acme/two"]); | ||
| }); | ||
|
|
||
| it("refuses to address two plugins that share a slug", () => { | ||
| let reason: string | undefined; | ||
| try { | ||
| buildPluginAdminMeta( | ||
| [plugin("@acme/plugin-seo"), plugin("acme_plugin_seo")], | ||
| undefined | ||
| ); | ||
| } catch (error) { | ||
| reason = (error as { logContext?: { reason?: string } }).logContext | ||
| ?.reason; | ||
| } | ||
|
|
||
| expect(reason).toBe("duplicate-admin-slug"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import type { PluginDefinition } from "./plugin-context"; | ||
| import { pluginAdminSlug } from "./plugin-slug"; | ||
| import { resolutionError } from "./resolution-error"; | ||
|
|
||
| /** | ||
| * Boot-check that no two plugins address the same admin slug. Throws | ||
| * fail-fast. | ||
| * | ||
| * `pluginAdminSlug` is deliberately lossy — it lowercases and collapses every | ||
| * non-alphanumeric run to one dash — so distinct package names routinely map | ||
| * to one slug: `@acme/plugin-seo`, `@acme/plugin.seo` and `ACME_Plugin_SEO` | ||
| * are all `acme-plugin-seo`. That slug is the plugin's address: the admin | ||
| * builds `/admin/plugins/<slug>` from it, core namespaces the plugin's admin | ||
| * routes with it, and host `pluginOverrides` are looked up by it. | ||
| * | ||
| * So a collision is not a cosmetic clash. Two plugins share one address, and | ||
| * every lookup along it answers with whichever the search reaches first: one | ||
| * plugin's page opens the other's, and one plugin's overrides silently apply | ||
| * to its neighbour. | ||
| * | ||
| * The check belongs HERE rather than at any of those lookups, and that is the | ||
| * whole point. At a lookup there is nothing to observe — `find()` returns a | ||
| * plugin, and a plugin is exactly what a correct lookup returns, so no code | ||
| * downstream can tell "the right one" from "the first of two". Registration is | ||
| * the last moment at which the ambiguity is still visible as ambiguity. | ||
| * | ||
| * @module plugins/validate-slugs | ||
| */ | ||
| export function validatePluginSlugs(plugins: PluginDefinition[]): void { | ||
| const byslug = new Map<string, string>(); | ||
|
|
||
| for (const plugin of plugins) { | ||
| const slug = pluginAdminSlug(plugin.name); | ||
| const owner = byslug.get(slug); | ||
|
|
||
| if (owner !== undefined) { | ||
| // Both names, because neither alone is actionable: the reader has to | ||
| // rename one of them, and the message is the only place the pair is | ||
| // ever stated. The slug is included because it is not obvious from | ||
| // either name which characters collapsed. | ||
| throw resolutionError( | ||
| "duplicate-admin-slug", | ||
| `Plugins "${owner}" and "${plugin.name}" both resolve to the admin ` + | ||
| `slug "${slug}", so they would share one admin address. Rename one ` + | ||
| `of them.`, | ||
| { slug, plugins: [owner, plugin.name] } | ||
| ); | ||
| } | ||
|
|
||
| byslug.set(slug, plugin.name); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.