History

fix: harden plugin command catalog

t3-code[bot] committed 2 months ago

Commit 12b8e9e1 parent 596bd978 files · +140 −18Browse files

Showing the changes to packages/plugin-runtime/src (2 of 8 files). Show all

Unified · Split

2 files
  1. packages/plugin-runtime/src/contract.ts +9 −1
  2. packages/plugin-runtime/src/runtime.ts +47 −7
packages/plugin-runtime/src/contract.ts+9−1
@@ -1,4 +1,12 @@
1−export interface Contribution<Data = unknown> {
1+export type ContributionData =
2+ | null
3+ | boolean
4+ | number
5+ | string
6+ | ReadonlyArray<ContributionData>
7+ | { readonly [key: string]: ContributionData };
8+
9+export interface Contribution<Data extends ContributionData = ContributionData> {
210 readonly id: string;
311 readonly label: string;
412 readonly data?: Data;
packages/plugin-runtime/src/runtime.ts+47−7
@@ -12,6 +12,7 @@ import * as Scope from "effect/Scope"; · 11 unmodified lines
1212
1313 import type {
1414 Contribution,
15+ ContributionData,
1516 PluginActivationContext,
1617 PluginDefinition,
1718 PluginRuntimeContributionSnapshot,
@@ -245,19 +246,58 @@ const sameStringRecord = ( · 227 unmodified lines
245246 return leftKeys.every((key) => left[key] === right[key]);
246247 };
247248
248−const deepFreeze = <Value>(value: Value): Value => {
249− if (typeof value !== "object" || value === null || Object.isFrozen(value)) return value;
250− for (const nested of Object.values(value)) deepFreeze(nested);
251− return Object.freeze(value);
249+const cloneContributionData = (
250+ value: ContributionData,
251+ ancestors = new WeakSet<object>(),
252+): ContributionData => {
253+ if (value === null || typeof value === "string" || typeof value === "boolean") return value;
254+ if (typeof value === "number") {
255+ if (!Number.isFinite(value)) throw new TypeError("Contribution data numbers must be finite");
256+ return value;
257+ }
258+ if (typeof value !== "object") {
259+ throw new TypeError("Contribution data must contain only JSON-compatible values");
260+ }
261+ if (ancestors.has(value)) throw new TypeError("Contribution data cannot contain cycles");
262+ ancestors.add(value);
263+ try {
264+ if (Array.isArray(value)) {
265+ const clone: Array<ContributionData> = [];
266+ for (let index = 0; index < value.length; index += 1) {
267+ if (!Object.hasOwn(value, index)) {
268+ throw new TypeError("Contribution data arrays cannot contain holes");
269+ }
270+ clone.push(cloneContributionData(value[index]!, ancestors));
271+ }
272+ return Object.freeze(clone);
273+ }
274+
275+ const prototype = Object.getPrototypeOf(value);
276+ if (prototype !== Object.prototype && prototype !== null) {
277+ throw new TypeError("Contribution data objects must use a plain or null prototype");
278+ }
279+ const clone = createNullPrototypeRecord<ContributionData>();
280+ for (const key of Reflect.ownKeys(value)) {
281+ if (typeof key !== "string") {
282+ throw new TypeError("Contribution data objects cannot use symbol keys");
283+ }
284+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
285+ if (descriptor === undefined || descriptor.enumerable !== true || !("value" in descriptor)) {
286+ throw new TypeError("Contribution data objects must use enumerable data properties");
287+ }
288+ clone[key] = cloneContributionData(descriptor.value as ContributionData, ancestors);
289+ }
290+ return Object.freeze(clone);
291+ } finally {
292+ ancestors.delete(value);
293+ }
252294 };
253295
254296 const detachContribution = (contribution: Contribution): Contribution =>
255297 Object.freeze({
256298 id: contribution.id,
257299 label: contribution.label,
258− ...(contribution.data === undefined
259− ? {}
260− : { data: deepFreeze(structuredClone(contribution.data)) }),
300+ ...(contribution.data === undefined ? {} : { data: cloneContributionData(contribution.data) }),
261301 });
262302
263303 const snapshotDefinitions = (

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t