Skip to content

Commit d80f1c9

Browse files
committed
Add CSS obfuscation test
1 parent fe4c8b8 commit d80f1c9

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/stats/test_stats.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,59 @@ def test_grpc_status_code(self):
132132
f"Expected a gRPC stats entry with GRPCStatusCode=0, got: {grpc_stats}"
133133
)
134134

135+
@features.client_side_stats_supported # FIXME: create a new feature ?
136+
@scenarios.trace_stats_computation
137+
class Test_Client_Stats_With_Client_Obfuscation:
138+
"""Test client-side stats do the obfuscation before-hand when available"""
139+
140+
def setup_obfuscation(self):
141+
"""Setup for obfuscation test - generates SQL spans for obfuscation testing"""
142+
test_user_ids = ["1", "2", "admin", "test"]
143+
for user_id in test_user_ids:
144+
weblog.get(f"/rasp/sqli?user_id={user_id}")
145+
146+
def test_obfuscation(self):
147+
"""Test that SQL resources are obfuscated before stats aggregation.
148+
149+
Validates:
150+
- Datadog-Obfuscation-Version header is present on stats payloads
151+
- SQL resource names are obfuscated (literals replaced with ?)
152+
- All 4 distinct queries are aggregated into a single obfuscated resource bucket
153+
"""
154+
want = "SELECT * FROM users WHERE id = ?"
155+
sql_stats = []
156+
obfuscation_header_found = False
157+
158+
for data in interfaces.library.get_data("/v0.6/stats"):
159+
headers = {h[0].lower(): h[1] for h in data["request"]["headers"]}
160+
if "datadog-obfuscation-version" in headers:
161+
obfuscation_header_found = True
162+
assert headers["datadog-obfuscation-version"] == "1", (
163+
f"Expected obfuscation version '1', got '{headers['datadog-obfuscation-version']}'"
164+
)
165+
166+
payload = data["request"]["content"]
167+
for bucket in payload.get("Stats", []):
168+
for stat in bucket.get("Stats", []):
169+
if stat.get("Type") == "sql":
170+
sql_stats.append(stat)
171+
172+
assert obfuscation_header_found, (
173+
"Datadog-Obfuscation-Version header not found on any stats payload"
174+
)
175+
176+
assert len(sql_stats) > 0, "Expected at least one SQL stats entry"
177+
total_hits = 0
178+
for stat in sql_stats:
179+
assert stat["Resource"] == want, (
180+
f"Expected obfuscated resource '{want}', got '{stat['Resource']}'"
181+
)
182+
total_hits += stat["Hits"]
183+
184+
assert total_hits == 4, (
185+
f"Expected 4 SQL hits (one per query), got {total_hits}"
186+
)
187+
135188

136189
@features.service_override_source
137190
@scenarios.trace_stats_computation

0 commit comments

Comments
 (0)