Skip to content

fix: normalize legacy configuration safely - #49

Closed
usercao wants to merge 1 commit into
mainfrom
fix/legacy-configuration-normalization
Closed

usercao wants to merge 1 commit into
mainfrom
fix/legacy-configuration-normalization

Conversation

@usercao

@usercao usercao commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Direct callers of the public PluginConfigurationManager can pass JavaScript objects rather than JSON. A throwing getter or revoked proxy currently escapes normalization, caller mutations can change accepted tag arrays or Emmet options without a configuration update, and an unknown __proto__ lint key is lost.

Copy accepted tag arrays and top-level configuration records, retain nested object references, and preserve unknown lint keys as own data properties. Validate array lengths before normalizing entries; logging a rejected value can still read its properties. If reading or normalizing configuration throws, reset to defaults, report the problem through the existing safe logger, and notify configuration listeners once. Listener callbacks remain outside the normalization catch.

This change contains the legacy normalization work separated from PR #43: the normalization guard, getArrayLength, own-property lint writes, and cloneConfigurationRecord. It is based directly on main and introduces no editor configuration protocol types, listener lifecycle changes, or diagnostic refresh changes.

Testing

  • corepack yarn test:unit -t 'PluginConfigurationManager'
  • corepack yarn verify

The seven new regression cases failed on main before the implementation change and pass afterward. The full verify run passes.

Copilot AI balanced review requested due to automatic review settings October 30, 2026 17:13
@changeset-bot

changeset-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 088af11

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@styled/typescript-styled-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the legacy configuration-normalization changes.

No open issues 🎉

What changed in this PR

Hardens legacy configuration normalization for direct JavaScript callers of the public PluginConfigurationManager, without changing the editor configuration protocol or listener lifecycle.

Changes:

  • Copies accepted tag arrays and top-level configuration records while preserving nested references and unknown lint keys.
  • Validates array lengths and recovers from configuration read failures by restoring defaults, logging safely, and notifying listeners.
  • Adds regression coverage, documents the behavior, and includes a patch changeset.
File Description
test/​unit/​plugin-configuration.test.ts Adds regression cases for copying, special lint keys, invalid lengths, and throwing inputs.
src/​configuration/​plugin-configuration.ts Adds guarded normalization, shallow copies, safe lint property writes, and array-length validation.
docs/​architecture.md Documents recovery, copying, and listener behavior.
.changeset/​steady-legacy-configuration.md Records the configuration fixes for a patch release.

Review effort: Balanced

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

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

  • Reset on unreadable input: updateFromPluginConfig now wraps normalization in try/catch; a throwing getter, revoked proxy, or throwing length resets the whole configuration to defaults, logs one message, and still notifies listeners exactly once with listener callbacks kept outside the guard.
  • Mutation independence: accepted tag arrays are copied, and emmet/merged lint records are shallow-copied via Object.fromEntries, while nested values stay shared and lint.validProperties is rebuilt as a new array.
  • Safe array length: new getArrayLength rejects arrays whose reported length is not a nonnegative safe integer before iteration, applied to both tags and lint.validProperties.
  • Own-key preservation: unknown lint keys are written with Object.defineProperty, so __proto__ survives as an own data property without changing the record's prototype.
  • Tests/docs/changeset: seven targeted regression cases, an updated Configuration section in docs/architecture.md, and a patch changeset.

I re-ran the full unit suite (942 pass) and yarn typecheck, and confirmed the seven new tests fail when only src/configuration/plugin-configuration.ts is reverted to the base commit. The __proto__ behavior is preserved end to end (spread and Object.fromEntries both create an own data property) and is inert in the downstream vscode-css-languageservice lint configuration, which only reads own keys.

Pullfrog  | View workflow run | Using big-pickle (free) | 𝕏

@quantizor quantizor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for splitting this out. Looking at it on its own, I'd rather not take it. None of these changes fix a problem that tsconfig or configurePlugin JSON can trigger on main. Each one guards against something only a direct JavaScript caller can cause (getters, Proxies, a caller mutating its input after the call), or against an unused __proto__ lint key.

  • The catch-all try/catch in updateFromPluginConfig would hide future bugs. If a normalizer breaks later, every user's config quietly resets to defaults behind a generic log line instead of failing a test.
  • getArrayLength only ever rejects a Proxy around an array, since a real array's length is always valid. Main already normalizes the test's '1' length correctly, so this rejects a config that works today, while a Proxy reporting Number.MAX_SAFE_INTEGER still gets through.
  • Defining unknown lint keys with defineProperty makes the two sources disagree. TypeScript's tsconfig parser already turns __proto__ into the prototype, so the key disappears there, while a configurePlugin payload would keep it as an own key. No CSS lint rule reads it, and main doesn't touch Object.prototype today.
  • Copying lint again is a second copy. Main already spreads it into a fresh object. Only the Emmet and tags copies change anything, and only for a caller that mutates its input afterward.
  • The changeset describes internals a plugin user wouldn't notice. This PR also conflicts with #43 in the "Source" bullet of docs/architecture.md.

The one real problem here is the spec line saying the manager "normalizes any input shape and never throws", which overpromises for direct API callers. Narrowing that sentence to JSON-shaped input would make a welcome one-line docs PR. I'd suggest closing this one.

@usercao

usercao commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the detailed review, that makes sense. I agree these only matter for direct JS callers, so I'll close this one.

Opened #53 for the docs fix.

@usercao usercao closed this Nov 19, 2026
@usercao
usercao deleted the fix/legacy-configuration-normalization branch November 21, 2026 13:01
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.

3 participants