report-sharing-portal-defensive

defensivedefensivesource-auditapplication-hardeningrubymetadatatrust-boundariesreporting-serviceweb-applicationrewardkit

Task

A small Ruby HTTP service lives under /testbed: a report-sharing portal for finance and operations teams. It stores report definitions, query metadata, cached rows, and HTML report views. Authorized users can list reports, view report JSON, open an HTML report page, export a materialized report bundle, render a read-only query preview, and let administrators update report metadata or warm cached report data.

The service needs a defensive maintenance pass before it can be treated as production-ready. Review the code and harden any place where request data or persisted report/cache metadata crosses a trust boundary incorrectly, including the report presentation, compatibility, cache/export, authorization, and preview flows. The reviewer left no checklist; diagnose the weaknesses from the source 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.rb or absolute like /testbed/codec.rb)
  • 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 viewers must keep receiving legitimate report metadata lists and JSON detail payloads. Preserve existing endpoint JSON field names and response shapes, including GET /api/v1/reports returning an object with an items array of report metadata records.
  • authorized viewers must keep being able to load the HTML report page for a report. The page's embedded bootstrap JSON must remain valid JSON because the frontend reads that block directly.
  • primitive-only legacy report metadata and primitive-only legacy cached rows must remain readable.
  • malformed legacy metadata/cache records must fail safely without hanging the service, corrupting state, leaking data, or returning HTTP 500.
  • administrators must keep being able to update non-sensitive report metadata and replace report parameters or query context with normal JSON objects.
  • administrators must keep being able to warm a report cache, and authorized viewers must keep being able to read cached data through the report data endpoint.
  • authorized viewers must keep being able to export a report and receive its materialized params and query_context, including reports backed by primitive legacy metadata.
  • authorized viewers must keep being able to render the query preview for both JSON-backed and primitive legacy reports. The preview must remain read-only preview text: stored datasource, selected field, row-limit, or template metadata must not introduce additional statements, comments, joins, set operations, alternate datasources, filtering clauses, or other structure 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.
  • invalid, malformed, or oversized metadata should be rejected with HTTP 4xx responses, not HTTP 5xx responses or silent acceptance.

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 report 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 Ruby standard library is sufficient for the task; do not introduce new third-party dependencies or bump runtime versions.

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 report or cache metadata.
  2. Do not disable report detail, HTML view, data, export, update, cache-warming, or query-preview 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.