Skip to content

fix(backend): centralize request boundary validation - #8367

Merged
Git-on-my-level merged 4 commits into
mainfrom
david/zach-boundary-contracts
Jun 25, 2026
Merged

Git-on-my-level merged 4 commits into
mainfrom
david/zach-boundary-contracts

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

Summary

This aggregates the high-signal request/protocol-boundary fixes from @ZachL111 into shared validation helpers instead of endpoint-by-endpoint patches.

Changes:

  • add utils.request_validation for multipart JSON form parsing, bounded query params, real calendar dates, UTC-safe sync filename timestamps, and image chunk envelopes
  • route app/persona multipart JSON parsing through the shared helper
  • enforce bounded list/history query parameters and real calendar dates at request boundaries
  • validate sync upload filename timestamps with one UTC-aware parser for v1/v2 paths
  • reject invalid websocket image chunk envelopes/sequences instead of silently ignoring malformed chunks
  • validate parallel event/action-item status arrays on the Pydantic request models
  • replace a previous sys.meta_path/MagicMock-heavy app JSON test with direct helper contract coverage

Supersedes/redesigns parts of #8218, #8219, #8220, #8222, #8227, #8231, #8260, #8263, #8275, #8278, #8279, #8280, #8295, #8303, #8309, #8321.

Original issue discovery, reproductions, and candidate fixes by @ZachL111 — thank you for surfacing these boundary bugs. This PR keeps the useful findings while moving the implementation toward shared contracts and smaller tests.

Deferred from the Oracle boundary bucket

Not included here because they need separate design or would broaden this PR:

Test plan

  • pytest tests/unit/test_request_validation_contracts.py tests/unit/test_sync_file_paths_filename_none.py tests/unit/test_apps_create_app_json.py tests/unit/test_conversation_events_bounds.py -q → 34 passed
  • python -m py_compile ... over modified modules/tests
  • git diff --check
  • bash backend/testing/e2e/run.sh -q --tb=short → 73 passed, 6 skipped

Review in cubic

- add shared request validation helpers for multipart JSON, query bounds, calendar dates, sync filenames, and image chunks
- validate app/persona form JSON through a common helper
- enforce bounded list/history query parameters and real calendar dates
- validate sync upload timestamps with UTC-aware parsing
- reject invalid websocket image chunk envelopes instead of silently ignoring them
- validate parallel event/action-item status arrays at the request-model boundary

Supersedes parts of #8218, #8219, #8220, #8222, #8227, #8231, #8260, #8263, #8275, #8278, #8279, #8280, #8295, #8303, #8309, #8321.
Original issue discovery, reproductions, and candidate fixes by @ZachL111.

Co-authored-by: ZachL111 <[email protected]>
This was referenced Jun 25, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d98c3d48db

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread backend/routers/sync.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 issues found across 15 files

Confidence score: 3/5

  • In backend/routers/sync.py, get_timestamp_from_path no longer accepts bare {timestamp}.wav VAD segment names because parse_sync_filename_timestamp only handles underscore-delimited formats, which can break sync processing for existing files—restore bare-timestamp support before merging to avoid a regression.
  • In backend/routers/apps.py, the auth_steps URL guard can still raise on malformed auth_steps types, causing a 500 instead of a client-facing 422; this turns bad input into server errors—add type-safe validation/guarding so invalid payloads are rejected cleanly.
  • In backend/routers/apps.py, whitespace-only webhook_url values bypass the required-field check and can persist as effectively empty URLs—trim then validate non-empty content before accepting updates.
  • backend/routers/sync.py and backend/utils/request_validation.py include redundant/dead validation branches, which is low severity but increases drift risk over time—remove or consolidate these checks to keep one clear validation contract.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/routers/apps.py">

<violation number="1" location="backend/routers/apps.py:499">
P2: `webhook_url` required check is bypassed by whitespace-only values, allowing empty webhook URLs after trimming.</violation>

<violation number="2" location="backend/routers/apps.py:745">
P2: The new auth_steps URL guard can still crash with malformed `auth_steps` types, returning 500 instead of a validation 422.</violation>
</file>

<file name="backend/routers/sync.py">

<violation number="1" location="backend/routers/sync.py:673">
P1: `get_timestamp_from_path` now rejects VAD segment filenames like `{timestamp}.wav` because `parse_sync_filename_timestamp` only parses underscore-delimited names. Keep support for bare timestamp filenames here, otherwise sync jobs can fail after VAD when segment paths are sorted/parsed.</violation>

<violation number="2" location="backend/routers/sync.py:695">
P3: Timestamp range validation is duplicated after delegating to `parse_sync_filename_timestamp`, leaving redundant dead-check logic that can drift from the shared contract.</violation>
</file>

<file name="backend/utils/request_validation.py">

<violation number="1" location="backend/utils/request_validation.py:91">
P3: `self.index >= cached_total` check is unreachable after prior validators/checks, so this branch is dead code.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread backend/routers/sync.py Resolved
Comment thread backend/routers/apps.py Resolved
Comment thread backend/routers/apps.py Resolved
Comment thread backend/routers/sync.py Resolved
Comment thread backend/utils/request_validation.py Resolved

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the timestamp parser regression.

