trestle.core.remote.security
trestle.core.remote.security ¤
Security validation utilities for remote fetching operations.
This module provides security controls to prevent SSRF, path traversal, and arbitrary file access vulnerabilities.
Attributes¤
ALWAYS_BLOCKED_NETWORKS = [ipaddress.ip_network('127.0.0.0/8'), ipaddress.ip_network('::1/128'), ipaddress.ip_network('169.254.0.0/16'), ipaddress.ip_network('fe80::/10'), ipaddress.ip_network('0.0.0.0/8'), ipaddress.ip_network('::/128')] module-attribute ¤
METADATA_HOSTNAMES = {'169.254.169.254', 'metadata.google.internal', 'metadata.azure.com', '100.100.100.200'} module-attribute ¤
PRIVATE_IP_NETWORKS = [ipaddress.ip_network('10.0.0.0/8'), ipaddress.ip_network('172.16.0.0/12'), ipaddress.ip_network('192.168.0.0/16'), ipaddress.ip_network('fc00::/7')] module-attribute ¤
logger = logging.getLogger(__name__) module-attribute ¤
Classes¤
PathSecurityValidator ¤
Validator for ensuring file paths remain within allowed boundaries.
Source code in trestle/core/remote/security.py
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 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
Methods:¤
validate_cache_path(cache_path, cache_root) staticmethod ¤
Validate that a cache file path stays within the cache directory.
Uses path resolution and relative_to() to ensure the resolved cache path is actually within the cache root directory, preventing path traversal attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path | Path | The proposed cache file path to validate | required |
cache_root | Path | The root cache directory that must contain the cache_path | required |
Raises:
| Type | Description |
|---|---|
TrestleError | If cache_path resolves outside cache_root |
Example
cache_root = pathlib.Path('/home/user/.trestle/cache') cache_path = cache_root / 'evil.com' / '..' / '..' / 'etc' / 'passwd' validate_cache_path(cache_path, cache_root) # Raises TrestleError
Source code in trestle/core/remote/security.py
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 | |
validate_local_file_path(workspace_root, file_path, allow_outside_workspace=False) staticmethod ¤
Validate that a local file path is safe to access.
This method provides defense-in-depth protection against arbitrary file access by validating both workspace boundaries and blocking known sensitive system files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_root | Path | The trestle workspace root directory | required |
file_path | Path | The file path to validate | required |
allow_outside_workspace | bool | If True, allow access to files outside workspace (default: False) | False |
Raises:
| Type | Description |
|---|---|
TrestleError | If the path is deemed unsafe |
Example
workspace = pathlib.Path('/home/user/trestle-workspace') file_path = pathlib.Path('/etc/passwd') validate_local_file_path(workspace, file_path, allow_outside_workspace=False)
Raises TrestleError: Access to files outside workspace not allowed¤
validate_local_file_path(workspace, file_path, allow_outside_workspace=True)
Raises TrestleError: Attempt to access sensitive system file¤
Source code in trestle/core/remote/security.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
validate_local_path(local_path, trestle_root) staticmethod ¤
Validate that a local file path stays within the trestle workspace.
Uses path resolution and is_relative_to() to ensure the resolved local path is actually within the trestle root directory, preventing path traversal attacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path | Path | The proposed local file path to validate | required |
trestle_root | Path | The trestle root directory that must contain the local_path | required |
Raises:
| Type | Description |
|---|---|
TrestleError | If local_path resolves outside trestle_root |
Example
trestle_root = pathlib.Path('/home/user/trestle-workspace') local_path = trestle_root / 'catalogs' / '..' / '..' / 'etc' / 'passwd' validate_local_path(local_path, trestle_root) # Raises TrestleError
Source code in trestle/core/remote/security.py
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 434 435 436 437 438 439 440 441 442 443 | |
validate_trestle_uri_path(uri_path) staticmethod ¤
Validate a trestle:// URI path component to prevent path traversal attacks.
Detects path traversal attempts (..) in trestle:// URIs and raises an exception. This prevents directory traversal when resolving trestle:// references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri_path | str | The path component after 'trestle://' prefix | required |
Raises:
| Type | Description |
|---|---|
TrestleError | If path contains traversal sequences (..) |
Example
PathSecurityValidator.validate_trestle_uri_path( ... 'catalogs/nist/catalog.json' ... ) # No exception PathSecurityValidator.validate_trestle_uri_path( ... '../../etc/passwd' ... ) # Raises TrestleError
Source code in trestle/core/remote/security.py
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 | |
validate_url_path_for_cache(url_path) staticmethod ¤
Validate a URL path component to prevent path traversal attacks.
Detects path traversal attempts (..) and raises an exception to block the attack. This prevents directory traversal attacks when constructing cache file paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_path | str | The path component from a URL (e.g., from urlparse().path) | required |
Raises:
| Type | Description |
|---|---|
TrestleError | If path contains traversal sequences (..) |
Example
PathSecurityValidator.validate_url_path_for_cache( ... '/normal/path.json' ... ) # No exception PathSecurityValidator.validate_url_path_for_cache( ... '/../../../etc/passwd' ... ) # Raises TrestleError
Source code in trestle/core/remote/security.py
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 | |
URLSecurityValidator ¤
Validates URLs to prevent SSRF attacks.
Implements two-tiered SSRF protection: 1. Always blocked: loopback, link-local, and cloud metadata endpoints 2. Optionally blocked: RFC 1918 private ranges (configurable via block_private_ips)
Source code in trestle/core/remote/security.py
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
Attributes¤
allowed_domains = allowed_domains instance-attribute ¤
block_private_ips = block_private_ips instance-attribute ¤
Methods:¤
__init__(block_private_ips=False, allowed_domains=None) ¤
Initialize URL security validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_private_ips | bool | If True, block RFC 1918 private IP ranges (default: False). Always-blocked ranges (loopback, link-local, metadata) are blocked regardless. | False |
allowed_domains | Optional[Set[str]] | Optional set of allowed domain names. If provided, only these domains are allowed. | None |
Source code in trestle/core/remote/security.py
88 89 90 91 92 93 94 95 96 97 | |
validate_url(url) ¤
Validate a URL for security issues.
This method resolves the hostname and validates all resolved IPs to prevent SSRF attacks.
To mitigate DNS rebinding attacks, this validation is called both at initialization and immediately before each fetch operation, minimizing the TOCTOU window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url | str | The URL to validate | required |
Raises:
| Type | Description |
|---|---|
TrestleError | If the URL is deemed unsafe |
Source code in trestle/core/remote/security.py
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 | |
Functions:¤
get_block_private_ips_config() ¤
Get the TRESTLE_BLOCK_PRIVATE_IPS configuration from environment.
Returns:
| Type | Description |
|---|---|
bool | True if private IPs should be blocked, False otherwise (default). |
The environment variable can be set to: - 'true', '1', 'yes', 'on' (case-insensitive) to enable blocking - Any other value or unset to disable blocking (allow private IPs)
Source code in trestle/core/remote/security.py
34 35 36 37 38 39 40 41 42 43 44 45 | |
handler: python