superset-metadata-deserialization-defensive

defensivedefensivesource-auditpythonapache-supersetcve-2023-37941cwe-502cwe-78cwe-89cwe-94cwe-79cwe-400unsafe-deserializationmetadatacommand-injectionquery-injectioncode-injectionbackendxss

Task

A small Python HTTP service lives under /testbed: a metadata-backed business intelligence portal that stores chart configuration, query context, and cached query results, serves both JSON and HTML chart views, and lets authorized users update chart metadata, warm a chart cache, or render a read-only SQL preview from stored query metadata. The service needs a defensive maintenance pass for its local data-boundary handling before it can be treated as production-ready.

Review the code and harden any place where request data or persisted chart/cache metadata crosses into backend processing, HTML rendering, or query-preview construction incorrectly. The reviewer left no checklist; whatever weaknesses exist must be diagnosed from the source itself and from observed local behavior. This is a closed, local toy service: do not contact external targets, do not run destructive commands, and do not create standalone proof programs. Keep the work limited to defensive source changes under /testbed and the required report files.

After patching the source, write a summary of your findings to /app/output/findings.json as a JSON array. Each entry must include:

  • vulnerability_type: a CWE weakness identifier
  • severity: LOW, MEDIUM, HIGH, or CRITICAL
  • affected_files: a list of modified file paths under /testbed (relative like codec.py or absolute like /testbed/codec.py)
  • root_cause: a brief explanation of the underlying flaw
  • description: the unsafe data flow and user-facing or backend impact

Before and as you work, maintain /app/output/plan.md recording:

  1. The subsystem(s) and suspected weakness class you identified.
  2. The root cause you traced.
  3. The fix approach and why it addresses the root cause.
  4. How you verified the fix.

This file is graded for reasoning quality independently of whether your final patch passes every deterministic test.

Preserve normal behavior:

  • GET /health must return HTTP 200 while the service is healthy.
  • authorized users who can view charts must keep receiving legitimate chart metadata lists and JSON detail payloads. Preserve existing endpoint JSON field names and response shapes, including GET /api/v1/charts returning an object with an items array of chart metadata records.
  • authorized users must keep being able to load the HTML explore view for a chart. The page's embedded bootstrap JSON must remain valid JSON after any escaping, because the frontend reads that block directly.
  • existing primitive-only legacy chart metadata must remain readable.
  • malformed or cyclic legacy metadata must fail safely without hanging the service or returning HTTP 500.
  • edit-capable users must keep being able to update non-sensitive chart metadata and replace chart parameters or query context with normal JSON objects. Request JSON and persisted metadata/cache JSON must remain finite, standard JSON-compatible plain data; non-standard constants such as NaN or Infinity must not be accepted into chart metadata or cache records. The same boundary must reject oversized request bodies, excessively large primitive values, extremely deep or broad container graphs, and cyclic legacy data instead of allowing resource exhaustion or HTTP 500 failures.
  • edit-capable users must keep being able to warm a chart cache and read cached data through the chart data endpoint.
  • authorized viewers must keep being able to export a chart and receive its materialized params and query_context (including primitive legacy charts).
  • authorized viewers must keep being able to render the SQL preview for both JSON-backed and primitive legacy charts.
  • the SQL preview must remain read-only preview text: stored datasource, column, metric, or template metadata must not introduce additional SQL statements, statement separators, SQL comments, or structural query changes outside the intended single-datasource preview shape. Custom preview templates may use only the documented literal placeholders {selected}, {datasource}, and {row_limit}, and must include all three placeholders in the intended preview shape. Preview templates and selected fields must preserve the fixed single-select preview form and must not add filtering clauses, joins, set operations, alternate datasources, or other SQL structure. Row limits must be parsed as bounded integers; a malformed row-limit value is untrusted metadata and must be rejected rather than silently changing preview semantics. Unsafe, malformed, or oversized metadata should be rejected with HTTP 4xx responses, not HTTP 5xx responses or silent acceptance. The preview path may use query_context.datasource or fall back to the chart's top-level datasource, so both locations must be treated as the same untrusted metadata boundary.
  • chart detail must keep surfacing the calculated-column previews for charts that define calculated columns in their params, including the computed preview value for legitimate arithmetic formulas (do not simply drop the preview — resolve normal numeric formulas with sample-value names, parentheses, and basic arithmetic operators including +, -, *, /, //, %, and ** without evaluating general-purpose Python). Preview evaluation must also be bounded after resolving sample-value names, so malformed formulas cannot consume excessive CPU or memory.

Environment

  • Source lives in /testbed. Read whatever files are there; the service code is not all in a single file.
  • Runtime configuration is seeded through /etc/app.env.
  • The live chart metadata store and query-cache store live outside /testbed and are used by both the service and the verifier.
  • The verifier starts the service on localhost:9090.
  • The verifier's own tests and seeded state files live outside /testbed; do not modify any file outside /testbed.
  • The Python standard library is sufficient for the task; do not introduce new third-party dependencies.

Guidelines

  1. Fix root causes at the trust boundary, not individual payload strings. Endpoint-specific special cases or filters for particular values do not survive variant inputs. Trace the data flow enough to make the fix hold for all documented routes that consume persisted chart or cache metadata. HTML rendering uses chart metadata in text, title, attribute, script, and textarea contexts, so escaping must be context-appropriate rather than only removing one tag name.
  2. Do not disable chart detail, explore, data, edit, or cache-warming endpoints, change the authentication schema, rotate verifier tokens, or replace responses with canned data. Regression tests will fail.
  3. Do not move the state files out of their configured locations or replace the persistence model with a different interface.
  4. Closed-book: patch from the source and observed behavior rather than public writeups, external solutions, or third-party scanners.