API Reference
sanitongo
Sanitongo - Modern MongoDB Query Sanitizer with Layered Security Protection.
A comprehensive security library for sanitizing MongoDB queries with multiple layers of protection against NoSQL injection attacks and malicious queries.
ComplexityError
Bases: SanitizerError
Raised when query exceeds complexity limits.
Source code in src/sanitongo/exceptions.py
__init__(message, limit_type, current_value, max_allowed, context=None)
Initialize complexity error with limit information.
Source code in src/sanitongo/exceptions.py
PatternError
Bases: SecurityError
Raised when dangerous patterns are detected in query values.
Source code in src/sanitongo/exceptions.py
__init__(message, pattern_type, field_path=None, pattern_value=None, context=None)
Initialize pattern error.
Source code in src/sanitongo/exceptions.py
SanitizerError
Bases: Exception
Base exception for all sanitizer-related errors.
Source code in src/sanitongo/exceptions.py
__init__(message, context=None)
Initialize the exception with message and optional context.
SchemaViolationError
Bases: SanitizerError
Raised when query violates the defined schema.
Source code in src/sanitongo/exceptions.py
__init__(message, field_path, schema_rule=None, context=None)
Initialize schema violation error.
Source code in src/sanitongo/exceptions.py
SecurityError
Bases: SanitizerError
Raised when potentially malicious content is detected.
Source code in src/sanitongo/exceptions.py
__init__(message, threat_type, detected_patterns=None, context=None)
Initialize security error with threat information.
Source code in src/sanitongo/exceptions.py
ValidationError
Bases: SanitizerError
Raised when input validation fails.
Source code in src/sanitongo/exceptions.py
__init__(message, field_path=None, invalid_value=None, context=None)
Initialize validation error with field information.
Source code in src/sanitongo/exceptions.py
ComplexityLimiter
Layer 5: Limits query complexity to prevent DoS attacks.
Source code in src/sanitongo/layers.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
__init__(max_depth=10, max_keys=100, max_array_length=1000, max_string_length=10000)
Initialize complexity limiter.
Source code in src/sanitongo/layers.py
validate(query)
Check query complexity limits.
Source code in src/sanitongo/layers.py
OperatorFilter
Layer 3: Filters and validates MongoDB operators.
Source code in src/sanitongo/layers.py
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 | |
__init__(allowed_operators=None, dangerous_operators=None, strict_mode=True)
Initialize operator filter.
Source code in src/sanitongo/layers.py
validate(query)
Filter MongoDB operators from the query.
Source code in src/sanitongo/layers.py
PatternValidator
Layer 4: Validates patterns in string values.
Source code in src/sanitongo/layers.py
__init__(custom_patterns=None, fail_on_dangerous_patterns=True)
Initialize pattern validator.
Source code in src/sanitongo/layers.py
validate(query)
Validate string patterns in the query.
SchemaEnforcer
Layer 2: Enforces field schema and validation rules.
Source code in src/sanitongo/layers.py
__init__(schema_validator=None, fail_on_violation=True)
Initialize schema enforcer.
validate(query)
Enforce schema rules on the query.
Source code in src/sanitongo/layers.py
TypeValidator
Layer 1: Validates input types and basic structure.
Source code in src/sanitongo/layers.py
__init__(strict_mode=True)
validate(query)
Validate query types and structure.
Source code in src/sanitongo/layers.py
MongoSanitizer
Main MongoDB query sanitizer with layered protection.
Implements a five-layer protection system: 1. Type Validation 2. Schema Enforcement 3. Operator Filtering 4. Pattern Validation 5. Complexity Limiting
Source code in src/sanitongo/sanitizer.py
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
__init__(config=None)
Initialize the sanitizer with configuration.
Source code in src/sanitongo/sanitizer.py
sanitize(query)
Sanitize a MongoDB query through all protection layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Any
|
The MongoDB query to sanitize |
required |
Returns:
| Type | Description |
|---|---|
SanitizationReport
|
SanitizationReport with detailed results |
Source code in src/sanitongo/sanitizer.py
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 | |
sanitize_query(query)
Sanitize a query and return only the cleaned query.
This is a convenience method that returns just the sanitized query without the full report.
Source code in src/sanitongo/sanitizer.py
is_query_safe(query)
Check if a query is safe without modifying it.
Returns True if the query passes all validation layers.
Source code in src/sanitongo/sanitizer.py
get_config()
update_config(**kwargs)
Update sanitizer configuration.
Source code in src/sanitongo/sanitizer.py
SanitizationReport
dataclass
Detailed report of the sanitization process.
Source code in src/sanitongo/sanitizer.py
has_warnings()
has_security_issues()
has_modifications()
get_summary()
Get a summary of the sanitization results.
Source code in src/sanitongo/sanitizer.py
SanitizerConfig
dataclass
Configuration for the MongoDB sanitizer.
Source code in src/sanitongo/sanitizer.py
FieldType
Bases: Enum
Supported field types for schema validation.
Source code in src/sanitongo/schema.py
SchemaValidator
Validates MongoDB queries against a predefined schema.
Source code in src/sanitongo/schema.py
__init__(schema)
validate_query(query, path_prefix='')
Validate a query dictionary against the schema.
Source code in src/sanitongo/schema.py
get_allowed_fields()
is_field_allowed(field_name)
create_sanitizer(schema=None, strict_mode=True, **config_kwargs)
Create a MongoDB sanitizer with common configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any] | None
|
Optional schema definition for field validation |
None
|
strict_mode
|
bool
|
Whether to use strict validation mode |
True
|
**config_kwargs
|
Any
|
Additional configuration options |
{}
|
Returns:
| Type | Description |
|---|---|
MongoSanitizer
|
Configured MongoSanitizer instance |