Validate inputs to admin and main APIs#347
Conversation
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
|
Tested with curl --request POST \
--url http://localhost:4444/gateways \
--header 'Authorization: Bearer $MCPGATEWAY_BEARER_TOKEN
--header 'content-type: application/json' \
--data '{
"name": "g7@LaPX#8qcz2MUEYwK(0R^4tnJZidBb+ol5DFVeN[Wpm93A1hI{O*}xG6vCTHsSQkfj!ry]-u=|>L8Z`$aXz(mY+B#R5c92nPUVW%Jd0MEhxA>oGwfqNevKg3s^F[Ht@L1bC)!=j4}TDQpIMlSZuNKOGm7~yRxJ9Bv+W>XizCkf(nlY&82#oqr5PA$JU}a*M-Z=@wEgphdL3VKI]CtNYX^69bmfT{+es0!u7~FWrOHv1LRydGC2qx]jz#n<BkDMU@8V(PZ&%)aA$T5hXowmiEgYl!J^bfM=NQcu7StdKCrx{4I}-vO3p9Bn+LzWYkjPQe@Hm%NXI!ow2^vTuCc5z#RYg9Bh(03LdaP=F&bZUJ-E+n4x$NKrsK{1t)V8MyidGLqj7AQhCmWR^pOs6ewXF2nlUYz!#@g0}93b&dT5K+%vH=[INBOMZra)*yLqxEJpCWfUhoR7twYnm+VX*ikgtUZP@#LAd1&cw29H^qOjbs5eyR~KlFC63MVnXpG%uWTdN(B!m+=rzJY4{aoE}-x7I9lf^UQKT5Xyw3C$gBAOHpN0RJqEk7dPGn4vztMbXCl%V!&L[uWYrosTI@9j=1ZKh3fxca}+-5NOM^PYUgFEG!bpLXqHd7Am]T#o*KWn{u0CJrszv2yt&934VXUIljMC",
"url": "http://localhost:8000/sse",
"transport_type": "SSE"
}'curl --request POST \
--url http://localhost:4444/gateways \
--header 'Authorization: Bearer $MCPGATEWAY_BEARER_TOKEN' \
--header 'content-type: application/json' \
--data '{
"name": "test",
"url": "hello_world",
"transport_type": "SSE"
}'curl --request POST \
--url http://localhost:4444/gateways \
--header 'Authorization: Bearer $MCPGATEWAY_BEARER_TOKEN' \
--header 'content-type: application/json' \
--data '{
"name": "<script>",
"url": "hello_world",
"transport_type": "SSE"
}'curl --request POST \
--url http://localhost:4444/tools \
--header 'Authorization: Bearer $MCPGATEWAY_BEARER_TOKEN' \
--header 'content-type: application/json' \
--data '{
"name": "<script>",
"url": "hello_world",
"integration_type": "REST",
"request_type": "GET"
}' |
Testing Checklist for Input Validation PR🔧 Setup & Basic Functionality
Issue with "Test Server Connectivity" UI: ❌ Error: Invalid URL: URL is required 🧪 Test Valid Inputs (Happy Path) ❌Tools: Create a new tool from a REST APICreate MCP Tool
❌ nothing happened. Though we should remove adding SSE tools as this is not supported. Create REST API Tool
❌ 1 validation error for ToolCreate request_type Value error, Request type 'POST' not allowed for MCP integration [type=value_error, input_value='POST', input_type=str] For further information visit https://errors.pydantic.dev/2.11/v/value_error Update Tool
Resources
❌ UI returns: Connection failed!
❌ Prompts
Gateways (MCP Servers)
Gateways (MCP Servers) with auth
Servers
🚫 Test Invalid Inputs (Security Validation)XSS/HTML Injection (test via Ui and API)
SQL Injection Patterns
Length Limits
Invalid Formats
📊 API Testing with curl/httpieTest Admin Endpoints# Should fail - XSS attempt
curl -X POST http://localhost:8000/admin/tools \
-H "Content-Type: application/json" \
-d '{"name": "<script>alert(1)</script>", "url": "https://example.com"}'
# Should succeed - valid input
curl -X POST http://localhost:8000/admin/tools \
-H "Content-Type: application/json" \
-d '{"name": "valid_tool", "url": "https://example.com", "description": "A safe tool"}'Test Main API Endpoints# Test resource creation with invalid URI
curl -X POST http://localhost:8000/resources \
-H "Content-Type: application/json" \
-d '{"uri": "bad<uri>", "name": "test", "content": "data"}'
# Test prompt with dangerous template
curl -X POST http://localhost:8000/prompts \
-H "Content-Type: application/json" \
-d '{"name": "test", "template": "<script>evil()</script>"}'🎯 Edge Cases
🔍 Regression Testing
📝 Documentation & Logs
🚀 Final Verification
🎭 Browser Testing (if admin UI enabled)
✅ Smoke Test Complete Workflow
Kubernetes / Minikube Deployment / ArgoCD
|
📋 Post-Deployment Testing Checklist🏥 Initial Health Checks
🔑 Authentication Setup
📊 API Verification - List Endpoints
📚 OpenAPI & Documentation
🧪 Create Test Data via UI/APITools - MCP Tool
Tools - REST API Tool
Resources
Prompts
Gateways
Servers
❌ Error Handling Verification
🗄️ Database & Migrations
📝 Log Verification
🔍 Process & Port Checks
🐳 Container Health (if using Docker)
✅ Final Integration Test
🧹 Cleanup (Optional)
|
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
|
New update passes test / smoketest. Will merge this PR. Then we'll work on remaining issues in a separate defect. |
* Use safe create classes and update test cases Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Linting fixes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Minor linting fix Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Add secure update classes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Remove Secure from class names Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Update uv.lock Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Add tests for validator Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Linting fixes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Fix REST tool addition Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
* Use safe create classes and update test cases Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Linting fixes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Minor linting fix Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Add secure update classes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Remove Secure from class names Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Update uv.lock Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Add tests for validator Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Linting fixes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Fix REST tool addition Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
* Use safe create classes and update test cases Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Linting fixes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Minor linting fix Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Add secure update classes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Remove Secure from class names Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Update uv.lock Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Add tests for validator Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Linting fixes Signed-off-by: Madhav Kandukuri <madhav165@gmail.com> * Fix REST tool addition Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
🐛 Bug-fix PR
Closes #339 (/admin endpoints), Closes #340 (all other endpoints)
Related defects on JavaScript /UI code are closed by separate PRs: #337 (Add proper HTML escaping for admin UI) and #338 (resolve all lint issues in web stack)
📌 Summary
What problem does this PR fix and why?
This PR implements comprehensive input validation for all API endpoints to prevent XSS, injection attacks, and data integrity issues. User-controlled data that gets displayed in the UI can cause layout problems, security vulnerabilities, and unexpected behavior without proper validation and escaping. This fix ensures all user input is validated against strict patterns and sanitized before storage and display.
🔁 Reproduction Steps
Link the issue and minimal steps to reproduce the bug.
Issues: #339, #340
POST /admin/toolswith name:<script>alert(1)</script>)🐞 Root Cause
What was wrong and where?
API endpoints were accepting and storing user input without validation, allowing:
💡 Fix Description
How did you solve it? Key design points.
Created centralized
SecurityValidatorclass (mcpgateway/validators.py):Applied validators to all Pydantic schemas:
ToolCreate,ResourceCreate,PromptCreate,GatewayCreate,ServerCreate@field_validatordecoratorsConfiguration-driven approach:
🧪 Verification
make testpytest tests/unit/mcpgateway/validation/test_validators.py📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)