|
13 | 13 | # limitations under the License. |
14 | 14 | """Data models for Keyspaces MCP Server.""" |
15 | 15 |
|
| 16 | +import re |
16 | 17 | from dataclasses import dataclass, field |
17 | 18 | from typing import Any, Dict, List |
18 | 19 |
|
| 20 | +from pydantic import BaseModel, Field, field_validator |
| 21 | + |
19 | 22 |
|
20 | 23 | @dataclass |
21 | 24 | class KeyspaceInfo: |
@@ -126,3 +129,50 @@ class QueryAnalysisResult: |
126 | 129 | is_full_table_scan: bool = False |
127 | 130 | recommendations: List[str] = field(default_factory=list) |
128 | 131 | performance_assessment: str = '' |
| 132 | + |
| 133 | + |
| 134 | +# Pydantic models for input validation |
| 135 | +class KeyspaceInput(BaseModel): |
| 136 | + """Validated keyspace input.""" |
| 137 | + keyspace: str = Field( |
| 138 | + ..., |
| 139 | + min_length=1, |
| 140 | + max_length=48, |
| 141 | + pattern=r'^[a-zA-Z][a-zA-Z0-9_]*$', |
| 142 | + description='Keyspace name (alphanumeric and underscore only)' |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +class TableInput(BaseModel): |
| 147 | + """Validated table input.""" |
| 148 | + keyspace: str = Field( |
| 149 | + ..., |
| 150 | + min_length=1, |
| 151 | + max_length=48, |
| 152 | + pattern=r'^[a-zA-Z][a-zA-Z0-9_]*$' |
| 153 | + ) |
| 154 | + table: str = Field( |
| 155 | + ..., |
| 156 | + min_length=1, |
| 157 | + max_length=48, |
| 158 | + pattern=r'^[a-zA-Z][a-zA-Z0-9_]*$' |
| 159 | + ) |
| 160 | + |
| 161 | + |
| 162 | +class QueryInput(BaseModel): |
| 163 | + """Validated query input.""" |
| 164 | + keyspace: str = Field( |
| 165 | + ..., |
| 166 | + min_length=1, |
| 167 | + max_length=48, |
| 168 | + pattern=r'^[a-zA-Z][a-zA-Z0-9_]*$' |
| 169 | + ) |
| 170 | + query: str = Field(..., min_length=1, max_length=10000) |
| 171 | + |
| 172 | + @field_validator('query') |
| 173 | + @classmethod |
| 174 | + def sanitize_query(cls, v: str) -> str: |
| 175 | + """Strip hidden unicode and control characters.""" |
| 176 | + v = re.sub(r'[\u200B-\u200D\uFEFF\u0000-\u001F\u007F-\u009F]', '', v) |
| 177 | + return v.strip() |
| 178 | + |
0 commit comments