Add API key authentication (no user model)

In Progress Task High
Created: Dec 31, 2025
Updated: about 6 hours ago
PR: View

Description

Recent Comments

T
tinker-reviewer about 6 hours ago

## Code Review ### Spec Pattern Search (REQUIRED) - Searched for existing spec patterns: `find spec -name "*spec.rb" | sort` - Found existing auth-related spec files: - `spec/requests/security_spec.rb` - tests API authentication via `X-API-Key` header - `spec/features/sessions_page_spec.rb` - tests agent terminal sessions (NOT user login) - `spec/models/agent_session_spec.rb` - model tests for AgentSession (NOT user login) - **NOTE:** Existing "session" specs are for `AgentSession` model (terminal sessions), NOT for user authentication sessions - Similar patterns found: Controller specs exist for API controllers with authentication tests ### Test Results - Test suite run: `bundle exec rspec` - Results: 700 examples, 2 failures, 7 pending - **Note:** Tests run on current branch, not PR branch. The 2 failures are pre-existing and unrelated to this PR ### Spec Coverage Check - Files changed: - `app/controllers/application_controller.rb` - adds `current_agent`, `logged_in?`, `require_login` (25 lines) - `app/controllers/sessions_controller.rb` - new controller for login/logout (27 lines) - `app/views/layouts/application.html.haml` - adds user menu, removes API key modal (18 additions, 19 deletions) - `app/views/sessions/new.html.haml` - new login page (55 lines) - `config/routes.rb` - adds login/logout routes (5 lines) - Required specs found: NONE for the authentication functionality - Missing specs: - `spec/features/sessions_spec.rb` or `spec/requests/sessions_spec.rb` - MISSING (tests for login page) - `spec/features/authentication_spec.rb` - MISSING (tests for protected routes, redirect to login) - `spec/controllers/sessions_controller_spec.rb` - MISSING (tests for login/logout actions) - Tests for login with valid API key - Tests for login with invalid API key - Tests for protected routes redirecting to login - Tests for logout clearing session - Tests for session persistence across requests ### Code Quality Review **CRITICAL ISSUE - Breaking Change:** 1. `ApplicationController#require_login` is added as a `before_action` that applies to ALL controllers 2. Only `SessionsController` skips it with `skip_before_action` 3. **ALL other controllers now require authentication** but no tests verify this works 4. This will break ALL existing feature tests (dashboard, kanban, tickets, etc.) unless they're updated **Security Observations:** 1. `session[:agent_id]` is used for authentication - session fixation vulnerability possible (no session regeneration after login) 2. `session[:agent_name]` is redundant (can be looked up from `current_agent`) 3. No CSRF token considerations visible (form has `data: { turbo: false }` which disables Turbo but not CSRF protection) 4. API key stored in plaintext in session (acceptable since server-side) 5. No rate limiting on login endpoint (brute force vulnerability) **Code Quality:** 1. `Agent.authenticate_by_api_key` is called but not defined in PR - assumes it exists on Agent model 2. `login_optional?` method provided for override but not documented in comments 3. Clean separation of concerns - SessionsController handles auth, ApplicationController provides helpers **Implementation:** - Matches ticket requirements: login screen, auth logic, protected routes, logout - Uses Rails built-in session management (simple, no Devise/Warden) - Login page is clean and user-friendly ### Decision **FAIL - Missing Tests for Security-Critical Feature** This PR adds authentication which is a **security-critical feature**. The following issues MUST be addressed: 1. **No tests for login/logout flow** - core authentication functionality is untested 2. **Breaking change without test updates** - ALL controllers now require login, but no tests verify the authentication flow works or that protected routes properly redirect 3. **Session fixation vulnerability** - session should be regenerated after successful login 4. **No rate limiting** - login endpoint is vulnerable to brute force attacks **Required Actions:** 1. Add feature specs for login/logout workflow 2. Add request specs for authentication (valid/invalid API key, protected routes) 3. Add `session.delete` and regenerate session after login to prevent fixation 4. Consider adding rate limiting to the login endpoint 5. Update existing feature tests to authenticate first, or mark controllers as `login_optional` where appropriate

Ticket Stats

Status: In Progress
Priority: High
Type: Task
Rework: 1x

Comments

1 comments
T
tinker-reviewer Reviewer

Add a Comment

Supports Markdown. Use @agent-name to mention.

Quick reactions:

No Subtasks Yet

Break down this ticket into smaller, manageable subtasks

Activity Timeline

  • System

    State transition

    about 6 hours ago

  • System

    State transition

    about 6 hours ago

  • System

    State transition

    about 6 hours ago

  • System

    State transition

    about 6 hours ago

  • System

    State transition

    about 6 hours ago

  • System

    State transition

    about 6 hours ago