|
| 1 | +# Unless explicitly stated otherwise all files in this repository are licensed under the the Apache License Version 2.0. |
| 2 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +# Copyright 2021 Datadog, Inc. |
| 4 | + |
| 5 | +from utils import interfaces, rfc, scenarios, weblog, features, logger |
| 6 | +from utils.dd_constants import Capabilities |
| 7 | + |
| 8 | +from tests.appsec.api_security.utils import BaseAppsecApiSecurityRcTest |
| 9 | + |
| 10 | + |
| 11 | +def get_schema(request, address): |
| 12 | + """Get api security schema from spans""" |
| 13 | + for _, _, span in interfaces.library.get_spans(request): |
| 14 | + meta = span.get("meta", {}) |
| 15 | + key = "_dd.appsec.s." + address |
| 16 | + payload = meta.get(key) |
| 17 | + if payload is not None: |
| 18 | + return payload |
| 19 | + else: |
| 20 | + logger.info(f"Schema not found in span meta for {key}") |
| 21 | + return None |
| 22 | + |
| 23 | + |
| 24 | +@rfc("https://docs.google.com/document/d/1wBrd-ShGoA9-aP96o0VIe46eBgw73GL1315R8QjuMoc/edit?tab=t.0") |
| 25 | +@scenarios.appsec_api_security_rc |
| 26 | +@features.api_security_configuration |
| 27 | +class Test_API_Security_Custom_Data_Classification_Capabilities(BaseAppsecApiSecurityRcTest): |
| 28 | + """Validate that ASM_PROCESSOR_OVERRIDES and ASM_CUSTOM_DATA_SCANNERS capabilities are exposed""" |
| 29 | + |
| 30 | + def setup_capabilities_check(self): |
| 31 | + """Setup for capabilities validation""" |
| 32 | + self.setup_scenario() |
| 33 | + |
| 34 | + def test_capabilities_check(self): |
| 35 | + """Verify both ASM_PROCESSOR_OVERRIDES and ASM_CUSTOM_DATA_SCANNERS capabilities""" |
| 36 | + # Verify capability 16: ASM_PROCESSOR_OVERRIDES |
| 37 | + interfaces.library.assert_rc_capability(Capabilities.ASM_PROCESSOR_OVERRIDES) |
| 38 | + |
| 39 | + # Verify capability 17: ASM_CUSTOM_DATA_SCANNERS |
| 40 | + interfaces.library.assert_rc_capability(Capabilities.ASM_CUSTOM_DATA_SCANNERS) |
| 41 | + |
| 42 | + |
| 43 | +@rfc("https://docs.google.com/document/d/1wBrd-ShGoA9-aP96o0VIe46eBgw73GL1315R8QjuMoc/edit?tab=t.0") |
| 44 | +@scenarios.appsec_api_security_rc |
| 45 | +@features.api_security_configuration |
| 46 | +class Test_API_Security_Custom_Data_Classification_Processor_Override(BaseAppsecApiSecurityRcTest): |
| 47 | + """Test API Security - Custom Data Classification with Processor Override""" |
| 48 | + |
| 49 | + def setup_request_method(self): |
| 50 | + """Test that processor overrides work correctly with custom scanners""" |
| 51 | + self.setup_scenario() |
| 52 | + self.request = weblog.get("/tag_value/api_rc_processor/200?testcard=1234567890") |
| 53 | + |
| 54 | + def test_request_method(self): |
| 55 | + """Verify custom scanner detects data based on processor override configuration""" |
| 56 | + schema = get_schema(self.request, "req.querytest") |
| 57 | + assert self.request.status_code == 200 |
| 58 | + assert schema is not None, "Schema should be present in the span" |
| 59 | + assert isinstance(schema, list), "Schema should be a list" |
| 60 | + |
| 61 | + # Verify that the custom scanner detected the testcard parameter |
| 62 | + if len(schema) > 0: |
| 63 | + assert "testcard" in schema[0], "testcard parameter should be in the schema" |
| 64 | + |
| 65 | + |
| 66 | +@rfc("https://docs.google.com/document/d/1wBrd-ShGoA9-aP96o0VIe46eBgw73GL1315R8QjuMoc/edit?tab=t.0") |
| 67 | +@scenarios.appsec_api_security_rc |
| 68 | +@features.api_security_configuration |
| 69 | +class Test_API_Security_Custom_Data_Classification_Scanner(BaseAppsecApiSecurityRcTest): |
| 70 | + """Test API Security - Custom Data Classification with Custom Scanner""" |
| 71 | + |
| 72 | + def setup_request_method(self): |
| 73 | + """Test that custom scanners work correctly for request body""" |
| 74 | + self.setup_scenario() |
| 75 | + self.request = weblog.post("/tag_value/api_rc_scanner/200", data={"testcard": "1234567890"}) |
| 76 | + |
| 77 | + def test_request_method(self): |
| 78 | + """Verify custom scanner detects and classifies sensitive data in request body""" |
| 79 | + schema = get_schema(self.request, "req.bodytest") |
| 80 | + assert self.request.status_code == 200 |
| 81 | + assert schema is not None, "Schema should be present in the span" |
| 82 | + assert isinstance(schema, list), "Schema should be a list" |
| 83 | + |
| 84 | + # Verify that the custom scanner detected the testcard field |
| 85 | + if len(schema) > 0: |
| 86 | + assert "testcard" in schema[0], "testcard field should be in the schema" |
| 87 | + # Check if the value was classified with custom tags |
| 88 | + # Structure: schema[0]["testcard"] = [[[value_length, classification]], metadata] |
| 89 | + if isinstance(schema[0]["testcard"], list) and len(schema[0]["testcard"]) > 0: |
| 90 | + values = schema[0]["testcard"][0] |
| 91 | + if isinstance(values, list) and len(values) > 0 and isinstance(values[0], list): |
| 92 | + if len(values[0]) > 1: |
| 93 | + classification = values[0][1] |
| 94 | + assert isinstance(classification, dict), "Classification should be a dict" |
| 95 | + assert "category" in classification, "Classification should include category" |
| 96 | + assert classification["category"] == "testcategory", "Category should be testcategory" |
| 97 | + assert "type" in classification, "Classification should include type" |
| 98 | + assert classification["type"] == "card", "Type should be card" |
| 99 | + |
| 100 | + |
| 101 | +@rfc("https://docs.google.com/document/d/1wBrd-ShGoA9-aP96o0VIe46eBgw73GL1315R8QjuMoc/edit?tab=t.0") |
| 102 | +@scenarios.appsec_api_security_rc |
| 103 | +@features.api_security_configuration |
| 104 | +class Test_API_Security_Custom_Data_Classification_Multiple_Scanners(BaseAppsecApiSecurityRcTest): |
| 105 | + """Test API Security - Multiple Custom Scanners""" |
| 106 | + |
| 107 | + def setup_request_method(self): |
| 108 | + """Test that multiple scanners work together correctly""" |
| 109 | + self.setup_scenario() |
| 110 | + self.request = weblog.post( |
| 111 | + "/tag_value/api_rc_scanner/200", data={ "mail": "[email protected]", "testcard": "1234567890"} |
| 112 | + ) |
| 113 | + |
| 114 | + def test_request_method(self): |
| 115 | + """Verify both standard and custom scanners detect their respective data""" |
| 116 | + schema = get_schema(self.request, "req.bodytest") |
| 117 | + assert self.request.status_code == 200 |
| 118 | + assert schema is not None, "Schema should be present in the span" |
| 119 | + assert isinstance(schema, list), "Schema should be a list" |
| 120 | + |
| 121 | + if len(schema) > 0: |
| 122 | + # Check for email detection by standard scanner |
| 123 | + assert "mail" in schema[0], "mail field should be in the schema" |
| 124 | + # Check for testcard detection by custom scanner |
| 125 | + assert "testcard" in schema[0], "testcard field should be in the schema" |
| 126 | + |
| 127 | + |
| 128 | +@rfc("https://docs.google.com/document/d/1wBrd-ShGoA9-aP96o0VIe46eBgw73GL1315R8QjuMoc/edit?tab=t.0") |
| 129 | +@scenarios.appsec_api_security_rc |
| 130 | +@features.api_security_configuration |
| 131 | +class Test_API_Security_Custom_Data_Classification_Negative(BaseAppsecApiSecurityRcTest): |
| 132 | + """Test API Security - Custom Data Classification Negative Cases""" |
| 133 | + |
| 134 | + def setup_request_method(self): |
| 135 | + """Test that data not matching scanner patterns is not classified""" |
| 136 | + self.setup_scenario() |
| 137 | + self.request = weblog.post("/tag_value/api_rc_scanner/200", data={"normalfield": "normalvalue"}) |
| 138 | + |
| 139 | + def test_request_method(self): |
| 140 | + """Verify that normal data without sensitive patterns is not over-classified""" |
| 141 | + schema = get_schema(self.request, "req.bodytest") |
| 142 | + assert self.request.status_code == 200 |
| 143 | + assert schema is not None, "Schema should be present in the span" |
| 144 | + |
| 145 | + # The schema should exist but the field should not be classified as sensitive |
| 146 | + if len(schema) > 0 and "normalfield" in schema[0]: |
| 147 | + field_data = schema[0]["normalfield"] |
| 148 | + # If it's classified, it should not have sensitive category tags |
| 149 | + # Structure: field_data = [[[value_length, classification]], metadata] |
| 150 | + if isinstance(field_data, list) and len(field_data) > 0: |
| 151 | + values = field_data[0] |
| 152 | + if isinstance(values, list) and len(values) > 0 and isinstance(values[0], list): |
| 153 | + if len(values[0]) > 1: |
| 154 | + classification = values[0][1] |
| 155 | + if isinstance(classification, dict) and "category" in classification: |
| 156 | + assert classification["category"] not in [ |
| 157 | + "pii", |
| 158 | + "testcategory", |
| 159 | + ], "Normal fields should not be classified as sensitive" |
0 commit comments