|
1 | 1 | import json |
| 2 | +import logging |
2 | 3 | import os.path |
3 | 4 |
|
| 5 | +import mock |
4 | 6 | import pytest |
5 | 7 | from six import ensure_binary |
6 | 8 |
|
|
20 | 22 | from tests.utils import snapshot |
21 | 23 |
|
22 | 24 |
|
| 25 | +try: |
| 26 | + from json.decoder import JSONDecodeError |
| 27 | +except ImportError: |
| 28 | + # handling python 2.X import error |
| 29 | + JSONDecodeError = ValueError # type: ignore |
| 30 | + |
23 | 31 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
24 | 32 | RULES_GOOD_PATH = os.path.join(ROOT_DIR, "rules-good.json") |
25 | 33 | RULES_BAD_PATH = os.path.join(ROOT_DIR, "rules-bad.json") |
@@ -423,3 +431,115 @@ def test_ddwaf_info_with_3_errors(): |
423 | 431 | assert info.loaded == 1 |
424 | 432 | assert info.failed == 2 |
425 | 433 | assert info.errors == {"missing key 'name'": ["crs-942-100", "crs-913-120"]} |
| 434 | + |
| 435 | + |
| 436 | +def test_ddwaf_info_with_json_decode_errors(tracer_appsec, caplog): |
| 437 | + tracer = tracer_appsec |
| 438 | + config = Config() |
| 439 | + config.http_tag_query_string = True |
| 440 | + |
| 441 | + with caplog.at_level(logging.WARNING), override_env(dict(DD_TRACE_CLIENT_IP_HEADER_DISABLED="False")), mock.patch( |
| 442 | + "ddtrace.appsec.processor.json.dumps", side_effect=JSONDecodeError("error", "error", 0) |
| 443 | + ), mock.patch.object(DDWaf, "info"): |
| 444 | + with tracer.trace("test", span_type=SpanTypes.WEB) as span: |
| 445 | + set_http_meta( |
| 446 | + span, |
| 447 | + config, |
| 448 | + method="PATCH", |
| 449 | + url=u"http://localhost/api/unstable/role_requests/dab1e9ae-9d99-11ed-bfdf-da7ad0900000?_authentication_token=2b0297348221f294de3a047e2ecf1235abb866b6", # noqa: E501 |
| 450 | + status_code="200", |
| 451 | + raw_uri=u"http://localhost/api/unstable/role_requests/dab1e9ae-9d99-11ed-bfdf-da7ad0900000?_authentication_token=2b0297348221f294de3a047e2ecf1235abb866b6", # noqa: E501 |
| 452 | + request_headers={ |
| 453 | + "host": u"localhost", |
| 454 | + "user-agent": "aa", |
| 455 | + "content-length": u"73", |
| 456 | + }, |
| 457 | + response_headers={ |
| 458 | + "content-length": "501", |
| 459 | + "x-ratelimit-remaining": "363", |
| 460 | + "x-ratelimit-name": "role_api", |
| 461 | + "x-ratelimit-limit": "500", |
| 462 | + "x-ratelimit-period": "60", |
| 463 | + "content-type": "application/json", |
| 464 | + "x-ratelimit-reset": "16", |
| 465 | + }, |
| 466 | + request_body={"_authentication_token": u"2b0297348221f294de3a047e2ecf1235abb866b6"}, |
| 467 | + ) |
| 468 | + |
| 469 | + assert "Error parsing data AppSec In-App WAF metrics report" in caplog.text |
| 470 | + |
| 471 | + |
| 472 | +def test_ddwaf_run_contained_typeerror(tracer_appsec, caplog): |
| 473 | + tracer = tracer_appsec |
| 474 | + |
| 475 | + config = Config() |
| 476 | + config.http_tag_query_string = True |
| 477 | + |
| 478 | + with caplog.at_level(logging.WARNING), mock.patch( |
| 479 | + "ddtrace.appsec.ddwaf.ddwaf_run", side_effect=TypeError("expected c_long instead of int") |
| 480 | + ), override_env(dict(DD_TRACE_CLIENT_IP_HEADER_DISABLED="False")): |
| 481 | + with tracer.trace("test", span_type=SpanTypes.WEB) as span: |
| 482 | + set_http_meta( |
| 483 | + span, |
| 484 | + config, |
| 485 | + method="PATCH", |
| 486 | + url=u"http://localhost/api/unstable/role_requests/dab1e9ae-9d99-11ed-bfdf-da7ad0900000?_authentication_token=2b0297348221f294de3a047e2ecf1235abb866b6", # noqa: E501 |
| 487 | + status_code="200", |
| 488 | + raw_uri=u"http://localhost/api/unstable/role_requests/dab1e9ae-9d99-11ed-bfdf-da7ad0900000?_authentication_token=2b0297348221f294de3a047e2ecf1235abb866b6", # noqa: E501 |
| 489 | + request_headers={ |
| 490 | + "host": u"localhost", |
| 491 | + "user-agent": "aa", |
| 492 | + "content-length": u"73", |
| 493 | + }, |
| 494 | + response_headers={ |
| 495 | + "content-length": "501", |
| 496 | + "x-ratelimit-remaining": "363", |
| 497 | + "x-ratelimit-name": "role_api", |
| 498 | + "x-ratelimit-limit": "500", |
| 499 | + "x-ratelimit-period": "60", |
| 500 | + "content-type": "application/json", |
| 501 | + "x-ratelimit-reset": "16", |
| 502 | + }, |
| 503 | + request_body={"_authentication_token": u"2b0297348221f294de3a047e2ecf1235abb866b6"}, |
| 504 | + ) |
| 505 | + |
| 506 | + assert span.get_tag(APPSEC_JSON) is None |
| 507 | + assert "Error executing Appsec In-App WAF: TypeError('expected c_long instead of int'" in caplog.text |
| 508 | + |
| 509 | + |
| 510 | +def test_ddwaf_run_contained_oserror(tracer_appsec, caplog): |
| 511 | + tracer = tracer_appsec |
| 512 | + |
| 513 | + config = Config() |
| 514 | + config.http_tag_query_string = True |
| 515 | + |
| 516 | + with caplog.at_level(logging.WARNING), mock.patch( |
| 517 | + "ddtrace.appsec.ddwaf.ddwaf_run", side_effect=OSError("ddwaf run failed") |
| 518 | + ), override_env(dict(DD_TRACE_CLIENT_IP_HEADER_DISABLED="False")): |
| 519 | + with tracer.trace("test", span_type=SpanTypes.WEB) as span: |
| 520 | + set_http_meta( |
| 521 | + span, |
| 522 | + config, |
| 523 | + method="PATCH", |
| 524 | + url=u"http://localhost/api/unstable/role_requests/dab1e9ae-9d99-11ed-bfdf-da7ad0900000?_authentication_token=2b0297348221f294de3a047e2ecf1235abb866b6", # noqa: E501 |
| 525 | + status_code="200", |
| 526 | + raw_uri=u"http://localhost/api/unstable/role_requests/dab1e9ae-9d99-11ed-bfdf-da7ad0900000?_authentication_token=2b0297348221f294de3a047e2ecf1235abb866b6", # noqa: E501 |
| 527 | + request_headers={ |
| 528 | + "host": u"localhost", |
| 529 | + "user-agent": "aa", |
| 530 | + "content-length": u"73", |
| 531 | + }, |
| 532 | + response_headers={ |
| 533 | + "content-length": "501", |
| 534 | + "x-ratelimit-remaining": "363", |
| 535 | + "x-ratelimit-name": "role_api", |
| 536 | + "x-ratelimit-limit": "500", |
| 537 | + "x-ratelimit-period": "60", |
| 538 | + "content-type": "application/json", |
| 539 | + "x-ratelimit-reset": "16", |
| 540 | + }, |
| 541 | + request_body={"_authentication_token": u"2b0297348221f294de3a047e2ecf1235abb866b6"}, |
| 542 | + ) |
| 543 | + |
| 544 | + assert span.get_tag(APPSEC_JSON) is None |
| 545 | + assert "Error executing Appsec In-App WAF: \nTraceback (" in caplog.text |
0 commit comments