Skip to content

Comments

fix(backend): security hardening for environment, cookies, oauth, and task locking#1311

Open
eren-karakus0 wants to merge 3 commits intoeigent-ai:mainfrom
eren-karakus0:fix/backend-security-hardening
Open

fix(backend): security hardening for environment, cookies, oauth, and task locking#1311
eren-karakus0 wants to merge 3 commits intoeigent-ai:mainfrom
eren-karakus0:fix/backend-security-hardening

Conversation

@eren-karakus0
Copy link
Contributor

@eren-karakus0 eren-karakus0 commented Feb 19, 2026

Related Issue

Closes #1328

Split from #1299 as requested by @bytecii.

Summary

Security hardening for the backend codebase.

Changes

  • environment.py: Restrict environment variable access with path validation (absolute paths within ~/.eigent/ allowed, path traversal blocked)
  • cookie_manager.py: Add SameSite/Secure/HttpOnly flags
  • oauth_state_manager.py: Add CSRF token validation
  • task.py: Fix race condition with proper lock/mutex handling on all task_locks accessors
  • tool_controller.py: Sanitize path parameter

Tests

  • test_environment.py - path validation: traversal, absolute within base, symlink escape
  • test_task.py - mutex tests merged in: thread safety, idempotent get_or_create, duplicate detection
  • test_cookie_manager.py - secure cookie flag validation
  • test_oauth_state_manager.py - CSRF token validation

Files (9)

  • backend/app/component/environment.py
  • backend/app/controller/tool_controller.py
  • backend/app/service/task.py
  • backend/app/utils/cookie_manager.py
  • backend/app/utils/oauth_state_manager.py
  • backend/tests/app/component/test_environment.py
  • backend/tests/app/service/test_task.py
  • backend/tests/app/utils/test_cookie_manager.py
  • backend/tests/app/utils/test_oauth_state_manager.py

… task locking

- Restrict environment variable access to an explicit allowlist
- Add SameSite/Secure/HttpOnly flags to cookie_manager
- Add CSRF token validation to oauth_state_manager
- Fix race condition in task service with proper lock/mutex handling
- Sanitize tool_controller path parameter
- Add tests for all changes
@Wendong-Fan
Copy link
Contributor

could @eren-karakus0 link the issue?

@eren-karakus0
Copy link
Contributor Author

Linked - closes #1328. Sorry for the delay!

Copy link
Collaborator

@bytecii bytecii left a comment

Choose a reason for hiding this comment

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

In general LGTM. Left some comments. Thanks!

logger.warning(
f"Security: Rejected absolute env_path. Path: {env_path}"
)
return None
Copy link
Collaborator

Choose a reason for hiding this comment

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

why we also reject the absolute path such as ~/.eigent/some-user/.env which is within the .eigent?

Copy link
Contributor Author

@eren-karakus0 eren-karakus0 Feb 22, 2026

Choose a reason for hiding this comment

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

Restored the original logic. Absolute paths within ~/.eigent/ are now accepted again. The boundary check via resolved_path.relative_to(base_resolved) still prevents escapes outside the safe directory. Updated the corresponding test as well.

return create_task_lock(id)


async def delete_task_lock(id: str):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also apply the mutex for other functions such as delete_task_lock, get_task_lock, and get_task_lock_if_exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied _task_locks_mutex to all three: get_task_lock, get_task_lock_if_exists, and delete_task_lock. For delete_task_lock, used a split-mutex approach - holds the lock for dict lookup and deletion, but releases it during await task_lock.cleanup() to avoid blocking other threads during async cleanup.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add the tests within the service/test_task.py directly for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - merged all 4 mutex tests into test_task.py using def test_xxx style with a clean_task_locks_with_mutex fixture for proper cleanup. Deleted test_task_lock_mutex.py.

- Restore original absolute path handling in environment.py (allow
  absolute paths within env_base_dir boundary)
- Add _task_locks_mutex to get_task_lock, get_task_lock_if_exists,
  and delete_task_lock for thread safety
- Merge mutex tests from test_task_lock_mutex.py into test_task.py
  using def test_xxx style
- Update test_environment.py to match restored path behavior
- Remove unused tempfile import in test_cookie_manager.py
- Fix line length formatting in task.py and test_oauth_state_manager.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Backend security: task lock race condition, predictable temp files, path traversal, OAuth state bypass

3 participants