procurement-ingest/lambdas/api/pagination.py

63 lines
2.4 KiB
Python
Raw Permalink Normal View History

feat(api): procurement-api read stack + OpenAPI docs (SHOC reconciliation path) (#127) * feat(api): add procurement-api stack - read API + OpenAPI docs page Third CDK stack: API Gateway REST API (IAM SigV4) over both pipelines' tables, replacing SHOC's retired SyncController cross-account DynamoDB scan as the reconciliation/backfill path. - lambdas/api/: handler (healthcheck + docs-token gate + router dispatch), router (single route table), pagination (opaque cursor, hostile -> 400), Decimal-safe serialization, wo_repo/po_repo reads. No VendorReplies. - OpenAPI 3.1 spec as source of truth incl. top-level webhooks section documenting the outbound SHOC feed; phase-2 write endpoints x-planned (router answers 501). Self-contained /docs page, no CDN. - Auth: AWS_IAM on data routes + resource policy scoped to exactly arn:aws:iam::396287094661:role/shoc-backend-dev on GET/*; /docs and /openapi.json carve-out is token-gated in the Lambda via shared web_ui_auth (fail-closed, INFRA-74 posture). - KMS: explicit Decrypt/DescribeKey on the DynamoDB CMK from SSM (name-imported table drops the key association - INFRA-104 class). - Alarms: errors/throttles/duration(p99>=22.5s) + gateway 5xx, ALARM-only to site-alerts. No access logging in v1 (docs ?token= shim stays out of logs); cloud_watch_role=False. - Tests: handler auth-seam + routing + Decimal round-trip; moto cursor pagination incl. hostile cursors; spec<->router drift gate; bundle AST pins for the api command; pytest.ini --cov + loader siblings. - Deploy role: third stack DescribeStacks ARN + procurement-api smoke invoke ARN (re-run create-deploy-role.sh before merge). * harden(api): apply sh-security-review findings to procurement-api Fan-out (6 detectors) + review findings resolved: Correctness / DoS: - pagination: require EXACT key-set match (was subset) so a partial/foreign composite cursor can't reach DynamoDB as an inconsistent ExclusiveStartKey -> ValidationException -> 500; comments Query now pins the cursor's work_order_id to the path entity. - handler: map botocore ValidationException to 400 (defense in depth) so a crafted cursor can't drive the zero-threshold 5xx alarm. - web_ui_auth: compare tokens as bytes; a non-ASCII presented token now fails closed (401) instead of crashing hmac.compare_digest into a 500. Resolves the pre-existing xfail(strict) follow-up test; hardens the web UIs too. Docs page: - typeStr() now escapes the one spec-derived string that reached innerHTML. - spec inlined into the docs <script> block escapes "<" -> < (</script> breakout guard); /openapi.json still served byte-faithful. - Cache-Control: no-store + Referrer-Policy: no-referrer on docs responses so the ?token= URL stays out of caches/Referer. - spec-drift test asserts the committed spec carries no "</" / "<!--". IAM / IaC: - resource policy enumerates the 7 data GET resources instead of GET/* so a future GET route can't silently inherit SHOC cross-account reach. - kms:Decrypt grant gains a kms:ViaService=dynamodb condition. - stage throttling (50 rps / 100 burst) bounds the unauthenticated /docs blast radius below the 10k account default. - corrected the PATCH/POST comment (same-account callers aren't blocked by the resource policy; 501 handler + absent write grant are the gate). - documented the RETAIN log-group first-deploy rollback trap and the resource-policy-needs-redeploy gotcha in-stack. Mandatory GPT-4.1 cross-family review of the full policy surface: no BLOCK/FIX. 675 tests pass, ruff clean, cdk synth green.
2026-07-23 19:32:20 -04:00
"""Opaque cursor pagination over DynamoDB ``LastEvaluatedKey``.
The cursor is base64url(JSON(LastEvaluatedKey)). It is untrusted client input:
``decode_cursor`` validates shape strictly (a flat dict whose keys are exactly
a subset of the table's key attributes and whose values are non-empty strings)
and raises ``BadCursor`` -- mapped to HTTP 400 by the handler -- on anything
else, so a malformed or tampered cursor can never reach DynamoDB as an
arbitrary ``ExclusiveStartKey`` or surface as a 500.
"""
import base64
import binascii
import json
DEFAULT_LIMIT = 100
MAX_LIMIT = 500
_MAX_CURSOR_CHARS = 2048
class BadCursor(ValueError):
pass
def clamp_limit(raw) -> int:
if raw is None or raw == "":
return DEFAULT_LIMIT
try:
value = int(raw)
except (TypeError, ValueError):
raise BadCursor("limit must be an integer") from None
return max(1, min(MAX_LIMIT, value))
def encode_cursor(last_evaluated_key: dict) -> str:
raw = json.dumps(last_evaluated_key, default=str, sort_keys=True)
return base64.urlsafe_b64encode(raw.encode()).decode()
def decode_cursor(cursor: str, key_attrs: frozenset) -> dict:
"""Decode a cursor and require it to be EXACTLY the table's key attributes.
``key_attrs`` is the full key schema of the operation the cursor feeds
(e.g. ``{work_order_id, comment_id}`` for the comments Query). An exact
match -- not a subset -- is required: a partial composite key or a cursor
minted for a different endpoint would otherwise pass a subset check, reach
DynamoDB as an incomplete/inconsistent ``ExclusiveStartKey``, and raise a
ValidationException that surfaces as a 500 (and pages the 5xx alarm). Here
it fails closed as a 400 instead. Callers additionally pin the
partition-key value to the request path (see ``wo_repo.list_comments``).
"""
if len(cursor) > _MAX_CURSOR_CHARS:
raise BadCursor("cursor too long")
try:
decoded = json.loads(base64.urlsafe_b64decode(cursor.encode()))
except (binascii.Error, UnicodeDecodeError, json.JSONDecodeError, ValueError):
raise BadCursor("cursor is not valid") from None
if not isinstance(decoded, dict) or set(decoded) != key_attrs:
raise BadCursor("cursor is not valid")
for value in decoded.values():
if not isinstance(value, str) or not value:
raise BadCursor("cursor is not valid")
return decoded