File size: 9,056 Bytes
9c13f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import re
from typing import Optional, Union
import html
import logging
from pydantic import BaseModel, validator, ValidationError
from fastapi import HTTPException, status

logger = logging.getLogger(__name__)

class QueryValidation:
    """
    Input validation and sanitization for user queries
    """

    # Dangerous patterns to block
    DANGEROUS_PATTERNS = [
        r'system\(',  # System command execution
        r'exec\(',     # Code execution
        r'eval\(',     # Code evaluation
        r'import\s+\w+',  # Import statements
        r'__\w+__',   # Special methods
        r'os\.\w+',   # OS module access
        r'subprocess\.\w+',  # Subprocess access
    ]

    @staticmethod
    def sanitize_input(text: str) -> str:
        """
        Sanitize user input by escaping HTML and removing dangerous patterns
        """
        if not text:
            return text

        # HTML escape to prevent XSS
        sanitized = html.escape(text)

        # Remove dangerous patterns
        for pattern in QueryValidation.DANGEROUS_PATTERNS:
            if re.search(pattern, sanitized, re.IGNORECASE):
                logger.warning(f"Dangerous pattern detected and removed: {pattern}")
                # Remove the dangerous pattern but keep the rest of the text
                sanitized = re.sub(pattern, '', sanitized, flags=re.IGNORECASE)

        return sanitized

    @staticmethod
    def validate_query_length(text: str, max_length: int = 10000) -> str:
        """
        Validate query length
        """
        if len(text) > max_length:
            raise ValueError(f"Query exceeds maximum length of {max_length} characters")
        return text

    @staticmethod
    def validate_content_type(content_type: str) -> bool:
        """
        Validate content type against allowed values
        """
        allowed_types = {'text', 'code', 'diagram_description', 'mathematical_formula'}
        return content_type in allowed_types

    @staticmethod
    def validate_user_type(user_type: str) -> bool:
        """
        Validate user type against allowed values
        """
        allowed_types = {'student', 'educator', 'admin'}
        return user_type in allowed_types

    @staticmethod
    def validate_message_type(message_type: str) -> bool:
        """
        Validate message type against allowed values
        """
        allowed_types = {'query', 'response', 'system_message', 'follow-up', 'text-selection'}
        return message_type in allowed_types

    @staticmethod
    def validate_record_type(record_type: str) -> bool:
        """
        Validate analytics record type against allowed values
        """
        allowed_types = {'user_interaction', 'common_question', 'system_usage', 'performance_metric'}
        return record_type in allowed_types

    @staticmethod
    def validate_aggregation_period(period: str) -> bool:
        """
        Validate aggregation period against allowed values
        """
        allowed_periods = {'hourly', 'daily', 'weekly', 'monthly'}
        return period in allowed_periods

    @staticmethod
    def validate_confidence_score(score: float) -> bool:
        """
        Validate confidence score is between 0 and 1
        """
        return 0 <= score <= 1

    @staticmethod
    def validate_url_path(path: str) -> bool:
        """
        Validate URL path is safe (no directory traversal)
        """
        if '../' in path or '..\\' in path:
            return False
        return True

    @staticmethod
    def is_safe_content(text: str) -> tuple[bool, Optional[str]]:
        """
        Check if content is safe for processing
        Returns (is_safe, reason_if_unsafe)
        """
        # Check for dangerous patterns
        for pattern in QueryValidation.DANGEROUS_PATTERNS:
            if re.search(pattern, text, re.IGNORECASE):
                return False, f"Dangerous pattern detected: {pattern}"

        # Check for excessive length
        if len(text) > 10000:  # 10k characters max
            return False, "Content exceeds maximum length"

        # Check for potential prompt injection attempts
        injection_patterns = [
            r'ignore\s+previous',
            r'forget\s+previous',
            r'system\s+prompt',
            r'you\s+are\s+now',
            r'disregard\s+instructions'
        ]

        for pattern in injection_patterns:
            if re.search(pattern, text, re.IGNORECASE):
                return False, f"Potential prompt injection detected: {pattern}"

        return True, None


def validate_and_sanitize_query(query: str) -> str:
    """
    Main function to validate and sanitize a user query
    """
    if not query or not isinstance(query, str):
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail="Query must be a non-empty string"
        )

    # Validate length first
    try:
        query = QueryValidation.validate_query_length(query)
    except ValueError as e:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail=str(e)
        )

    # Check if content is safe
    is_safe, reason = QueryValidation.is_safe_content(query)
    if not is_safe:
        logger.warning(f"Unsafe content detected: {reason}")
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail="Content validation failed"
        )

    # Sanitize the input
    sanitized_query = QueryValidation.sanitize_input(query)

    return sanitized_query


class ConversationCreateRequest(BaseModel):
    """
    Pydantic model for conversation creation request validation
    """
    user_id: Optional[str] = None
    session_id: Optional[str] = None
    initial_query: Optional[str] = None

    @validator('user_id')
    def validate_user_id(cls, v):
        if v is not None and len(v) > 100:
            raise ValueError('user_id must be 100 characters or less')
        return v

    @validator('session_id')
    def validate_session_id(cls, v):
        if v is not None and len(v) > 100:
            raise ValueError('session_id must be 100 characters or less')
        return v

    @validator('initial_query')
    def validate_initial_query(cls, v):
        if v is not None:
            if len(v) > 10000:
                raise ValueError('initial_query exceeds maximum length of 10000 characters')
            # Additional validation for safety
            is_safe, reason = QueryValidation.is_safe_content(v)
            if not is_safe:
                raise ValueError(f'initial_query contains unsafe content: {reason}')
        return v


class MessageCreateRequest(BaseModel):
    """
    Pydantic model for message creation request validation
    """
    content: str
    message_type: str = 'query'

    @validator('content')
    def validate_content(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('content cannot be empty')
        if len(v) > 10000:
            raise ValueError('content exceeds maximum length of 10000 characters')
        # Additional validation for safety
        is_safe, reason = QueryValidation.is_safe_content(v)
        if not is_safe:
            raise ValueError(f'content contains unsafe content: {reason}')
        return v

    @validator('message_type')
    def validate_message_type(cls, v):
        if not QueryValidation.validate_message_type(v):
            raise ValueError(f'invalid message_type: {v}')
        return v


class SearchRequest(BaseModel):
    """
    Pydantic model for search request validation
    """
    query: str
    max_results: int = 5
    include_citations: bool = True

    @validator('query')
    def validate_search_query(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('query cannot be empty')
        if len(v) > 1000:
            raise ValueError('query exceeds maximum length of 1000 characters')
        # Additional validation for safety
        is_safe, reason = QueryValidation.is_safe_content(v)
        if not is_safe:
            raise ValueError(f'query contains unsafe content: {reason}')
        return v

    @validator('max_results')
    def validate_max_results(cls, v):
        if v < 1 or v > 20:
            raise ValueError('max_results must be between 1 and 20')
        return v


class FeedbackRequest(BaseModel):
    """
    Pydantic model for feedback request validation
    """
    message_id: str
    conversation_id: str
    feedback_type: str
    comment: Optional[str] = None

    @validator('feedback_type')
    def validate_feedback_type(cls, v):
        allowed_types = {'positive', 'negative', 'report_inaccurate', 'report_inappropriate'}
        if v not in allowed_types:
            raise ValueError(f'feedback_type must be one of {allowed_types}')
        return v

    @validator('comment')
    def validate_comment(cls, v):
        if v and len(v) > 1000:
            raise ValueError('comment exceeds maximum length of 1000 characters')
        return v