fix(chore): centralize FastAPI Query validators to prevent drift#4540
Open
bogdanmariusc10 wants to merge 2 commits intomainfrom
Open
Conversation
Extract ~87 inline Query(pattern=...) validators across 13 files into shared Annotated[..., Query(...)] aliases in mcpgateway/common/query_params.py, sourcing regex strings from Settings where appropriate. This is a follow-up to PR #4337 (ICACF-25), which correctly added input validation at the Query layer but inlined the patterns at every call-site, creating maintenance and drift risk. Changes: - Created mcpgateway/common/query_params.py with 30+ shared query parameter aliases - Added 6 new validation patterns to mcpgateway/config.py: * validation_relationship_pattern * validation_entity_type_pattern * validation_time_range_pattern * validation_status_filter_pattern * validation_period_type_pattern * validation_aggregation_pattern - Replaced 87 inline Query(pattern=...) declarations across: * mcpgateway/admin.py * mcpgateway/main.py * mcpgateway/routers/{sso,log_search,observability,oauth_router,teams,email_auth,rbac,toolops_router,llm_admin_router,llm_config_router}.py - Updated tests/security/test_query_param_validation.py to import patterns from centralized module - Documented new validation patterns in .env.example - Fixed admin.py:17577 drift (now uses SecurityValidator.TOOL_NAME_PATTERN) - Increased SSO authorization code max_length from 512 to 4096 for Microsoft Entra ID support Benefits: - Eliminates drift risk between inline patterns and centralized validators - Reduces refactor friction (update once vs 8+ files) - Simplifies security review (15 aliases vs 87 inline regexes) - Maintains endpoint-specific OpenAPI parameter.schema.pattern metadata - Preserves per-param 422 validation responses Closes #4337 Signed-off-by: Bogdan-Marius-Catanus <[email protected]>
Open
21 tasks
Signed-off-by: Bogdan-Marius-Catanus <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Related Issue
Closes #4436
📝 Summary
This PR centralizes ~87 duplicated inline
Query(pattern=...)validators across 13 files into a shared module (mcpgateway/common/query_params.py), eliminating maintenance drift risk and simplifying security review.Problem: PR #4337 correctly added input validation at the Query layer but inlined regex patterns at every call-site. This created:
admin.py:17577already hard-coded a pattern that exists inSecurityValidator.TOOL_NAME_PATTERNSolution: Extract patterns into
Annotated[..., Query(...)]aliases backed bySettingsvalidation patterns andSecurityValidatorconstants, following existing codebase conventions.Changes:
mcpgateway/common/query_params.pywith 30+ shared query parameter aliasesmcpgateway/config.py(relationship, entity_type, time_range, status_filter, period_type, aggregation)Query(pattern=...)declarations across admin.py, main.py, and 11 routerstests/security/test_query_param_validation.pyto import from centralized moduleadmin.py:17577drift (now usesSecurityValidator.TOOL_NAME_PATTERN)max_lengthfrom 512 to 4096 for Microsoft Entra ID support (reported in Issue #[BUG][REGRESSION]: SSO callback returns HTTP 422 — authorization code max_length too short for Microsoft Entra ID (breaks SSO login) #4490 and an open PR fix: increase SSO authorization code max_length from 512 to 4096 (Entra ID regression) #4491).env.exampleBenefits:
parameter.schema.patternmetadata🏷️ Type of Change
🧪 Verification
make lintmake testmake coverageAdditional verification:
Query(pattern=...)declarations successfully replacedsearch_files)Annotatedtypes correctly defined (no defaults insideQuery())/docs)tests/security/test_query_param_validation.pycontinues at 160+ cases✅ Checklist
make black isort pre-commit)📓 Notes
Design Decisions
Why
Annotatedtypes instead of middleware validation?parameter.schema.patternmetadataWhy separate aliases for similar patterns?
team_idandgateway_idboth use^[a-zA-Z0-9_-]+$today but are semantically differentFastAPI Annotated Pattern:
FastAPI automatically extracts and applies validation rules from the
Annotatedmetadata. The= Noneis just the default when the parameter is missing.Files Changed (17 total)
mcpgateway/common/query_params.py(342 lines).env.example,.secrets.baseline,mcpgateway/config.py,mcpgateway/admin.py,mcpgateway/main.py, 11 routers, test fileSecurity Impact