Skip to content

fix: show stored config that is not a JSON object as text and block unsafe saves - #718

Open
bhaskargurram-ai wants to merge 2 commits into
apache:masterfrom
bhaskargurram-ai:fix/guard-json-parse-config
Open

bhaskargurram-ai wants to merge 2 commits into
apache:masterfrom
bhaskargurram-ai:fix/guard-json-parse-config

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown
Contributor

Plugin config, plugin-handle extObj and API-document ext/document are stored as strings and were passed straight to JSON.parse inside render. A record whose string is malformed, or is valid JSON that is not an object, threw during render and blanked the Home page, the Plugin and NamespacePlugin lists, the plugin edit forms and the API-document form.

src/utils/jsonObject.js adds parseJsonObject, which never throws and returns { value } for a JSON object, { value: undefined } for null/empty and { error } for everything else, plus formatJsonObject for read-only views. The Home, Plugin and NamespacePlugin popovers pretty-print a JSON object as before and otherwise render the original string as text, which React escapes.

The Plugin and NamespacePlugin edit forms keep the stored string. When it is not a JSON object the schema fields are replaced by a textarea holding the original text with a validation error shown on open, and the form cannot be saved until that text is a JSON object; the corrected text is then saved as the config. Nothing is substituted with {}, so a save can no longer overwrite the stored value with an empty object. A field whose plugin-handle extObj is not a JSON object renders without its required/default/rule metadata instead of throwing. The API-document form shows ext and document as text with an error status when they are not JSON objects; isJsonStr already blocks saving a bad ext and is unchanged.

Tests cover null/empty values, malformed strings and valid JSON of an unexpected type as separate cases in src/utils/jsonObject.test.js, src/routes/System/Plugin/AddModal.test.js, src/routes/System/NamespacePlugin/AddModal.test.js and src/routes/Document/components/AddAndUpdateApiDoc.test.js. Against master, 8 of the new tests fail with the uncaught SyntaxError; with this change npm run test:unit passes 308 tests in 58 suites, npm run lint and prettier --check are clean and npm run build succeeds.

Fixes #612

…nsafe saves

Plugin config, plugin-handle extObj and API-document ext/document are
stored as strings and were passed straight to JSON.parse inside render.
A record whose string is malformed, or valid JSON that is not an object,
threw during render and blanked the Home page, the Plugin and
NamespacePlugin lists, the plugin edit forms and the API-document form.

Add parseJsonObject and formatJsonObject in src/utils/jsonObject.js.
The read-only views pretty-print a JSON object as before and otherwise
render the original string as text. The Plugin and NamespacePlugin edit
forms keep the stored string: when it is not a JSON object the schema
fields are replaced by a textarea holding the original text with a
validation error, and the form cannot be saved until that text is a
JSON object. Nothing is substituted with {}. A field whose extObj is
not a JSON object renders without its metadata instead of throwing. The
API-document form shows ext and document as text with an error status
when they are not JSON objects.

Fixes apache#612

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.

🟡 Changes recommended

Both plugin recovery validators accept cleared text, allowing configuration to be overwritten without a valid JSON object.

2 open findings
What changed in this PR

Addresses #612 by safely displaying malformed or non-object stored JSON across dashboard views and adding recovery controls to plugin forms.

Changes:

  • Adds shared JSON-object parsing and display helpers.
  • Replaces unsafe rendering with raw-text fallbacks and validation feedback.
  • Adds regression tests for parsing, rendering, and configuration recovery.
File Description
src/​utils/​jsonObject.test.js Tests parsing and display behavior.
src/​utils/​jsonObject.js Adds safe parsing and formatting helpers.
src/​routes/​System/​Plugin/​index.js Safely displays stored configuration.
src/​routes/​System/​Plugin/​AddModal.test.js Tests configuration recovery and metadata handling.
src/​routes/​System/​Plugin/​AddModal.js Adds raw-text recovery and guarded metadata parsing.
src/​routes/​System/​NamespacePlugin/​index.js Safely displays namespace-plugin configuration.
src/​routes/​System/​NamespacePlugin/​AddModal.test.js Tests recovery and malformed metadata.
src/​routes/​System/​NamespacePlugin/​AddModal.js Adds recovery validation and guarded parsing.
src/​routes/​Home/​index.js Safely formats configuration popovers.
src/​routes/​Document/​components/​AddAndUpdateApiDoc.test.js Tests object rendering and raw-text fallbacks.
src/​routes/​Document/​components/​AddAndUpdateApiDoc.js Displays invalid JSON as text with errors.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/routes/System/NamespacePlugin/AddModal.js Outdated
Comment thread src/routes/System/Plugin/AddModal.js Outdated
The recovery text area accepted an empty value, because parseJsonObject
treats empty input as "no config" rather than an error, so clearing the
field and saving erased the stored configuration. Both plugin forms now
require the text to parse as a JSON object. Adds a clear-then-save test
for each form.

This branch has not been deployed

No deployments
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.

[BUG] Unguarded parsing of malformed configuration strings can break affected views

2 participants