Commit 004403b
fix(tracing/anthropic): guard ContentBlockDelta / Stop indices from negatives
The upper-bound check on ContentBlockDelta/Stop was `idx < len(response.Content)`.
In Go, that condition is trivially satisfied by negative ints, so a malformed
or hostile upstream chunk with `Index: -1` slipped through and panicked on the
`response.Content[idx]` access below. The panic surfaces inside the tracing
pipeline (Anthropic messages recorder) and takes the request down when
OpenInference tracing is enabled.
Only ContentBlockStart had the non-negative guard. Extract the shared check
into `isValidContentBlockIndex(idx)` and apply it to every SSE index path
(Start / Delta / Stop). The upper bound (maxContentBlocks = 1000) is preserved
so a giant Index can't force a giant allocation either.
Regression test added: a chunk sequence with a valid Start followed by
ContentBlockDelta{Index:-5} and ContentBlockStop{Index:-1} now completes
without panic and produces the same content as if those events were absent.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent d230e3c commit 004403b
2 files changed
Lines changed: 72 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
236 | 249 | | |
237 | 250 | | |
238 | 251 | | |
| |||
270 | 283 | | |
271 | 284 | | |
272 | 285 | | |
273 | | - | |
274 | | - | |
275 | | - | |
| 286 | + | |
276 | 287 | | |
277 | 288 | | |
278 | 289 | | |
| |||
285 | 296 | | |
286 | 297 | | |
287 | 298 | | |
288 | | - | |
289 | | - | |
290 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
291 | 307 | | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
297 | 317 | | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
| 318 | + | |
| 319 | + | |
305 | 320 | | |
306 | 321 | | |
307 | 322 | | |
308 | 323 | | |
309 | 324 | | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
310 | 328 | | |
311 | 329 | | |
312 | 330 | | |
| |||
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
238 | 238 | | |
239 | 239 | | |
240 | 240 | | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
241 | 277 | | |
242 | 278 | | |
243 | 279 | | |
| |||
0 commit comments