-
Notifications
You must be signed in to change notification settings - Fork 8.2k
fix: enforce authentication for superuser cli command #9152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThis update introduces stricter security controls and configuration options for superuser creation via the CLI. It adds authentication requirements, the ability to disable CLI superuser creation, and improved handling of the AUTO_LOGIN setting. The changes include new documentation, settings, logic in the CLI command, utility functions, and comprehensive unit tests. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant Settings
participant DB
participant Auth
User->>CLI: Run 'langflow superuser' [with optional args]
CLI->>Settings: Check AUTO_LOGIN, ENABLE_SUPERUSER_CLI
alt ENABLE_SUPERUSER_CLI is False
CLI-->>User: Exit with error (CLI disabled)
else AUTO_LOGIN is True
CLI->>DB: Check if any superusers exist
alt No superusers
CLI->>DB: Create default superuser
CLI-->>User: Success
else
CLI-->>User: Exit with error (additional superuser not allowed)
end
else
CLI->>User: Prompt for username/password if not provided
CLI->>Auth: Validate auth_token
alt Token valid and is superuser
CLI->>DB: Create superuser with provided credentials
CLI-->>User: Success
else
CLI-->>User: Exit with error (auth failed)
end
end
Estimated code review effort3 (~45 minutes) Possibly related PRs
Suggested labels
Suggested reviewers
✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (2)
src/backend/base/langflow/__main__.py (2)
666-666: Remove unnecessary variable initialization.The variable
existing_superusersis initialized as an empty list but immediately overwritten. This initialization is unnecessary.- from langflow.services.database.models.user.crud import get_all_superusers - existing_superusers = [] + from langflow.services.database.models.user.crud import get_all_superusers
834-834: Track the TODO for non-auto-login API key creation.The TODO comment indicates planned functionality. Consider creating a GitHub issue to track this enhancement.
Would you like me to open a new issue to track implementing API key creation for non-auto-login users via CLI?
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
uv.lockis excluded by!**/*.lock
📒 Files selected for processing (6)
SECURITY.md(1 hunks)src/backend/base/langflow/__main__.py(3 hunks)src/backend/base/langflow/services/database/models/user/crud.py(1 hunks)src/backend/base/langflow/services/settings/auth.py(1 hunks)src/backend/base/langflow/services/utils.py(1 hunks)src/backend/tests/unit/test_cli.py(2 hunks)
📓 Path-based instructions (4)
{src/backend/**/*.py,tests/**/*.py,Makefile}
📄 CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
{src/backend/**/*.py,tests/**/*.py,Makefile}: Run make format_backend to format Python code before linting or committing changes
Run make lint to perform linting checks on backend Python code
Files:
src/backend/base/langflow/services/database/models/user/crud.pysrc/backend/tests/unit/test_cli.pysrc/backend/base/langflow/services/settings/auth.pysrc/backend/base/langflow/services/utils.pysrc/backend/base/langflow/__main__.py
src/backend/base/langflow/services/database/models/**/*.py
📄 CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
Place database models in src/backend/base/langflow/services/database/models/
Files:
src/backend/base/langflow/services/database/models/user/crud.py
src/backend/tests/unit/**/*.py
📄 CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
Test component integration within flows using create_flow, build_flow, and get_build_events utilities
Files:
src/backend/tests/unit/test_cli.py
src/backend/tests/**/*.py
📄 CodeRabbit Inference Engine (.cursor/rules/testing.mdc)
src/backend/tests/**/*.py: Unit tests for backend code must be located in the 'src/backend/tests/' directory, with component tests organized by component subdirectory under 'src/backend/tests/unit/components/'.
Test files should use the same filename as the component under test, with an appropriate test prefix or suffix (e.g., 'my_component.py' → 'test_my_component.py').
Use the 'client' fixture (an async httpx.AsyncClient) for API tests in backend Python tests, as defined in 'src/backend/tests/conftest.py'.
When writing component tests, inherit from the appropriate base class in 'src/backend/tests/base.py' (ComponentTestBase, ComponentTestBaseWithClient, or ComponentTestBaseWithoutClient) and provide the required fixtures: 'component_class', 'default_kwargs', and 'file_names_mapping'.
Each test in backend Python test files should have a clear docstring explaining its purpose, and complex setups or mocks should be well-commented.
Test both sync and async code paths in backend Python tests, using '@pytest.mark.asyncio' for async tests.
Mock external dependencies appropriately in backend Python tests to isolate unit tests from external services.
Test error handling and edge cases in backend Python tests, including using 'pytest.raises' and asserting error messages.
Validate input/output behavior and test component initialization and configuration in backend Python tests.
Use the 'no_blockbuster' pytest marker to skip the blockbuster plugin in tests when necessary.
Be aware of ContextVar propagation in async tests; test both direct event loop execution and 'asyncio.to_thread' scenarios to ensure proper context isolation.
Test error handling by mocking internal functions using monkeypatch in backend Python tests.
Test resource cleanup in backend Python tests by using fixtures that ensure proper initialization and cleanup of resources.
Test timeout and performance constraints in backend Python tests using 'asyncio.wait_for' and timing assertions.
Test Langflow's Messag...
Files:
src/backend/tests/unit/test_cli.py
🧠 Learnings (2)
src/backend/tests/unit/test_cli.py (12)
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-07-18T18:25:54.474Z
Learning: Applies to src/backend/tests/unit/components/**/*.py : Create comprehensive unit tests for all new components
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Mock external dependencies appropriately in backend Python tests to isolate unit tests from external services.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test component configuration updates in backend Python tests by asserting correct updates to build configuration objects.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.091Z
Learning: Applies to src/backend/tests/**/*.py : Each test in backend Python test files should have a clear docstring explaining its purpose, and complex setups or mocks should be well-commented.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Validate input/output behavior and test component initialization and configuration in backend Python tests.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test error handling by mocking internal functions using monkeypatch in backend Python tests.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test error handling and edge cases in backend Python tests, including using 'pytest.raises' and asserting error messages.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-07-18T18:25:54.474Z
Learning: Applies to src/backend/tests/unit/components/**/*.py : Use ComponentTestBaseWithClient or ComponentTestBaseWithoutClient as base classes for component unit tests
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test components that require external APIs using appropriate pytest markers (e.g., 'api_key_required', 'no_blockbuster') and environment variables for API keys.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test Langflow's REST API endpoints in backend Python tests using the async client fixture and asserting response codes and payloads.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-07-18T18:25:54.474Z
Learning: Applies to tests/**/*.py : Test authenticated API endpoints using logged_in_headers
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/unit/test_database.py : For database-related tests that may fail in batch runs but pass individually, consider running them sequentially and be aware of this behavior when writing such tests.
SECURITY.md (2)
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/docs_development.mdc:0-0
Timestamp: 2025-07-18T18:26:42.011Z
Learning: Applies to docs/docs/**/*.{md,mdx} : Use consistent terminology: always capitalize Langflow, Component, Flow, and uppercase API and JSON.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/docs_development.mdc:0-0
Timestamp: 2025-06-23T12:46:29.953Z
Learning: All terminology such as 'Langflow', 'Component', 'Flow', 'API', and 'JSON' must be capitalized or uppercased as specified in the terminology section.
🧬 Code Graph Analysis (3)
src/backend/base/langflow/services/database/models/user/crud.py (1)
src/backend/base/langflow/services/database/models/user/model.py (1)
User(25-51)
src/backend/tests/unit/test_cli.py (1)
src/backend/tests/conftest.py (1)
runner(432-434)
src/backend/base/langflow/services/utils.py (1)
src/backend/base/langflow/services/settings/manager.py (1)
SettingsService(13-49)
🧰 Additional context used
📓 Path-based instructions (4)
{src/backend/**/*.py,tests/**/*.py,Makefile}
📄 CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
{src/backend/**/*.py,tests/**/*.py,Makefile}: Run make format_backend to format Python code before linting or committing changes
Run make lint to perform linting checks on backend Python code
Files:
src/backend/base/langflow/services/database/models/user/crud.pysrc/backend/tests/unit/test_cli.pysrc/backend/base/langflow/services/settings/auth.pysrc/backend/base/langflow/services/utils.pysrc/backend/base/langflow/__main__.py
src/backend/base/langflow/services/database/models/**/*.py
📄 CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
Place database models in src/backend/base/langflow/services/database/models/
Files:
src/backend/base/langflow/services/database/models/user/crud.py
src/backend/tests/unit/**/*.py
📄 CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)
Test component integration within flows using create_flow, build_flow, and get_build_events utilities
Files:
src/backend/tests/unit/test_cli.py
src/backend/tests/**/*.py
📄 CodeRabbit Inference Engine (.cursor/rules/testing.mdc)
src/backend/tests/**/*.py: Unit tests for backend code must be located in the 'src/backend/tests/' directory, with component tests organized by component subdirectory under 'src/backend/tests/unit/components/'.
Test files should use the same filename as the component under test, with an appropriate test prefix or suffix (e.g., 'my_component.py' → 'test_my_component.py').
Use the 'client' fixture (an async httpx.AsyncClient) for API tests in backend Python tests, as defined in 'src/backend/tests/conftest.py'.
When writing component tests, inherit from the appropriate base class in 'src/backend/tests/base.py' (ComponentTestBase, ComponentTestBaseWithClient, or ComponentTestBaseWithoutClient) and provide the required fixtures: 'component_class', 'default_kwargs', and 'file_names_mapping'.
Each test in backend Python test files should have a clear docstring explaining its purpose, and complex setups or mocks should be well-commented.
Test both sync and async code paths in backend Python tests, using '@pytest.mark.asyncio' for async tests.
Mock external dependencies appropriately in backend Python tests to isolate unit tests from external services.
Test error handling and edge cases in backend Python tests, including using 'pytest.raises' and asserting error messages.
Validate input/output behavior and test component initialization and configuration in backend Python tests.
Use the 'no_blockbuster' pytest marker to skip the blockbuster plugin in tests when necessary.
Be aware of ContextVar propagation in async tests; test both direct event loop execution and 'asyncio.to_thread' scenarios to ensure proper context isolation.
Test error handling by mocking internal functions using monkeypatch in backend Python tests.
Test resource cleanup in backend Python tests by using fixtures that ensure proper initialization and cleanup of resources.
Test timeout and performance constraints in backend Python tests using 'asyncio.wait_for' and timing assertions.
Test Langflow's Messag...
Files:
src/backend/tests/unit/test_cli.py
🧠 Learnings (2)
src/backend/tests/unit/test_cli.py (12)
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-07-18T18:25:54.474Z
Learning: Applies to src/backend/tests/unit/components/**/*.py : Create comprehensive unit tests for all new components
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Mock external dependencies appropriately in backend Python tests to isolate unit tests from external services.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test component configuration updates in backend Python tests by asserting correct updates to build configuration objects.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.091Z
Learning: Applies to src/backend/tests/**/*.py : Each test in backend Python test files should have a clear docstring explaining its purpose, and complex setups or mocks should be well-commented.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Validate input/output behavior and test component initialization and configuration in backend Python tests.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test error handling by mocking internal functions using monkeypatch in backend Python tests.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test error handling and edge cases in backend Python tests, including using 'pytest.raises' and asserting error messages.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-07-18T18:25:54.474Z
Learning: Applies to src/backend/tests/unit/components/**/*.py : Use ComponentTestBaseWithClient or ComponentTestBaseWithoutClient as base classes for component unit tests
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test components that require external APIs using appropriate pytest markers (e.g., 'api_key_required', 'no_blockbuster') and environment variables for API keys.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/**/*.py : Test Langflow's REST API endpoints in backend Python tests using the async client fixture and asserting response codes and payloads.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-07-18T18:25:54.474Z
Learning: Applies to tests/**/*.py : Test authenticated API endpoints using logged_in_headers
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.092Z
Learning: Applies to src/backend/tests/unit/test_database.py : For database-related tests that may fail in batch runs but pass individually, consider running them sequentially and be aware of this behavior when writing such tests.
SECURITY.md (2)
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/docs_development.mdc:0-0
Timestamp: 2025-07-18T18:26:42.011Z
Learning: Applies to docs/docs/**/*.{md,mdx} : Use consistent terminology: always capitalize Langflow, Component, Flow, and uppercase API and JSON.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/docs_development.mdc:0-0
Timestamp: 2025-06-23T12:46:29.953Z
Learning: All terminology such as 'Langflow', 'Component', 'Flow', 'API', and 'JSON' must be capitalized or uppercased as specified in the terminology section.
🧬 Code Graph Analysis (3)
src/backend/base/langflow/services/database/models/user/crud.py (1)
src/backend/base/langflow/services/database/models/user/model.py (1)
User(25-51)
src/backend/tests/unit/test_cli.py (1)
src/backend/tests/conftest.py (1)
runner(432-434)
src/backend/base/langflow/services/utils.py (1)
src/backend/base/langflow/services/settings/manager.py (1)
SettingsService(13-49)
🔇 Additional comments (11)
src/backend/base/langflow/services/settings/auth.py (2)
30-37: Excellent security-focused field definition.The AUTO_LOGIN field now includes comprehensive security warnings and clear documentation about its development-only nature. The TODO comment for v1.6 provides a clear migration path.
43-47: Well-designed security control setting.The ENABLE_SUPERUSER_CLI setting provides granular control over CLI superuser creation with clear security guidance. The description appropriately recommends disabling in production.
src/backend/base/langflow/services/database/models/user/crud.py (1)
65-69: Clean and correct database query implementation.The function properly implements async database access with clear naming and appropriate SQLAlchemy syntax. The E712 noqa is correct for boolean comparisons in SQLAlchemy queries.
src/backend/base/langflow/services/utils.py (3)
80-80: Improved type annotation enhances code clarity.Adding the explicit
SettingsServicetype annotation improves IDE support and makes the function signature more self-documenting.
83-89: Well-structured credential handling logic.The conditional logic clearly separates AUTO_LOGIN behavior (using defaults) from production mode (using configured credentials with proper cleanup). This aligns well with the security model.
91-92: Essential validation prevents runtime errors.Adding validation to ensure both username and password are set before proceeding prevents potential issues downstream and provides clear error messaging.
SECURITY.md (1)
53-83: Comprehensive and well-structured security documentation.The new security section effectively documents the privilege escalation risks and provides clear, actionable guidance for securing the superuser CLI command. The production configuration example is particularly valuable for users.
src/backend/tests/unit/test_cli.py (1)
67-234: Excellent comprehensive test coverage for security scenarios.The new
TestSuperuserCommandclass provides thorough testing of all security-related CLI behaviors including:
- First-time superuser creation without auth requirements
- Authentication enforcement for additional superusers
- CLI command disabling functionality
- AUTO_LOGIN credential handling
- Token validation success and failure cases
The tests use appropriate mocking, clear documentation, and follow testing best practices. This coverage aligns perfectly with the security requirements documented in SECURITY.md.
src/backend/base/langflow/__main__.py (3)
741-744: Security audit logging implementation looks good.The implementation of security audit logging for both successful and failed superuser creation attempts is appropriate and follows security best practices.
648-651: SECURITY.md Documentation Verified for ENABLE_SUPERUSER_CLISECURITY.md already includes a “Disable CLI Superuser Creation” section covering
LANGFLOW_ENABLE_SUPERUSER_CLIand lists it in the recommended production settings. No further updates are needed.
633-751: Authentication Integration VerifiedAll new authentication settings and utilities are present, documented, and covered by unit tests:
- ENABLE_SUPERUSER_CLI is defined with a clear description in
src/backend/base/langflow/services/settings/auth.py
and exercised insrc/backend/tests/unit/test_cli.py.get_all_superusersis implemented in
src/backend/base/langflow/services/database/models/user/crud.py.get_current_user_by_jwtis implemented in
src/backend/base/langflow/services/auth/utils.py.check_keyis implemented in
src/backend/base/langflow/services/database/models/api_key/crud.py.- Default credentials (
langflow/langflow) are defined in
src/backend/base/langflow/services/settings/constants.pyfor AUTO_LOGIN development mode.No further changes are required.
4c29039 to
f4fba16
Compare
|
* Enforce authentication for superuser cli command * shorten security md * cleanup * use session_scope * re-add uvlock * [autofix.ci] apply automated fixes * ruff * update env example * [autofix.ci] apply automated fixes * better exception handling * [autofix.ci] apply automated fixes * update tests to not use mocks * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * Remove old test * Catch exceptions for typer * Try output instead of stdout * Use xdist to run in serial * Separate create superuse * [autofix.ci] apply automated fixes * Ruff * [autofix.ci] apply automated fixes * lint --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
* Enforce authentication for superuser cli command * shorten security md * cleanup * use session_scope * re-add uvlock * [autofix.ci] apply automated fixes * ruff * update env example * [autofix.ci] apply automated fixes * better exception handling * [autofix.ci] apply automated fixes * update tests to not use mocks * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes * Remove old test * Catch exceptions for typer * Try output instead of stdout * Use xdist to run in serial * Separate create superuse * [autofix.ci] apply automated fixes * Ruff * [autofix.ci] apply automated fixes * lint --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
--------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>



Enforce authentication for superuser cli command. Maintains existing AUTO_LOGIN behavior, but adds additional checks when creating superusers.
Summary by CodeRabbit
New Features
Documentation
Bug Fixes
Tests