Commit 304595c
authored
chore(aap): replace callbacks dict with typed block_callable attribut… (#16784)
APPSEC-61618
## Summary
- Replace the generic `callbacks` dict on `ASM_Environment` with a typed `block_callable` attribute, removing the indirect `get_value`/`set_value` pattern for storing the block request callable
- Move `_block_request_callable` from `ddtrace/contrib/internal/flask/patch.py` into `ddtrace/appsec/_contrib/flask/__init__.py` as `_flask_block_request_callable`, keeping Flask-specific blocking logic in the appsec Flask module
- Add `_make_block_response()` which returns a `(body, status, headers)` tuple instead of raising via `abort()`, preventing Flask's traced error handlers (`handle_exception`, `handle_http_exception`) from creating extra spans that lack fingerprint tags
## Motivation
The `callbacks` dict on `ASM_Environment` was a loosely-typed bag used to store a single block callable. Replacing it with a typed `block_callable: Optional[Callable]` attribute:
- Makes the data model explicit and easier to reason about
- Removes unused `_CALLBACKS` and `_BLOCK_CALL` constants
- Eliminates `get_value`/`set_value` indirection for this specific use case
The `_make_block_response()` function fixes system test failures (`test_fingerprinting_network_block`, `test_fingerprinting_header_block`, `test_fingerprinting_endpoint_blocking`, `test_session_blocking`) where fingerprint tags were missing from some spans. The root cause was `abort()` raising an `HTTPException`, which triggered Flask's traced `handle_exception`/`handle_http_exception` handlers, creating extra spans
without `_dd.appsec.fp.http.*` tags. Returning a tuple avoids Flask's error handling entirely.
## Changes
- **`ddtrace/appsec/_asm_request_context.py`**: Replace `self.callbacks: dict` with `self.block_callable: Optional[Callable]`. Update `set_block_request_callable()` and `block_request()` to use the attribute directly. Remove `_CALLBACKS` and `_BLOCK_CALL` constants.
- **`ddtrace/appsec/_contrib/flask/__init__.py`**: Add `_make_block_response()` (returns tuple) and `_flask_block_request_callable()` (uses `abort()`). Use `_make_block_response` in `_on_wrapped_view` for path-parameter blocking. Remove `get_value`/`set_value` imports.
- **`ddtrace/contrib/internal/flask/patch.py`**: Remove `_block_request_callable()` and the `block_request_callable` context item (now handled by the appsec Flask module).
- **`tests/appsec/appsec/test_asm_request_context.py`**: Update tests to use `env.block_callable` instead of `get_value("callbacks", "block")`.
- **`tests/appsec/integrations/flask_tests/test_appsec_flask.py`**: Use `get_triggers()` to extract the actual `block_id` from WAF triggers instead of hardcoding `"default"`.
Co-authored-by: christophe.papazian <[email protected]>1 parent 8e3a91d commit 304595c
File tree
7 files changed
+78
-47
lines changed- ddtrace
- appsec
- _contrib
- django
- flask
- contrib/internal/flask
- tests/appsec
- appsec
- contrib_appsec
- integrations/flask_tests
7 files changed
+78
-47
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
70 | 69 | | |
71 | 70 | | |
72 | | - | |
73 | 71 | | |
74 | 72 | | |
75 | 73 | | |
| |||
114 | 112 | | |
115 | 113 | | |
116 | 114 | | |
117 | | - | |
| 115 | + | |
118 | 116 | | |
119 | 117 | | |
120 | 118 | | |
| |||
337 | 335 | | |
338 | 336 | | |
339 | 337 | | |
340 | | - | |
| 338 | + | |
341 | 339 | | |
342 | 340 | | |
343 | 341 | | |
| |||
435 | 433 | | |
436 | 434 | | |
437 | 435 | | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
442 | 440 | | |
443 | 441 | | |
444 | 442 | | |
| |||
482 | 480 | | |
483 | 481 | | |
484 | 482 | | |
485 | | - | |
| 483 | + | |
486 | 484 | | |
487 | 485 | | |
488 | 486 | | |
489 | 487 | | |
490 | 488 | | |
491 | | - | |
492 | | - | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
493 | 493 | | |
494 | 494 | | |
495 | 495 | | |
496 | 496 | | |
497 | 497 | | |
498 | 498 | | |
499 | | - | |
500 | | - | |
501 | | - | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
502 | 502 | | |
503 | 503 | | |
504 | 504 | | |
| |||
515 | 515 | | |
516 | 516 | | |
517 | 517 | | |
518 | | - | |
| 518 | + | |
519 | 519 | | |
520 | 520 | | |
521 | 521 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
| 197 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
14 | | - | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
| |||
122 | 120 | | |
123 | 121 | | |
124 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
125 | 141 | | |
126 | 142 | | |
127 | 143 | | |
| |||
131 | 147 | | |
132 | 148 | | |
133 | 149 | | |
134 | | - | |
| 150 | + | |
135 | 151 | | |
136 | 152 | | |
137 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
138 | 174 | | |
139 | 175 | | |
140 | 176 | | |
141 | | - | |
142 | | - | |
143 | 177 | | |
144 | | - | |
145 | | - | |
146 | | - | |
| 178 | + | |
147 | 179 | | |
148 | 180 | | |
149 | 181 | | |
| |||
152 | 184 | | |
153 | 185 | | |
154 | 186 | | |
155 | | - | |
156 | 187 | | |
157 | 188 | | |
158 | 189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | | - | |
20 | | - | |
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
| |||
543 | 540 | | |
544 | 541 | | |
545 | 542 | | |
546 | | - | |
547 | | - | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | 543 | | |
556 | 544 | | |
557 | 545 | | |
| |||
563 | 551 | | |
564 | 552 | | |
565 | 553 | | |
566 | | - | |
567 | 554 | | |
568 | 555 | | |
569 | 556 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | | - | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
67 | | - | |
68 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
69 | 72 | | |
70 | 73 | | |
71 | 74 | | |
| |||
102 | 105 | | |
103 | 106 | | |
104 | 107 | | |
105 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
106 | 111 | | |
107 | 112 | | |
108 | 113 | | |
109 | | - | |
| 114 | + | |
110 | 115 | | |
111 | 116 | | |
112 | 117 | | |
| |||
Binary file not shown.
Lines changed: 10 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
75 | 79 | | |
76 | 80 | | |
77 | 81 | | |
| |||
82 | 86 | | |
83 | 87 | | |
84 | 88 | | |
85 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
86 | 94 | | |
87 | 95 | | |
88 | 96 | | |
| |||
0 commit comments