|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Stop-gap for google/adk-python#7569 until #7134 lands. |
| 16 | +
|
| 17 | +Rewrites every tool confirmation answer that arrives on an invocation whose |
| 18 | +``user_id`` is not an allowlisted operator to ``confirmed=False``, before the |
| 19 | +runner stores the message. The rejected answer still resolves the pending |
| 20 | +call, so the agent returns the normal rejection result instead of stalling, |
| 21 | +which is what the reverted 9e9eaa6 got wrong. |
| 22 | +
|
| 23 | +Covers both places a confirmation answer enters the framework: |
| 24 | +
|
| 25 | +* the LlmAgent flow: a function response named ``adk_request_confirmation``; |
| 26 | +* workflow tool and function nodes: a function response named |
| 27 | + ``adk_request_input`` whose id is ``wf_tool_confirmation:<node_path>``. |
| 28 | +
|
| 29 | +Over A2A on current main, ``user_id`` is the authenticated user name when the |
| 30 | +A2A server authenticated the caller and ``A2A_USER_<context_id>`` otherwise, |
| 31 | +so an allowlist of operator names cannot be satisfied by an unauthenticated |
| 32 | +peer. It does NOT help on ``/run`` or ``/run_sse``, where ``user_id`` comes |
| 33 | +from the request body; those need API-server authentication in front of them. |
| 34 | +""" |
| 35 | + |
| 36 | +from __future__ import annotations |
| 37 | + |
| 38 | +from collections.abc import Iterable |
| 39 | +import logging |
| 40 | +from typing import Optional |
| 41 | + |
| 42 | +from google.adk.agents.invocation_context import InvocationContext |
| 43 | +from google.adk.plugins.base_plugin import BasePlugin |
| 44 | +from google.adk.utils._function_call_names import REQUEST_CONFIRMATION_FUNCTION_CALL_NAME |
| 45 | +from google.genai import types |
| 46 | + |
| 47 | +logger = logging.getLogger('google_adk.' + __name__) |
| 48 | + |
| 49 | +_WORKFLOW_CONFIRMATION_PREFIX = 'wf_tool_confirmation:' |
| 50 | + |
| 51 | + |
| 52 | +class OperatorOnlyConfirmationPlugin(BasePlugin): |
| 53 | + """Refuses tool confirmations from any user_id that is not an operator.""" |
| 54 | + |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + operators: Iterable[str], |
| 58 | + name: str = 'operator_only_confirmation', |
| 59 | + ): |
| 60 | + super().__init__(name=name) |
| 61 | + self._operators = frozenset(operators) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def _is_confirmation_answer(fr: types.FunctionResponse) -> bool: |
| 65 | + # The LlmAgent flow matches confirmation answers by name; the workflow |
| 66 | + # resume path keys resume inputs by id alone, so the id decides there and |
| 67 | + # the name is not consulted. |
| 68 | + if fr.name == REQUEST_CONFIRMATION_FUNCTION_CALL_NAME: |
| 69 | + return True |
| 70 | + return (fr.id or '').startswith(_WORKFLOW_CONFIRMATION_PREFIX) |
| 71 | + |
| 72 | + async def on_user_message_callback( |
| 73 | + self, |
| 74 | + *, |
| 75 | + invocation_context: InvocationContext, |
| 76 | + user_message: types.Content, |
| 77 | + ) -> Optional[types.Content]: |
| 78 | + if invocation_context.user_id in self._operators: |
| 79 | + return None |
| 80 | + if not user_message or not user_message.parts: |
| 81 | + return None |
| 82 | + |
| 83 | + changed = False |
| 84 | + parts = [] |
| 85 | + for part in user_message.parts: |
| 86 | + fr = part.function_response |
| 87 | + if fr is not None and self._is_confirmation_answer(fr): |
| 88 | + response = dict(fr.response) if isinstance(fr.response, dict) else {} |
| 89 | + if response.get('confirmed') is not False: |
| 90 | + response['confirmed'] = False |
| 91 | + part = part.model_copy( |
| 92 | + update={ |
| 93 | + 'function_response': fr.model_copy( |
| 94 | + update={'response': response} |
| 95 | + ) |
| 96 | + } |
| 97 | + ) |
| 98 | + changed = True |
| 99 | + logger.warning( |
| 100 | + 'Refusing tool confirmation %s from user_id %r: not an' |
| 101 | + ' allowlisted operator.', |
| 102 | + fr.id, |
| 103 | + invocation_context.user_id, |
| 104 | + ) |
| 105 | + parts.append(part) |
| 106 | + |
| 107 | + if not changed: |
| 108 | + return None |
| 109 | + return user_message.model_copy(update={'parts': parts}) |
0 commit comments