Skip to content

Harden AI generation endpoints to use token-derived user identity#435

Open
AJaySi wants to merge 1 commit intomainfrom
codex/refactor-user-id-handling-in-api-routes-v4hrgc
Open

Harden AI generation endpoints to use token-derived user identity#435
AJaySi wants to merge 1 commit intomainfrom
codex/refactor-user-id-handling-in-api-routes-v4hrgc

Conversation

@AJaySi
Copy link
Owner

@AJaySi AJaySi commented Mar 12, 2026

Motivation

  • Prevent clients from supplying authoritative user_id values to public AI generation and retrieval routes.
  • Ensure all user identity is derived from verified token claims (id / clerk_user_id) and surface an authentication error when missing.
  • Restrict task/strategy reads and optimizations so only the creating (authenticated) user can access them.

Description

  • Added current_user: Dict[str, Any] = Depends(get_current_user) to public AI endpoints and removed client-provided user_id parameters from routes such as generate_comprehensive_strategy, generate_strategy_component, get_strategy_generation_status, generate_comprehensive_strategy_polling, get_strategy_generation_status_by_task, and get_latest_generated_strategy.
  • Introduced _get_authenticated_user_id(current_user) helper to consistently extract the authenticated user id (string) from token claims and raise HTTPException(status_code=401) when missing.
  • Eliminated fallback/default user_id behavior (e.g. request.get("user_id", 1) and query default user IDs) and ensured downstream calls use the authenticated user_id string; removed an unused Query import.
  • Added owner authorization checks for task-status and strategy-optimization flows (returning 403 when the authenticated user does not own the requested task/strategy), and added current_user checks to optimize_existing_strategy.
  • Added focused static/source-level tests backend/security_test_ai_generation_endpoints_auth.py that verify route signatures, enforcement of token-derived user_id, task-owner authorization, and the 401 on missing authenticated id.

Testing

  • Ran pytest -q backend/security_test_ai_generation_endpoints_auth.py, which executed the new auth-hardening assertions and all tests passed (4 passed).
  • Ran python -m compileall backend/api/content_planning/api/content_strategy/endpoints/ai_generation_endpoints.py backend/security_test_ai_generation_endpoints_auth.py to validate modules compile successfully (succeeded).

Codex Task

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant