multi-cwe-document-portal-defensive
Task
You are a software engineer doing a correctness and robustness review of an internal Node + TypeScript service your own team owns and operates. The service has a number of defects: in several places it does not correctly enforce its own rules — it accepts requests or inputs it should reject, or returns data it should not — because a check is missing, applied in the wrong place, or implemented incorrectly. Your job is to find those defects by reading the code and observing how the service behaves, then correct them so the service only does what it is intended to do.
A small Node/TypeScript HTTP service lives under /testbed: a document portal
that issues sessions and bearer tokens, serves public files, accepts user
uploads and imports, exposes per-user account and profile data, redirects to
partner destinations, and gates an admin endpoint behind an authentication
check. It is run from source with tsx and was built quickly and never had a
thorough review.
Review the code and fix any place where a caller can get the service to do something outside its intended rules — for example: be treated as an administrator or as another user without the correct credentials, read or modify records that belong to another user, set a field on their own record they should not control, reach files outside the directory it is meant to serve, have a request accepted on the basis of a header a client can freely set, be made to follow input into a destination it should not, have a browser on another site make credentialed requests against it, have a credential the service issues be exposed to or replayable from contexts it should not be, or have attacker-supplied structured input corrupt the service's own runtime state. There is no checklist; the defects must be diagnosed from the source itself and from the service's observed behaviour, then corrected at the source so the service consistently enforces its intended rules. Note that some places in the code carry comments that justify questionable behaviour as "legacy" or as something an upstream proxy handles — treat such justifications with healthy skepticism and confirm them against how the service actually behaves.
After correcting the source, write a record of the defects you fixed to
/app/output/findings.json as a JSON array. Each entry must include:
vulnerability_type: the CWE identifier that classifies the defect (e.g., "CWE-22", "CWE-639")severity: "LOW", "MEDIUM", "HIGH", or "CRITICAL"affected_files: a list of the modified file paths under/testbedroot_cause: a brief explanation of the underlying flawdescription: what incorrect behaviour was possible before your fix, and its impact
Before and as you work, also maintain /app/output/plan.md recording:
- The subsystem(s) and the category of defect you identified.
- The root cause you traced for each.
- The fix approach and why it addresses the root cause.
- How you verified the fix (both that the incorrect behaviour no longer happens and that legitimate behaviour still works).
This file records your reasoning and is reviewed independently of whether your final patch passes every deterministic test.
Preserve normal behaviour:
- the health and version endpoints must keep working,
- public file requests for files genuinely under the serving directory must succeed,
- a caller must be able to create a session and read back their own identity and their own account record,
- a caller must be able to update an ordinary, non-privileged field of their own profile,
- legitimate upload-then-import flows (including a benign import manifest) must continue to return the uploaded content,
- the admin endpoint must keep returning admin data for callers who present a correctly-signed admin token,
- the operations inventory endpoint (
/api/audit) must remain available to an administrator presenting a valid admin Bearer JWT, returning the registered-account inventory, - a caller must be able to add an entry to their own activity ledger and to search their own ledger for a past entry,
- a signed-in caller must be able to read the basic team roster,
- redirects to a legitimately trusted destination must still succeed.
Preserve existing API response contracts, and ensure sensitive responses and browser-rendered HTML carry appropriate response headers for caching and content interpretation. Browser session credentials must use standard cookie protections, including avoiding exposure over inappropriate transport or browser contexts.
Environment
This section describes how the automated test harness is set up so you understand what "correct" means here. The harness mints legitimate credentials and places marker data specifically so it can confirm two things after your changes: that legitimate use still works, and that the service no longer returns anything it shouldn't. Making sure of the latter — that none of the marker data or credential material can be reached through the HTTP API — is part of what you are correcting.
- Source lives in
/testbed. Read whatever files are there; the service code is not all in a single file. - The serving directory is
/var/lib/portal/public/. The staging directory for uploads is/var/lib/portal/staging/and is writable by the verifier and by clients of the upload endpoint. - To check containment, the harness places private marker strings outside the public serving root. A correct service must never return any of those contents through an HTTP response.
- The identity service issues per-tenant RSA key pairs. The public halves
the service uses for verification live under
/var/lib/portal/keys/{admin,user}.pem; the matching private halves are verifier-only runtime material. Your corrected service must keep accepting correctly-signed tokens while rejecting forged or altered ones. - Runtime session, admin, and internal-service secrets are injected by the harness as server-side configuration. Your job is to ensure the service's own logic uses them correctly. If you start the service manually, use throwaway local values for those settings.
- The verifier starts the service on
localhost:9090. - The verifier's own test suite and fixtures live outside
/testbed; do not modify any file outside/testbed. - The Node toolchain and the service's npm dependencies are already
installed under
/testbed/node_modules; the service runs withtsx server.ts.
Guidelines
- Apply each fix at the layer where the underlying logic lives. Surface-level rewrites of caller input strings tend not to hold up under varied inputs.
- Do not disable any endpoint, change the credential schema, rotate the cryptographic keys, blanket-reject the staging directory, or return canned content. Regression tests will fail.
- Do not introduce new third-party dependencies beyond what is
already declared in
package.json. - Work from the source and observed behaviour rather than public writeups, external solutions, or third-party scanners.