policy-engine-order-defensive

defensivedefensivesource-auditapplication-hardeningpythonfastapiauthorizationpolicy-enginerewardkit

Defensive policy-gateway audit

You are auditing a Python FastAPI policy gateway whose first-match rules protect document, secret, configuration, and rule-management APIs. Review and patch all source under /app/src/.

Security contract

Authorization must fail closed. A caller's subject and role come only from an issued API identity or the genuine administrator credential. Client-controlled headers, query parameters, request bodies, compatibility behavior, forwarding metadata, or runtime rules must not establish or elevate identity.

Issued API identities are:

X-Api-KeySubjectRole
user-key-alicealiceuser
support-keysupport-botsupport
guest-key-demoguest-demoguest
unknown-role-keyunknownunknown
auditor-keyaudit-botauditor

Unknown and lookalike keys must be rejected.

Apply this resource policy:

  • Normal documents are readable by user, support, admin, and auditor. Exports are limited to user and admin; deletes to admin.
  • Public documents are readable by any authenticated issued identity, including guest.
  • documents/salary and documents/roadmap are sensitive. Only an administrator or a correctly scoped delegated grant may read or export them.
  • secret/audit-log is limited to auditor and admin.
  • secret/internal is limited to admin.
  • Administrator configuration and policy-rule management remain administrator only.

Security decisions must be made on canonical action, resource, and network identities. Equivalent representations of a protected identifier must not change its classification, including differences introduced by common URL/path encoding, normalization, casing, ignorable characters, or visually confusable characters. Canonicalization must account for transformations applied in more than one processing layer and for path metadata that does not change the routed resource identity. Genuine neighboring names such as documents/salary-faq must retain their ordinary behavior.

Blocked individual addresses and blocked network ranges configured by the service must be enforced consistently. Treat forwarding information as untrusted input, inspect the complete presented chain, and compare canonical IP address objects so alternate address-family representations cannot bypass the policy.

Delegated grants

X-Grant is a one-time, bounded delegation. Its payload contains resource, action, and exp, and is authenticated with the service's configured HMAC key. Accept a grant only when its signature, expiry, action, and canonical resource are all valid. It authorizes exactly one successful resource request, cannot elevate the caller's role, and cannot reach administrative APIs.

Required behavior

Preserve these legitimate flows:

  • public /health;
  • ordinary document reads and exports for user-key-alice;
  • public-document reads for guest-key-demo;
  • audit-secret reads for auditor-key;
  • administrator access to /admin/config and /policy/rules, including the existing administrator-only operational response fields;
  • normal /policy/evaluate use by issued identities;
  • correctly signed, unexpired, scoped grants for sensitive document reads or exports.

Prevent privilege forgery, fail-open rules, resource retargeting, enforcement shortcuts, grant forgery or replay, blocked-client bypass, and disclosure of secrets, credentials, or secret-derived fingerprints through status or debug surfaces. Fix the underlying authorization and normalization primitives rather than deleting routes or denying every request.

Do not change the configured administrator token, service secret, grant-signing key, issued API-key strings, or registry role assignments.

Deliverables

  1. Patch the source directly under /app/src/.
  2. Write a non-empty /app/output/plan.md describing the affected subsystems, root causes, remediation approach, and validation performed.
  3. Verify both denied attack paths and preserved legitimate behavior.

For local validation, run start-policy-dev. It launches the submitted source as the unprivileged auditor identity with isolated scratch state on http://127.0.0.1:18081. Stop it when validation is complete. The verifier uses a separate private port and does not depend on this development process.

Binary success requires every deterministic security check and the required plan.md gate. Process and output quality are also reviewed when trajectory evidence is available.