langflow icon indicating copy to clipboard operation
langflow copied to clipboard

feat: Gate custom component creation for admins only

Open vjgit96 opened this issue 6 months ago • 4 comments

Summary

Restricts custom component CODE CREATION to admin users while allowing everyone to perform field metadata refreshes. Fixes unintended side effect where normal users couldn't refresh field options.

Changes

  • Add custom_component_admin_only gate to POST /custom_component endpoint

    • Blocks non-admin creation of NEW custom component code
    • Applies when flag enabled
  • FIX: Remove gate from POST /custom_component/update endpoint

    • This endpoint handles metadata-only operations (field refresh)
    • Used when users change model providers, API keys, etc.
    • Normal users need access to refresh available options
    • Does not allow code modification

Rationale

The update endpoint is called for refreshing field metadata when dependencies change (e.g., available models when switching LLM providers). Blocking this prevented normal users from making model selections when provider configuration changed. The gate now only applies to NEW component creation, not metadata updates.

Type

Security/Bug Fix

Related

Part 3 of 4 in LE-906 P2 Fixes split Addresses: Custom component gate affecting normal field refreshes

Summary by CodeRabbit

  • New Features

    • Custom component creation can now be restricted to administrators via configuration.
    • Custom component updates now validate against known server templates.
  • Tests

    • Added tests for custom component admin-only authorization behavior.

Review Change Stack

vjgit96 avatar May 12 '26 20:05 vjgit96

[!IMPORTANT]

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 619cf5e1-6e0d-4bcb-8341-a80ead75c34c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • [ ] 🔍 Trigger review

Walkthrough

This PR adds admin-only permission gates to custom component creation and updates. The custom_component endpoint now blocks non-superusers when admin-only mode is enabled, and custom_component_update introduces hash-based validation to allow non-admins to refresh known templates while blocking truly custom code in the same mode.

Changes

Admin-only Custom Component Authorization

Layer / File(s) Summary
Custom component creation permission check
src/backend/base/langflow/api/v1/endpoints.py
The custom_component endpoint adds an early check: when custom_component_admin_only is enabled, non-superusers receive a 403 with "Custom component creation is restricted to administrators".
Custom component update with hash-based validation and test coverage
src/backend/base/langflow/api/v1/endpoints.py, src/backend/tests/unit/api/v1/test_endpoints.py
The custom_component_update endpoint computes known template hashes early, then in admin-only mode blocks non-superusers from editing truly custom code while permitting refresh of known templates by hash match. Tests verify both paths: known-template refresh succeeds with HTTP 200, and custom code submission is blocked with HTTP 403 and "restricted to administrators" detail.

🎯 2 (Simple) | ⏱️ ~12 minutes


[!IMPORTANT]

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error, 3 warnings)

Check name Status Explanation Resolution
Test Coverage For New Implementations ❌ Error The POST /custom_component (CREATE) endpoint gained an admin-only gate, but lacks test coverage. Only the POST /custom_component/update (UPDATE) endpoint tests were added. Add test_custom_component_create_admin_only_blocks_non_admin verifying non-superusers cannot create when custom_component_admin_only=True.
Test Quality And Coverage ⚠️ Warning Tests only cover the UPDATE endpoint admin gate, not the CREATE endpoint admin gate implemented at lines 1138-1141. Both have new functionality but only UPDATE is tested. Add test_custom_component_create_admin_only_blocks_non_admin to verify CREATE endpoint returns HTTP 403 when custom_component_admin_only=True and user is non-superuser.
Test File Naming And Structure ⚠️ Warning Test file follows proper naming and pytest structure, but lacks test coverage for CREATE endpoint admin-only gate, a gap identified in review comments. Add test_custom_component_create_admin_only_blocks_non_admin to verify non-admin users cannot create custom components when custom_component_admin_only=True.
Excessive Mock Usage Warning ⚠️ Warning Tests use dynamic mock objects instead of real SettingsService, which exists and is reused in other tests. Pattern matches existing anti-pattern in same test file. Refactor to call real get_settings_service() and monkeypatch only specific attributes (like test_get_config_mcp_base_url_from_settings does) instead of replacing entire mock objects.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately summarizes the primary change: adding an admin-only gate to custom component creation, which is the main objective of this PR.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Commit unit tests in branch feature/le-906-pr3-custom-component-admin

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 12 '26 20:05 coderabbitai[bot]

Codecov Report

:x: Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 57.57%. Comparing base (2e41b7f) to head (993295e). :warning: Report is 1 commits behind head on release-1.10.0.

Files with missing lines Patch % Lines
src/backend/base/langflow/api/v1/endpoints.py 87.50% 2 Missing :warning:
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##           release-1.10.0   #13098      +/-   ##
==================================================
- Coverage           57.58%   57.57%   -0.02%     
==================================================
  Files                2264     2264              
  Lines              216006   216016      +10     
  Branches            30550    30544       -6     
==================================================
- Hits               124395   124379      -16     
- Misses              90196    90221      +25     
- Partials             1415     1416       +1     
Flag Coverage Δ
backend 64.70% <87.50%> (+<0.01%) :arrow_up:
frontend 56.94% <ø> (-0.02%) :arrow_down:
lfx 52.83% <ø> (-0.01%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/backend/base/langflow/api/v1/endpoints.py 79.95% <87.50%> (+1.67%) :arrow_up:

... and 36 files with indirect coverage changes

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar May 12 '26 20:05 codecov[bot]

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 42%
42.01% (55431/131938) 69.22% (7539/10890) 40.84% (1249/3058)

Unit Test Results

Tests Skipped Failures Errors Time
4848 0 :zzz: 0 :x: 0 :fire: 12m 4s :stopwatch:

github-actions[bot] avatar May 12 '26 20:05 github-actions[bot]

✅ Test Coverage Advisor

No source changes detected without accompanying tests. Thanks for keeping coverage up! 🎉

Advisory check only — never blocks merge.

github-actions[bot] avatar May 21 '26 19:05 github-actions[bot]