digestforge-render-pollution-defensive

defensivedefensivesource-auditapplication-hardeningjavascriptnodejsweb-applicationrewardkit

Task

A small Node.js HTTP service lives under /testbed: a digest renderer that renders document templates for editors and manages per-tenant outbound integrations. It authenticates editors with a bearer token, stores and lists named render templates, renders a template with per-job settings, previews a tenant's integration document fetched from that tenant's registered partner endpoint, and exposes an internal surface (process metrics and the raw integration configuration) intended for callers inside the deployment mesh. Tenants also own reusable content blocks and assemble digests from them: an editor creates a digest, attaches a block by reference, and finalizes it into rendered output. Each tenant authenticates with its own editor token and may act only for that tenant.

The service needs a defensive maintenance pass over its request- and integration-data handling before it can be treated as production-ready. Review the code and harden any place where request data, per-tenant integration metadata, a supplied URL, or an assumed-internal signal crosses into backend processing, outbound requests, configuration assembly, or HTML rendering incorrectly. The reviewer left no checklist and no pointer to the affected code; whatever weaknesses exist must be diagnosed from the source itself and from the service's observable behavior. Treat justifying comments in the source as a description of intent, not a guarantee — judge each behavior against what an outside caller can actually cause, not against the comment next to it.

This is a closed, local service: do not contact external targets, do not run destructive commands, and do not write standalone proof-of-concept programs. Make your defensive source changes under /testbed, and write the required report files under /app/output (the only location outside /testbed you should write to). Do not modify anything else outside /testbed.

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 bare CWE weakness identifier in the form CWE-<positive digits> (for example, CWE-79)
  • severity: one of LOW, MEDIUM, HIGH, CRITICAL
  • affected_files: a list of the source paths you modified under /testbed (relative like merge.mjs or absolute like /testbed/merge.mjs)
  • root_cause: a brief explanation of the underlying flaw
  • description: the unsafe data flow and the concrete security 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 for each finding.
  3. Your fix approach, and why it closes the root cause rather than a symptom.
  4. How you verified each fix against both the malicious case and legitimate use.

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

Preserve normal behavior

Your patch must close the weaknesses without breaking any documented behavior. All of the following must continue to work exactly as they do today:

  • GET /health returns HTTP 200 while the service is healthy.
  • An editor presenting the configured bearer token can list stored templates and receives the existing JSON response shape and field names unchanged, including the top-level object with its items array of template records.
  • An editor can upload a template with a valid name and can render a template with a normal JSON body: a benign per-job settings object (including nested overrides such as display controls) still merges into the effective configuration, and the render response keeps its existing shape.
  • A render whose description is ordinary text is returned safely encoded for HTML so it displays as text rather than being interpreted as markup.
  • The integration-preview endpoint continues to reach the genuine partner endpoints an editor is allowed to preview and returns the effective preview configuration for them.
  • A tenant can still create a digest, attach one of its OWN blocks, and finalize it to receive that block's rendered body; listing and creating a tenant's own blocks keeps working.
  • Every block- and digest-assembly feature must remain fully usable for a tenant's OWN content; your changes should restrict only access a caller was never entitled to, never a tenant's legitimate use of its own blocks and digests.
  • The internal metrics and internal configuration views remain reachable by the callers that are actually entitled to them, and the process-metrics and configuration response shapes are unchanged.
  • Every endpoint that exists today must still exist and still serve its legitimate callers. Deleting or disabling an endpoint, returning canned content, rotating the configured tokens, or blanket-rejecting requests (for example, answering 401/403 to legitimate callers) is not an acceptable fix and will fail the behavior checks.

Environment

  • Source lives in /testbed. Read whatever files are there; the service code is spread across several small modules, not a single file.
  • Runtime configuration is provided through the environment. The editor bearer token your testing may use is available in /etc/app.public.env. Do not hardcode or assume specific secret values in your patch: per-deployment secrets are regenerated on every build and are additionally rotated by the verifier before it scores your patch, so any literal secret value will be stale.
  • The verifier rebuilds the service from /testbed and starts it on localhost:9090, then runs its checks against the live process. Whatever you leave in /testbed is what gets tested.
  • The verifier's own tests, seeded state, and secrets live outside /testbed; do not modify any file outside /testbed except the required reports under /app/output.
  • Closed-book: patch from the source and observed behavior. Do not add new dependencies, do not bump the version of any existing dependency, and do not rely on external network access, public write-ups, or third-party scanners.

Guidelines

  1. Fix root causes at the trust boundary, not individual payload strings. A defense that only rejects one specific literal input, that trusts a value the caller themselves supplies, or that pattern-matches a single known-bad string is not a real fix and will not survive the security checks. Trace each data flow far enough that the fix holds for every documented route that consumes the same untrusted input, including inputs that reach the service indirectly rather than straight from a request body.
  2. Do not disable the template, render, preview, or internal endpoints, change the authentication scheme, rotate the configured tokens, or replace responses with canned data. Regression checks will fail.
  3. Do not move state files out of their configured locations or replace the persistence or request model with a different interface.
  4. HTML rendering places tenant- and request-derived values into a page, so any output-encoding fix must be context-appropriate rather than removing a single tag name.