Change:

  • parse_sync_filename_timestamp now accepts both upload names (audio_<timestamp>.bin) and generated VAD segment names (<timestamp>.wav), while preserving the same seconds/millis normalization and range validation.

Verification:

  • ENCRYPTION_SECRET=*** VIRTUAL_ENV= uv run pytest tests/unit/test_request_validation_contracts.py -q → 25 passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c3a5dab556

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread backend/utils/request_validation.py Resolved
Comment thread backend/utils/request_validation.py Resolved
Add hermetic route-level coverage for validation behavior introduced by the request-boundary aggregate PR:
- malformed app/persona multipart JSON is rejected before fake storage writes
- real touched routes reject invalid query/date bounds without 500s
- v2 sync uploads reject invalid filename timestamps before job/storage side effects
- invalid listen image chunks close the WebSocket with policy violation

Timed hermetic e2e locally:
- before: 73 passed, 6 skipped in 9.22s pytest / 11.89s wall
- after: 78 passed, 6 skipped in 9.51s pytest / 12.25s wall

Runtime delta: +0.29s pytest, +0.36s wall.
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Harness follow-up pushed in c27c18434.

Added backend/testing/e2e/test_boundary_contract_compatibility.py to exercise the new request-boundary behavior through the real hermetic FastAPI app instead of only pure helper/unit coverage:

  • malformed POST /v1/apps multipart app_data rejects with 422 before fake GCS writes
  • malformed POST /v1/personas multipart persona_data rejects with 422 before fake GCS writes
  • touched real routes reject invalid query/date bounds without 500s (conversations, calendar/meetings, goals history, scores, daily-score, focus-sessions)
  • POST /v2/sync-local-files rejects invalid upload filename timestamps before sync job/storage side effects
  • invalid /v4/web/listen image chunks close with websocket policy violation 1008

Local timing, same worktree/venv:

Run Result Pytest runtime Wall time
Before harness add 73 passed, 6 skipped 9.22s 11.89s
After harness add 78 passed, 6 skipped 9.23s 11.69s

The wall-time delta is effectively noise/slightly faster on the final run; the added coverage does not materially increase hermetic CI runtime. I also checked the new file standalone with durations: app setup dominates (~3.2s), while the actual added route calls are small; the websocket test is ~0.6s after using the multi-channel path to avoid the existing 7s pending-conversation sweeper.

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Correction after rebasing onto the latest branch head: the harness commit is now 1c90e72e4.

Re-ran the full hermetic e2e harness after the rebase:

Run Result Pytest runtime Wall time
Before harness add 73 passed, 6 skipped 9.22s 11.89s
After harness add + rebase 78 passed, 6 skipped 9.68s 12.35s

Measured delta: +5 e2e tests, about +0.46s pytest runtime / +0.46s wall time on this machine. CI is running on the pushed SHA now.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/routers/sync.py">

<violation number="1" location="backend/routers/sync.py:673">
P1: `get_timestamp_from_path` now rejects VAD segment filenames like `{timestamp}.wav` because `parse_sync_filename_timestamp` only parses underscore-delimited names. Keep support for bare timestamp filenames here, otherwise sync jobs can fail after VAD when segment paths are sorted/parsed.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the unresolved review feedback in PR #8367.

Changed:

  • Preserved VAD segment timestamp support for bare and fractional segment filenames, including 1704067200.0.wav / 1704067200.5.wav.
  • Kept sync upload timestamp range validation centralized in parse_sync_filename_timestamp and removed the duplicate range check in retrieve_file_paths.
  • Hardened external integration validation so whitespace-only/non-string webhook_url values fail with 422 before trimming.
  • Hardened auth_steps backfill so malformed single-step values return 422 instead of crashing.
  • Raised the image chunk total cap to match larger mobile photo chunk counts and removed the unreachable cached-total index branch.
  • Extracted shared listen websocket e2e helpers to backend/testing/e2e/listen_test_helpers.py and reused them from both listen e2e files.

Verification passed:

  • uv run black --check routers/apps.py routers/sync.py utils/request_validation.py testing/e2e/listen_test_helpers.py testing/e2e/test_boundary_contract_compatibility.py testing/e2e/test_listen_stt.py tests/unit/test_request_validation_contracts.py tests/unit/test_apps_create_app_json.py
  • .venv/bin/python -m pytest tests/unit/test_request_validation_contracts.py tests/unit/test_apps_create_app_json.py -q — 42 passed
  • .venv/bin/python -m pytest testing/e2e/test_boundary_contract_compatibility.py testing/e2e/test_listen_stt.py -q — 8 passed

@Git-on-my-level
Git-on-my-level merged commit de819ed into main Jun 25, 2026
3 checks passed
@Git-on-my-level
Git-on-my-level deleted the david/zach-boundary-contracts branch June 25, 2026 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants