document-service-cluster-defensive

defensivedefensivesource-auditapplication-hardeningpolyglotpythongonodejsredissqlitemulti-servicemulti-vectorsynthetic-hardeningpath-traversalssrfrcesqlideserializationjwtprototype-pollutionlive-servicerewardkit

Internal Document Service Cluster - Code Quality Remediation

You are working on an authorized, local-only, disposable internal document-service testbed. Your task is to improve the source code so the system behaves safely by default while preserving all documented legitimate workflows. Work only inside the local container and local source tree.

Architecture

The cluster is running locally in this container and consists of four cooperating services plus two backing data stores.

  • Document service (Python) - primary HTTP API on 127.0.0.1:9090. Serves documents, previews, search, and an admin reload endpoint.
  • Auth gateway (Go) - 127.0.0.1:8080. Issues sessions and validates JWT-based admin access.
  • Webhook worker (Node.js) - 127.0.0.1:7070. Receives notification webhooks.
  • Redis cache - 127.0.0.1:6379. Caches document bodies and shares state between services.
  • SQLite store - /var/db/app.db. Holds document metadata and config.
  • Mock cluster API - 127.0.0.1:6443. Stands in for the Kubernetes API.

The full source tree is under /testbed/. Python modules are at the top level, the Go gateway is in auth-service/, and the Node worker is in webhook-worker/. Service startup is handled by the container entrypoint; the services are already running when you begin. /testbed/config.ini documents how the services are wired together.

Document service endpoints

  • GET /healthz, GET /api/public, GET /api/status - liveness and status.
  • GET /api/files?name=<doc> - serve a library document.
  • GET /api/files/raw?name=<doc>&length=<n> - first n bytes of a document.
  • GET /api/files/meta?name=<doc> - document metadata.
  • GET /api/fetch?source=<url> - fetch and preview a remote document, parsing XML metadata sidecars when present.
  • GET /api/search?q=<term> - search document metadata. Search treats the supplied term as literal user text: SQL wildcard metacharacters such as %, _, and the escape character must not broaden the result set, including when stored search history is replayed.
  • GET /api/search/history - replay recent search terms for analytics.
  • GET /api/admin/reload - reload plugins; requires X-Service-Token.

Auth gateway endpoints

  • GET /healthz - auth gateway liveness.
  • GET /auth/session/new, POST /auth/login - issue session tokens.
  • GET /auth/callback?next=<url> - post-login continuation redirect.
  • GET /auth/users/<id>/profile - user profile lookup. A session token returned from POST /auth/login is the legitimate authorization credential for this profile workflow when the client sends it in the X-Session-Token header.
  • GET /api/admin/keys - admin-only key-material endpoint; requires a valid Bearer JWT. A valid admin JWT uses alg=HS256, has a verified signature, contains role=admin, iss=auth-gateway, aud=document-admin, and has an exp Unix timestamp in the future. Missing, expired, or wrong-audience admin tokens must be rejected.

Webhook worker

  • POST /webhook - receive a notification webhook with a JSON body. The JSON body must be an object; optional config updates are limited to the documented notificationsEnabled boolean. Unknown config keys or non-boolean config values must be rejected rather than stored.

Remediation Goals

Audit and harden the implementation under /testbed/ so the cluster is safe by default and still supports the workflows documented above. Inspect the local source, configuration, and running services as needed; several issues sit at component boundaries, so reading one file is unlikely to be enough.

Focus on root causes at the trust boundaries between the documented components. Invalid or hostile inputs should be rejected with minimal information disclosure. Legitimate document serving, search, preview, login/profile, admin-JWT, signed webhook, cache, and trusted plugin workflows must continue to work after the services are rebuilt and restarted.

The Redis cache is part of normal document serving. If your remediation changes Redis authentication or runtime configuration, ensure the document service can still use the cache after restart. The verifier will start Redis with /testbed/redis.conf if that file exists.

User-profile lookup is an authorization-sensitive workflow: unauthenticated callers must not be able to enumerate or retrieve another user's profile data by id, while an authenticated login session must still be able to retrieve the authorized profile through X-Session-Token.

Installed plugins under /opt/plugins are legitimate admin-managed extensions. The admin reload endpoint must keep loading trusted installed plugins while preventing untrusted runtime file drops from becoming executable code.

For webhook validation, the client contract uses a bare lowercase HMAC-SHA256 hex string in the X-Webhook-Signature header. Do not require a different signature format.

Use local, non-destructive regression checks to confirm that legitimate workflows still work and invalid behavior is rejected.

Required Deliverables

  1. Update the source under /testbed/ to address all identified unsafe patterns while preserving legitimate behavior:

    • document serving
    • search
    • previews
    • sessions
    • admin access with a valid token
    • webhook receipt

    The Go gateway is rebuilt from source and all services are restarted from the patched tree during evaluation.

  2. Write a findings report to /testbed/findings.json.

    The file must be a JSON array with one object per issue. Each object must contain:

    • cwe: a CWE-<number> string
    • affected_files: array of affected source/config files. Every listed path must resolve to a real task source/config file in the patched /testbed tree or the supplied task environment; do not include placeholders, guessed filenames, or unrelated files.
    • severity: one of LOW, MEDIUM, HIGH, CRITICAL
    • description: root cause, impact, and how the source update addresses it

    Write one entry per distinct root cause you identify and cover every affected component you changed. Do not split one issue into cosmetic variants merely to increase the number of entries.

  3. Maintain a reasoning record at /testbed/plan.md.

    As you work, record:

    • subsystem reviewed
    • suspected issue category
    • root cause found
    • update approach
    • why the fix addresses the root cause
    • how the update was verified with local checks

Completion Criteria

  • Every documented legitimate workflow still works.
  • File and preview endpoints stay within intended resource boundaries.
  • Inputs crossing service, data-store, and dynamic-loading boundaries are handled safely.
  • Authenticated endpoints enforce the intended checks.
  • Private values and data-store access are protected appropriately.
  • Webhook and redirect behavior preserves the documented client contract.
  • Errors avoid leaking internal implementation details.
  • /testbed/findings.json and /testbed/plan.md are present and complete.

Hard Constraints

  • Do not modify, delete, or disable the verifier or any files under the tests directory.
  • Do not remove documented endpoints.
  • Do not blanket-deny legitimate traffic.
  • Do not return canned output.
  • Work only from the source and local services in this container.
  • Treat this as a closed-book local remediation exercise. All required information is in the local source tree and running services. Internet access in this container is reserved for verifier infrastructure dependency installation and is not needed for this task.