Commit 95ece4f
fix(tool-service): harden _coerce_to_tool_result last-resort fallback against broken str/repr
Codex stop-review found that the prior fix still had escape hatches:
``type(payload).__name__``, ``str(payload)`` and ``repr(payload)`` all
live inside the last-resort fallback but none were individually
guarded, so a payload whose ``__str__`` and ``__repr__`` both raise
(or whose ``type().__name__`` lookup fails) could still escape the
helper and break the "never raises" invariant.
Extract two module-level helpers with staged defensive fallbacks:
* ``_safe_type_name(obj)`` — returns ``type(obj).__name__`` or
``"<untypeable>"`` sentinel, never raises.
* ``_safe_text_repr(obj, fallback_type)`` — tries ``str`` → ``repr`` →
``f"<{fallback_type} object (unrepresentable)>"``. Guarantees a
``str`` return on every branch; each coercion attempt is
try/except-guarded, and the final tier is a pre-computed literal
so even a metaclass whose ``__name__`` property raises cannot
propagate.
``_coerce_to_tool_result``'s opaque-JSON fallback now computes
``payload_type`` via ``_safe_type_name`` once up front and passes it
both to the DEBUG/WARNING log sites and to ``_safe_text_repr``.
Regression guard: ``test_payload_with_broken_str_and_repr_still_produces_toolresult``
exercises an ``AdversarialPayload`` whose ``__str__`` and ``__repr__``
both raise ``RuntimeError``. Against the pre-fix code either the
``str(payload)`` call or the fall-through ``repr(payload)`` would
propagate; against the new shape the helper emits the third-tier
sentinel text and a WARNING log. Empirically validated via an
in-line stress test of ``_safe_type_name`` and ``_safe_text_repr``
against a metaclass with a ``__name__`` property that raises.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Springer <jps@s390x.com>1 parent 699361b commit 95ece4f
File tree
3 files changed
+126
-36
lines changed- mcpgateway/services
- tests/unit/mcpgateway/services
3 files changed
+126
-36
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
8666 | 8666 | | |
8667 | 8667 | | |
8668 | 8668 | | |
8669 | | - | |
| 8669 | + | |
8670 | 8670 | | |
8671 | 8671 | | |
8672 | 8672 | | |
8673 | 8673 | | |
8674 | 8674 | | |
8675 | 8675 | | |
8676 | 8676 | | |
8677 | | - | |
| 8677 | + | |
8678 | 8678 | | |
8679 | 8679 | | |
8680 | 8680 | | |
8681 | 8681 | | |
8682 | 8682 | | |
8683 | 8683 | | |
8684 | 8684 | | |
8685 | | - | |
| 8685 | + | |
8686 | 8686 | | |
8687 | 8687 | | |
8688 | 8688 | | |
8689 | 8689 | | |
8690 | 8690 | | |
8691 | 8691 | | |
8692 | 8692 | | |
8693 | | - | |
| 8693 | + | |
8694 | 8694 | | |
8695 | 8695 | | |
8696 | 8696 | | |
8697 | 8697 | | |
8698 | 8698 | | |
8699 | 8699 | | |
8700 | 8700 | | |
8701 | | - | |
| 8701 | + | |
8702 | 8702 | | |
8703 | 8703 | | |
8704 | 8704 | | |
8705 | 8705 | | |
8706 | 8706 | | |
8707 | 8707 | | |
8708 | 8708 | | |
8709 | | - | |
| 8709 | + | |
8710 | 8710 | | |
8711 | 8711 | | |
8712 | 8712 | | |
8713 | 8713 | | |
8714 | 8714 | | |
8715 | 8715 | | |
8716 | 8716 | | |
8717 | | - | |
| 8717 | + | |
8718 | 8718 | | |
8719 | 8719 | | |
8720 | 8720 | | |
8721 | 8721 | | |
8722 | 8722 | | |
8723 | 8723 | | |
8724 | 8724 | | |
8725 | | - | |
| 8725 | + | |
8726 | 8726 | | |
8727 | 8727 | | |
8728 | 8728 | | |
8729 | 8729 | | |
8730 | 8730 | | |
8731 | 8731 | | |
8732 | 8732 | | |
8733 | | - | |
| 8733 | + | |
8734 | 8734 | | |
8735 | 8735 | | |
8736 | 8736 | | |
8737 | 8737 | | |
8738 | 8738 | | |
8739 | 8739 | | |
8740 | 8740 | | |
8741 | | - | |
| 8741 | + | |
8742 | 8742 | | |
8743 | 8743 | | |
8744 | 8744 | | |
8745 | 8745 | | |
8746 | 8746 | | |
8747 | 8747 | | |
8748 | 8748 | | |
8749 | | - | |
| 8749 | + | |
8750 | 8750 | | |
8751 | 8751 | | |
8752 | 8752 | | |
8753 | 8753 | | |
8754 | 8754 | | |
8755 | 8755 | | |
8756 | 8756 | | |
8757 | | - | |
| 8757 | + | |
8758 | 8758 | | |
8759 | 8759 | | |
8760 | 8760 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
371 | 371 | | |
372 | 372 | | |
373 | 373 | | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
374 | 429 | | |
375 | 430 | | |
376 | 431 | | |
| |||
1673 | 1728 | | |
1674 | 1729 | | |
1675 | 1730 | | |
1676 | | - | |
1677 | | - | |
1678 | | - | |
1679 | | - | |
1680 | | - | |
1681 | | - | |
1682 | | - | |
1683 | | - | |
1684 | | - | |
1685 | | - | |
1686 | | - | |
1687 | | - | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
1688 | 1742 | | |
1689 | 1743 | | |
1690 | 1744 | | |
1691 | 1745 | | |
1692 | | - | |
1693 | | - | |
1694 | | - | |
1695 | | - | |
1696 | 1746 | | |
1697 | | - | |
1698 | | - | |
| 1747 | + | |
| 1748 | + | |
1699 | 1749 | | |
1700 | 1750 | | |
1701 | | - | |
1702 | | - | |
1703 | | - | |
1704 | | - | |
1705 | | - | |
| 1751 | + | |
1706 | 1752 | | |
1707 | 1753 | | |
1708 | 1754 | | |
| |||
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2332 | 2332 | | |
2333 | 2333 | | |
2334 | 2334 | | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
| 2343 | + | |
| 2344 | + | |
| 2345 | + | |
| 2346 | + | |
| 2347 | + | |
| 2348 | + | |
| 2349 | + | |
| 2350 | + | |
| 2351 | + | |
| 2352 | + | |
| 2353 | + | |
| 2354 | + | |
| 2355 | + | |
| 2356 | + | |
| 2357 | + | |
| 2358 | + | |
| 2359 | + | |
| 2360 | + | |
| 2361 | + | |
| 2362 | + | |
| 2363 | + | |
| 2364 | + | |
| 2365 | + | |
| 2366 | + | |
| 2367 | + | |
| 2368 | + | |
| 2369 | + | |
| 2370 | + | |
| 2371 | + | |
| 2372 | + | |
| 2373 | + | |
| 2374 | + | |
| 2375 | + | |
| 2376 | + | |
| 2377 | + | |
| 2378 | + | |
2335 | 2379 | | |
2336 | 2380 | | |
2337 | 2381 | | |
| |||
0 commit comments