| 1 |
import sys |
| 2 |
import unittest |
| 3 |
|
| 4 |
from webtest import TestApp |
| 5 |
|
| 6 |
from pecan import expose, make_app |
| 7 |
from pecan.secure import secure, unlocked, SecureController |
| 8 |
from pecan.tests import PecanTestCase |
| 9 |
|
| 10 |
|
| 11 |
class TestSecure(PecanTestCase): |
| 12 |
def test_simple_secure(self): |
| 13 |
authorized = False |
| 14 |
|
| 15 |
class SecretController(SecureController): |
| 16 |
@expose() |
| 17 |
def index(self): |
| 18 |
return 'Index' |
| 19 |
|
| 20 |
@expose() |
| 21 |
@unlocked |
| 22 |
def allowed(self): |
| 23 |
return 'Allowed!' |
| 24 |
|
| 25 |
@classmethod |
| 26 |
def check_permissions(cls): |
| 27 |
return authorized |
| 28 |
|
| 29 |
class RootController(object): |
| 30 |
@expose() |
| 31 |
def index(self): |
| 32 |
return 'Hello, World!' |
| 33 |
|
| 34 |
@expose() |
| 35 |
@secure(lambda: False) |
| 36 |
def locked(self): |
| 37 |
return 'No dice!' |
| 38 |
|
| 39 |
@expose() |
| 40 |
@secure(lambda: True) |
| 41 |
def unlocked(self): |
| 42 |
return 'Sure thing' |
| 43 |
|
| 44 |
secret = SecretController() |
| 45 |
|
| 46 |
app = TestApp(make_app( |
| 47 |
RootController(), |
| 48 |
debug=True, |
| 49 |
static_root='tests/static' |
| 50 |
)) |
| 51 |
response = app.get('/') |
| 52 |
assert response.status_int == 200 |
| 53 |
assert response.body == b'Hello, World!' |
| 54 |
|
| 55 |
response = app.get('/unlocked') |
| 56 |
assert response.status_int == 200 |
| 57 |
assert response.body == b'Sure thing' |
| 58 |
|
| 59 |
response = app.get('/locked', expect_errors=True) |
| 60 |
assert response.status_int == 401 |
| 61 |
|
| 62 |
response = app.get('/secret/', expect_errors=True) |
| 63 |
assert response.status_int == 401 |
| 64 |
|
| 65 |
response = app.get('/secret/allowed') |
| 66 |
assert response.status_int == 200 |
| 67 |
assert response.body == b'Allowed!' |
| 68 |
|
| 69 |
def test_unlocked_attribute(self): |
| 70 |
class AuthorizedSubController(object): |
| 71 |
@expose() |
| 72 |
def index(self): |
| 73 |
return 'Index' |
| 74 |
|
| 75 |
@expose() |
| 76 |
def allowed(self): |
| 77 |
return 'Allowed!' |
| 78 |
|
| 79 |
class SecretController(SecureController): |
| 80 |
@expose() |
| 81 |
def index(self): |
| 82 |
return 'Index' |
| 83 |
|
| 84 |
@expose() |
| 85 |
@unlocked |
| 86 |
def allowed(self): |
| 87 |
return 'Allowed!' |
| 88 |
|
| 89 |
authorized = unlocked(AuthorizedSubController()) |
| 90 |
|
| 91 |
class RootController(object): |
| 92 |
@expose() |
| 93 |
def index(self): |
| 94 |
return 'Hello, World!' |
| 95 |
|
| 96 |
@expose() |
| 97 |
@secure(lambda: False) |
| 98 |
def locked(self): |
| 99 |
return 'No dice!' |
| 100 |
|
| 101 |
@expose() |
| 102 |
@secure(lambda: True) |
| 103 |
def unlocked(self): |
| 104 |
return 'Sure thing' |
| 105 |
|
| 106 |
secret = SecretController() |
| 107 |
|
| 108 |
app = TestApp(make_app( |
| 109 |
RootController(), |
| 110 |
debug=True, |
| 111 |
static_root='tests/static' |
| 112 |
)) |
| 113 |
response = app.get('/') |
| 114 |
assert response.status_int == 200 |
| 115 |
assert response.body == b'Hello, World!' |
| 116 |
|
| 117 |
response = app.get('/unlocked') |
| 118 |
assert response.status_int == 200 |
| 119 |
assert response.body == b'Sure thing' |
| 120 |
|
| 121 |
response = app.get('/locked', expect_errors=True) |
| 122 |
assert response.status_int == 401 |
| 123 |
|
| 124 |
response = app.get('/secret/', expect_errors=True) |
| 125 |
assert response.status_int == 401 |
| 126 |
|
| 127 |
response = app.get('/secret/allowed') |
| 128 |
assert response.status_int == 200 |
| 129 |
assert response.body == b'Allowed!' |
| 130 |
|
| 131 |
response = app.get('/secret/authorized/') |
| 132 |
assert response.status_int == 200 |
| 133 |
assert response.body == b'Index' |
| 134 |
|
| 135 |
response = app.get('/secret/authorized/allowed') |
| 136 |
assert response.status_int == 200 |
| 137 |
assert response.body == b'Allowed!' |
| 138 |
|
| 139 |
def test_secure_attribute(self): |
| 140 |
authorized = False |
| 141 |
|
| 142 |
class SubController(object): |
| 143 |
@expose() |
| 144 |
def index(self): |
| 145 |
return 'Hello from sub!' |
| 146 |
|
| 147 |
class RootController(object): |
| 148 |
@expose() |
| 149 |
def index(self): |
| 150 |
return 'Hello from root!' |
| 151 |
|
| 152 |
sub = secure(SubController(), lambda: authorized) |
| 153 |
|
| 154 |
app = TestApp(make_app(RootController())) |
| 155 |
response = app.get('/') |
| 156 |
assert response.status_int == 200 |
| 157 |
assert response.body == b'Hello from root!' |
| 158 |
|
| 159 |
response = app.get('/sub/', expect_errors=True) |
| 160 |
assert response.status_int == 401 |
| 161 |
|
| 162 |
authorized = True |
| 163 |
response = app.get('/sub/') |
| 164 |
assert response.status_int == 200 |
| 165 |
assert response.body == b'Hello from sub!' |
| 166 |
|
| 167 |
def test_secured_generic_controller(self): |
| 168 |
authorized = False |
| 169 |
|
| 170 |
class RootController(object): |
| 171 |
|
| 172 |
@classmethod |
| 173 |
def check_permissions(cls): |
| 174 |
return authorized |
| 175 |
|
| 176 |
@expose(generic=True) |
| 177 |
def index(self): |
| 178 |
return 'Index' |
| 179 |
|
| 180 |
@secure('check_permissions') |
| 181 |
@index.when(method='POST') |
| 182 |
def index_post(self): |
| 183 |
return 'I should not be allowed' |
| 184 |
|
| 185 |
@secure('check_permissions') |
| 186 |
@expose(generic=True) |
| 187 |
def secret(self): |
| 188 |
return 'I should not be allowed' |
| 189 |
|
| 190 |
app = TestApp(make_app( |
| 191 |
RootController(), |
| 192 |
debug=True, |
| 193 |
static_root='tests/static' |
| 194 |
)) |
| 195 |
response = app.get('/') |
| 196 |
assert response.status_int == 200 |
| 197 |
response = app.post('/', expect_errors=True) |
| 198 |
assert response.status_int == 401 |
| 199 |
response = app.get('/secret/', expect_errors=True) |
| 200 |
assert response.status_int == 401 |
| 201 |
|
| 202 |
def test_secured_generic_controller_lambda(self): |
| 203 |
authorized = False |
| 204 |
|
| 205 |
class RootController(object): |
| 206 |
|
| 207 |
@expose(generic=True) |
| 208 |
def index(self): |
| 209 |
return 'Index' |
| 210 |
|
| 211 |
@secure(lambda: authorized) |
| 212 |
@index.when(method='POST') |
| 213 |
def index_post(self): |
| 214 |
return 'I should not be allowed' |
| 215 |
|
| 216 |
@secure(lambda: authorized) |
| 217 |
@expose(generic=True) |
| 218 |
def secret(self): |
| 219 |
return 'I should not be allowed' |
| 220 |
|
| 221 |
app = TestApp(make_app( |
| 222 |
RootController(), |
| 223 |
debug=True, |
| 224 |
static_root='tests/static' |
| 225 |
)) |
| 226 |
response = app.get('/') |
| 227 |
assert response.status_int == 200 |
| 228 |
response = app.post('/', expect_errors=True) |
| 229 |
assert response.status_int == 401 |
| 230 |
response = app.get('/secret/', expect_errors=True) |
| 231 |
assert response.status_int == 401 |
| 232 |
|
| 233 |
def test_secured_generic_controller_secure_attribute(self): |
| 234 |
authorized = False |
| 235 |
|
| 236 |
class SecureController(object): |
| 237 |
|
| 238 |
@expose(generic=True) |
| 239 |
def index(self): |
| 240 |
return 'I should not be allowed' |
| 241 |
|
| 242 |
@index.when(method='POST') |
| 243 |
def index_post(self): |
| 244 |
return 'I should not be allowed' |
| 245 |
|
| 246 |
@expose(generic=True) |
| 247 |
def secret(self): |
| 248 |
return 'I should not be allowed' |
| 249 |
|
| 250 |
class RootController(object): |
| 251 |
sub = secure(SecureController(), lambda: authorized) |
| 252 |
|
| 253 |
app = TestApp(make_app( |
| 254 |
RootController(), |
| 255 |
debug=True, |
| 256 |
static_root='tests/static' |
| 257 |
)) |
| 258 |
response = app.get('/sub/', expect_errors=True) |
| 259 |
assert response.status_int == 401 |
| 260 |
response = app.post('/sub/', expect_errors=True) |
| 261 |
assert response.status_int == 401 |
| 262 |
response = app.get('/sub/secret/', expect_errors=True) |
| 263 |
assert response.status_int == 401 |
| 264 |
|
| 265 |
def test_secured_generic_controller_secure_attribute_with_unlocked(self): |
| 266 |
|
| 267 |
class RootController(SecureController): |
| 268 |
|
| 269 |
@unlocked |
| 270 |
@expose(generic=True) |
| 271 |
def index(self): |
| 272 |
return 'Unlocked!' |
| 273 |
|
| 274 |
@unlocked |
| 275 |
@index.when(method='POST') |
| 276 |
def index_post(self): |
| 277 |
return 'Unlocked!' |
| 278 |
|
| 279 |
@expose(generic=True) |
| 280 |
def secret(self): |
| 281 |
return 'I should not be allowed' |
| 282 |
|
| 283 |
app = TestApp(make_app( |
| 284 |
RootController(), |
| 285 |
debug=True, |
| 286 |
static_root='tests/static' |
| 287 |
)) |
| 288 |
response = app.get('/') |
| 289 |
assert response.status_int == 200 |
| 290 |
response = app.post('/') |
| 291 |
assert response.status_int == 200 |
| 292 |
response = app.get('/secret/', expect_errors=True) |
| 293 |
assert response.status_int == 401 |
| 294 |
|
| 295 |
def test_state_attribute(self): |
| 296 |
from pecan.secure import Any, Protected |
| 297 |
assert repr(Any) == '<SecureState Any>' |
| 298 |
assert bool(Any) is False |
| 299 |
|
| 300 |
assert repr(Protected) == '<SecureState Protected>' |
| 301 |
assert bool(Protected) is True |
| 302 |
|
| 303 |
def test_secure_obj_only_failure(self): |
| 304 |
class Foo(object): |
| 305 |
pass |
| 306 |
|
| 307 |
try: |
| 308 |
secure(Foo()) |
| 309 |
except Exception as e: |
| 310 |
assert isinstance(e, TypeError) |
| 311 |
|
| 312 |
|
| 313 |
class TestObjectPathSecurity(PecanTestCase): |
| 314 |
|
| 315 |
def setUp(self): |
| 316 |
super(TestObjectPathSecurity, self).setUp() |
| 317 |
permissions_checked = set() |
| 318 |
|
| 319 |
class DeepSecretController(SecureController): |
| 320 |
authorized = False |
| 321 |
|
| 322 |
@expose() |
| 323 |
@unlocked |
| 324 |
def _lookup(self, someID, *remainder): |
| 325 |
if someID == 'notfound': |
| 326 |
return None |
| 327 |
return SubController(someID), remainder |
| 328 |
|
| 329 |
@expose() |
| 330 |
def index(self): |
| 331 |
return 'Deep Secret' |
| 332 |
|
| 333 |
@classmethod |
| 334 |
def check_permissions(cls): |
| 335 |
permissions_checked.add('deepsecret') |
| 336 |
return cls.authorized |
| 337 |
|
| 338 |
class SubController(object): |
| 339 |
def __init__(self, myID): |
| 340 |
self.myID = myID |
| 341 |
|
| 342 |
@expose() |
| 343 |
def index(self): |
| 344 |
return 'Index %s' % self.myID |
| 345 |
|
| 346 |
deepsecret = DeepSecretController() |
| 347 |
|
| 348 |
class SecretController(SecureController): |
| 349 |
authorized = False |
| 350 |
independent_authorization = False |
| 351 |
|
| 352 |
@expose() |
| 353 |
def _lookup(self, someID, *remainder): |
| 354 |
if someID == 'notfound': |
| 355 |
return None |
| 356 |
elif someID == 'lookup_wrapped': |
| 357 |
return self.wrapped, remainder |
| 358 |
return SubController(someID), remainder |
| 359 |
|
| 360 |
@secure('independent_check_permissions') |
| 361 |
@expose() |
| 362 |
def independent(self): |
| 363 |
return 'Independent Security' |
| 364 |
|
| 365 |
wrapped = secure( |
| 366 |
SubController('wrapped'), 'independent_check_permissions' |
| 367 |
) |
| 368 |
|
| 369 |
@classmethod |
| 370 |
def check_permissions(cls): |
| 371 |
permissions_checked.add('secretcontroller') |
| 372 |
return cls.authorized |
| 373 |
|
| 374 |
@classmethod |
| 375 |
def independent_check_permissions(cls): |
| 376 |
permissions_checked.add('independent') |
| 377 |
return cls.independent_authorization |
| 378 |
|
| 379 |
class NotSecretController(object): |
| 380 |
@expose() |
| 381 |
def _lookup(self, someID, *remainder): |
| 382 |
if someID == 'notfound': |
| 383 |
return None |
| 384 |
return SubController(someID), remainder |
| 385 |
|
| 386 |
unlocked = unlocked(SubController('unlocked')) |
| 387 |
|
| 388 |
class RootController(object): |
| 389 |
secret = SecretController() |
| 390 |
notsecret = NotSecretController() |
| 391 |
|
| 392 |
self.deepsecret_cls = DeepSecretController |
| 393 |
self.secret_cls = SecretController |
| 394 |
|
| 395 |
self.permissions_checked = permissions_checked |
| 396 |
self.app = TestApp(make_app( |
| 397 |
RootController(), |
| 398 |
debug=True, |
| 399 |
static_root='tests/static' |
| 400 |
)) |
| 401 |
|
| 402 |
def tearDown(self): |
| 403 |
self.permissions_checked.clear() |
| 404 |
self.secret_cls.authorized = False |
| 405 |
self.deepsecret_cls.authorized = False |
| 406 |
|
| 407 |
def test_sub_of_both_not_secret(self): |
| 408 |
response = self.app.get('/notsecret/hi/') |
| 409 |
assert response.status_int == 200 |
| 410 |
assert response.body == b'Index hi' |
| 411 |
|
| 412 |
def test_protected_lookup(self): |
| 413 |
response = self.app.get('/secret/hi/', expect_errors=True) |
| 414 |
assert response.status_int == 401 |
| 415 |
|
| 416 |
self.secret_cls.authorized = True |
| 417 |
response = self.app.get('/secret/hi/') |
| 418 |
assert response.status_int == 200 |
| 419 |
assert response.body == b'Index hi' |
| 420 |
assert 'secretcontroller' in self.permissions_checked |
| 421 |
|
| 422 |
def test_secured_notfound_lookup(self): |
| 423 |
response = self.app.get('/secret/notfound/', expect_errors=True) |
| 424 |
assert response.status_int == 404 |
| 425 |
|
| 426 |
def test_secret_through_lookup(self): |
| 427 |
response = self.app.get( |
| 428 |
'/notsecret/hi/deepsecret/', expect_errors=True |
| 429 |
) |
| 430 |
assert response.status_int == 401 |
| 431 |
|
| 432 |
def test_layered_protection(self): |
| 433 |
response = self.app.get('/secret/hi/deepsecret/', expect_errors=True) |
| 434 |
assert response.status_int == 401 |
| 435 |
assert 'secretcontroller' in self.permissions_checked |
| 436 |
|
| 437 |
self.secret_cls.authorized = True |
| 438 |
response = self.app.get('/secret/hi/deepsecret/', expect_errors=True) |
| 439 |
assert response.status_int == 401 |
| 440 |
assert 'secretcontroller' in self.permissions_checked |
| 441 |
assert 'deepsecret' in self.permissions_checked |
| 442 |
|
| 443 |
self.deepsecret_cls.authorized = True |
| 444 |
response = self.app.get('/secret/hi/deepsecret/') |
| 445 |
assert response.status_int == 200 |
| 446 |
assert response.body == b'Deep Secret' |
| 447 |
assert 'secretcontroller' in self.permissions_checked |
| 448 |
assert 'deepsecret' in self.permissions_checked |
| 449 |
|
| 450 |
def test_cyclical_protection(self): |
| 451 |
self.secret_cls.authorized = True |
| 452 |
self.deepsecret_cls.authorized = True |
| 453 |
response = self.app.get('/secret/1/deepsecret/2/deepsecret/') |
| 454 |
assert response.status_int == 200 |
| 455 |
assert response.body == b'Deep Secret' |
| 456 |
assert 'secretcontroller' in self.permissions_checked |
| 457 |
assert 'deepsecret' in self.permissions_checked |
| 458 |
|
| 459 |
def test_unlocked_lookup(self): |
| 460 |
response = self.app.get('/notsecret/1/deepsecret/2/') |
| 461 |
assert response.status_int == 200 |
| 462 |
assert response.body == b'Index 2' |
| 463 |
assert 'deepsecret' not in self.permissions_checked |
| 464 |
|
| 465 |
response = self.app.get( |
| 466 |
'/notsecret/1/deepsecret/notfound/', expect_errors=True |
| 467 |
) |
| 468 |
assert response.status_int == 404 |
| 469 |
assert 'deepsecret' not in self.permissions_checked |
| 470 |
|
| 471 |
def test_mixed_protection(self): |
| 472 |
self.secret_cls.authorized = True |
| 473 |
response = self.app.get( |
| 474 |
'/secret/1/deepsecret/notfound/', expect_errors=True |
| 475 |
) |
| 476 |
assert response.status_int == 404 |
| 477 |
assert 'secretcontroller' in self.permissions_checked |
| 478 |
assert 'deepsecret' not in self.permissions_checked |
| 479 |
|
| 480 |
def test_independent_check_failure(self): |
| 481 |
response = self.app.get('/secret/independent/', expect_errors=True) |
| 482 |
assert response.status_int == 401 |
| 483 |
assert len(self.permissions_checked) == 1 |
| 484 |
assert 'independent' in self.permissions_checked |
| 485 |
|
| 486 |
def test_independent_check_success(self): |
| 487 |
self.secret_cls.independent_authorization = True |
| 488 |
response = self.app.get('/secret/independent') |
| 489 |
assert response.status_int == 200 |
| 490 |
assert response.body == b'Independent Security' |
| 491 |
assert len(self.permissions_checked) == 1 |
| 492 |
assert 'independent' in self.permissions_checked |
| 493 |
|
| 494 |
def test_wrapped_attribute_failure(self): |
| 495 |
self.secret_cls.independent_authorization = False |
| 496 |
response = self.app.get('/secret/wrapped/', expect_errors=True) |
| 497 |
assert response.status_int == 401 |
| 498 |
assert len(self.permissions_checked) == 1 |
| 499 |
assert 'independent' in self.permissions_checked |
| 500 |
|
| 501 |
def test_wrapped_attribute_success(self): |
| 502 |
self.secret_cls.independent_authorization = True |
| 503 |
response = self.app.get('/secret/wrapped/') |
| 504 |
assert response.status_int == 200 |
| 505 |
assert response.body == b'Index wrapped' |
| 506 |
assert len(self.permissions_checked) == 1 |
| 507 |
assert 'independent' in self.permissions_checked |
| 508 |
|
| 509 |
def test_lookup_to_wrapped_attribute_on_self(self): |
| 510 |
self.secret_cls.authorized = True |
| 511 |
self.secret_cls.independent_authorization = True |
| 512 |
response = self.app.get('/secret/lookup_wrapped/') |
| 513 |
assert response.status_int == 200 |
| 514 |
assert response.body == b'Index wrapped' |
| 515 |
assert len(self.permissions_checked) == 2 |
| 516 |
assert 'independent' in self.permissions_checked |
| 517 |
assert 'secretcontroller' in self.permissions_checked |
| 518 |
|
| 519 |
def test_unlocked_attribute_in_insecure(self): |
| 520 |
response = self.app.get('/notsecret/unlocked/') |
| 521 |
assert response.status_int == 200 |
| 522 |
assert response.body == b'Index unlocked' |
| 523 |
|
| 524 |
|
| 525 |
class SecureControllerSharedPermissionsRegression(PecanTestCase): |
| 526 |
"""Regression tests for https://github.com/dreamhost/pecan/issues/131""" |
| 527 |
|
| 528 |
def setUp(self): |
| 529 |
super(SecureControllerSharedPermissionsRegression, self).setUp() |
| 530 |
|
| 531 |
class Parent(object): |
| 532 |
@expose() |
| 533 |
def index(self): |
| 534 |
return 'hello' |
| 535 |
|
| 536 |
class UnsecuredChild(Parent): |
| 537 |
pass |
| 538 |
|
| 539 |
class SecureChild(Parent, SecureController): |
| 540 |
@classmethod |
| 541 |
def check_permissions(cls): |
| 542 |
return False |
| 543 |
|
| 544 |
class RootController(object): |
| 545 |
|
| 546 |
secured = SecureChild() |
| 547 |
unsecured = UnsecuredChild() |
| 548 |
|
| 549 |
self.app = TestApp(make_app(RootController())) |
| 550 |
|
| 551 |
def test_inherited_security(self): |
| 552 |
assert self.app.get('/secured/', status=401).status_int == 401 |
| 553 |
assert self.app.get('/unsecured/').status_int == 200 |