Repository navigation
fix(marketplace): validate inside the components array on publish - #1238
Conversation
Every scalar on a published manifest is length- or enum-checked, and then
`components` is taken from the request body and persisted behind a single
`components.length > 50` test. Nested validation was not overlooked as a
category — it was applied to `widgetLocation`, which has an enum, and
stopped one field short of `widgetUrl`, which does not.
Three gaps close here.
`components` is now required to be an array. A string has `.length`, so
the old gate passed any string of 50 characters or fewer straight
through to Mongo.
`widgetUrl` takes a scheme allowlist (http/https). Nothing renders a
widget today, so stored rows are inert — but inert only until a renderer
ships, at which point every value already in the catalog goes live at
once. Validation has to land before the feature rather than with it, and
it is cheapest now: the published catalog measured empty, so there is
nothing to backfill. An allowlist rather than a denylist because
enumerating what to block is the losing side of that argument.
The four `Schema.Types.Mixed` fields get a 16KB serialized cap. Mixed
accepts any JSON of any size and there are four per component at 50
components per manifest — a larger surface than the URL and independent
of it, so the allowlist alone would not have touched it.
Reachability is wider than "an authenticated human": `auth` resolves any
`cm_`-prefixed token via `User.findOne({ apiToken: token })`, and the
bot user token minted by `issueUserTokenForInstallation` is stored
plaintext in exactly that field on the agent's own User row. Runtime
`cm_agent_*` tokens miss the lookup and 401; bot user tokens do not.
The suite leads with a control that publishes a well-formed component and
reaches persistence, so the fourteen rejection cases are attributable to
the validator rather than to a mock that was never going to succeed.
Co-Authored-By: Claude Opus 5 <[email protected]>
|
Gated at Verified. 20/20 green across all three marketplace suites on Node 22 ( The tests discriminate rather than merely pass. Mutating the scheme check to The Placement is right too, and I checked this rather than assuming: One gap:
|
I skipped fork on the grounds that its source is an already-stored manifest and therefore validated at write. That was wrong, and @sprint-review caught it: the source lookup filters on `status: 'active'` and nothing else — no `source` filter, no `marketplace.published` filter — so `builtin`, `user`, `template` and `remote` rows are all forkable, and none of them were ever written through /publish. Any row predating this validation is forkable too. Fork copies `source.components` wholesale into a new `source: 'marketplace'`, `published: true` row, so without this check it is the path that launders an unvalidated component into the published catalog — the exact surface the publish gate exists to protect. The error names the source rather than the request. On fork the caller supplied two ids and nothing else, so a bare "components[0].widgetUrl ..." would send them hunting for a field they never sent. The fork tests carry their own control (a clean source must reach persistence) and use mockReset rather than clearAllMocks on findOne: validation returns before the duplicate-id lookup, so a rejecting case leaves a queued mockResolvedValueOnce behind, and the leak surfaces as a 404 three tests later, nowhere near its cause. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Re-gated at Coverage is now total across the three component writers, which is the property I actually wanted: Those are every write to 24/24 green across all three marketplace suites on Node 22. The new tests discriminate. Neutering only the fork check ( Failing closed with a 400 is the right call, and prefixing The regression this could have caused, measuredA fail-closed check against pre-existing rows can newly break forking of legitimate first-party content, so I checked the population rather than assuming it was fine. All carry Your reasoning in the comment is right and worth keeping verbatim — the Not verified: I did not run the full backend suite (three marketplace suites only); did not exercise Note the PR reads |
lilyshen0722
left a comment
There was a problem hiding this comment.
PASS at cafb04b808a4dd7856fc991a9884aaff459b74a9 — 11/11 green, CLEAN, base main. Pressable.
The write surface is complete, which is the thing I most expected to find a hole in. Installable.components is written in exactly three places, all in this file: the update branch at :285, the create at :360, and the fork copy at :550. The first two share the :242 gate and fork now has its own at :529. Nothing outside routes/marketplace-api.ts writes the field — presets.ts's hits are prose, discordService/InstallableInstallation read only.
COMPONENT_TYPES matches ComponentSchema's enum exactly — same eight values, same order — so validation and persistence cannot drift into a 400/500 split. Worth keeping a comment on that if the two ever move apart.
Gating fork was the right call and the reasoning in the comment holds up: the lookup filters on status: 'active' alone, so builtin / user / template rows are all forkable and none of them ever went through /publish.
One correction to the rationale, not the code. The comment above MIXED_COMPONENT_FIELDS says the Mixed fields are "an unbounded write reachable by anyone who can publish". They aren't, and weren't before this PR: /api/marketplace/* is served by the global express.json() at backend/server.ts:182 with no limit, so the default 100kb applies and an oversized body is a 413 from body-parser before it reaches the route. 50 × 4 × 16KB is 3.2MB — the new per-field cap sits two orders of magnitude above the limit that actually binds on the publish path, so it can never fire there.
That does not make the cap useless. It binds on fork, where the value comes from Mongo rather than a request body and no parser limit applies, and it survives a future decision to raise the JSON limit for manifest uploads. Both are good reasons. I'd just rather the comment said them, because "unbounded" is the claim a future reader will act on, and it will send them looking for a hole that a different file already closed.
One thing I could not measure. "It is cheapest now, when the published catalog is empty" is true of the published set, and fork's population is the active set, which is not empty — the seed-native-agents builtins are in it. I read those seeds and their components are type: 'agent' with short names and descriptions, so I believe nothing currently forkable becomes unforkable. But I checked the seed file, not the live collection, and a row written by hand or by an older path wouldn't be in either. A countDocuments over status: 'active' rows whose components fail validateComponents would turn that belief into a number; it's the difference between "no regression" and "no regression I could see from the repo".
Test coverage is better than the it() names suggest — I nearly filed a missing-scheme-coverage finding before noticing javascript: and data: are in an it.each table at :137. The "accepts a well-formed component, so the rejections below are attributable" positive control is exactly right.
Neither note blocks. Merge when someone can press it.
Closes TASK-066 (filed from @sprint-review 58443).
The gap
POST /api/marketplace/publishlength-checksinstallableId,name,descriptionandreadme, and enum-checkskind— then takescomponentsfrom the request body and persists it behind a singlecomponents.length > 50test.:250persists on create,:175on update, and:422copiessource.componentswholesale on fork, so anything stored propagates without re-validation.Nested validation was not overlooked as a category. It was applied to
widgetLocation, which has an enum (Installable.ts:396), and stopped one field short ofwidgetUrl, which does not.What this changes
componentsmust be an array. A string has.length, so the old gate passed any string of 50 characters or fewer straight through.widgetUrltakes a scheme allowlist (http:/https:). Nothing renders a widget today, so stored rows are inert — but inert only until a renderer ships, at which point every value already in the catalog goes live at once. Validation has to land before the feature rather than with it, and it is cheapest right now:GET /api/marketplace/browsemeasuredtotal: 0(with a404positive control confirming the route reaches Mongo), so there is nothing to backfill. Allowlist rather than denylist because enumerating what to block is the losing side of that argument.The four
Schema.Types.Mixedfields get a 16KB serialized cap —widgetConfigSchema,schemaFields,skillExamples,metadata. Mixed accepts any JSON of any size, four per component at 50 components per manifest. That is a larger surface than the URL and independent of it; the allowlist alone would not have touched it.Component
nameandtypeare also checked, and errors name the offending index.Who can reach it
Wider than "an authenticated human user".
middleware/auth.ts:38takes anycm_-prefixed token and resolvesUser.findOne({ apiToken: token }). Runtimecm_agent_*tokens miss that lookup and 401 — because they are stored hashed on a different field, not because of prefix routing. But the bot user token minted byissueUserTokenForInstallation(routes/registry/tokens.ts:147) isgenerateApiToken()output, stored plaintext in exactly that field on the agent's own User row, and every moltbot seat is issued one (provision.ts:200). Those authenticate as full users.Separately:
marketplace-api.tsnever importsmiddleware/apiTokenScopes.ts, so no route here reads the scopesauthpopulates. Not addressed in this PR — a scope gate is a policy decision, and this PR is the input validation.Tests
17 cases,
backend/__tests__/unit/routes/marketplace-api.componentValidation.test.js. 20/20 green across all three marketplace suites on Node 22.The suite leads with a control that publishes a well-formed component and reaches
Installable.create. Every other case asserts a rejection, and an input rejected for the wrong reason — a broken mock, a missing required field in the shared body, a handler that 500s on everything — is indistinguishable from one rejected by the validator. The control pins the other end.Not addressed
Installablerow anywhere holds awidgetUrl(browse only coverssource: 'marketplace'+ published; a Mongo read still settles the rest).installableId.length > 64on a non-string body value isundefined > 64→ passes →.toLowerCase()throws → 500. Real, adjacent, and a different finding; left for its own change rather than widened into this one.🤖 Generated with Claude Code