|
| 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 | +"""Internal utilities for MCP tools. |
| 16 | +
|
| 17 | +This module contains internal validation and sanitization utilities |
| 18 | +that are not part of the public API and follow RFC 7230 properly. |
| 19 | +
|
| 20 | +**Security Notes:** |
| 21 | +
|
| 22 | +- Header validation implements RFC 7230 §3.2 for proper HTTP header format |
| 23 | +- Only truly dangerous control characters are removed from header values |
| 24 | +- All functions log security-relevant warnings when appropriate |
| 25 | +
|
| 26 | +**RFC 7230 Compliance:** |
| 27 | +
|
| 28 | +- Header names: only letters, digits, and hyphens allowed |
| 29 | +- Header values: control characters (0x00-0x1F, 0x7F) are dangerous |
| 30 | +
|
| 31 | +**Attack Prevention:** |
| 32 | +
|
| 33 | +- HTTP header injection attacks via control character filtering |
| 34 | +- Response splitting attacks through CRLF handling |
| 35 | +- Log injection attacks via character sanitization |
| 36 | +""" |
| 37 | + |
| 38 | +from __future__ import annotations |
| 39 | + |
| 40 | +import logging |
| 41 | +import re |
| 42 | +from typing import Any |
| 43 | + |
| 44 | +logger = logging.getLogger("google_adk." + __name__) |
| 45 | + |
| 46 | +# RFC 7230 compliant header name pattern (allows letters, digits, hyphens) |
| 47 | +_HEADER_NAME_PATTERN = re.compile(r"^[a-zA-Z0-9-]+\Z") |
| 48 | + |
| 49 | +# Truly dangerous characters that should never appear in header values |
| 50 | +# These are characters that can break HTTP parsing or cause injection |
| 51 | +_DANGEROUS_CHARS = { |
| 52 | + "\x00", |
| 53 | + "\x01", |
| 54 | + "\x02", |
| 55 | + "\x03", |
| 56 | + "\x04", |
| 57 | + "\x05", |
| 58 | + "\x06", |
| 59 | + "\x07", |
| 60 | + "\x08", |
| 61 | + "\x0b", |
| 62 | + "\x0c", |
| 63 | + "\x0e", |
| 64 | + "\x0f", |
| 65 | + "\x10", |
| 66 | + "\x11", |
| 67 | + "\x12", |
| 68 | + "\x13", |
| 69 | + "\x14", |
| 70 | + "\x15", |
| 71 | + "\x16", |
| 72 | + "\x17", |
| 73 | + "\x18", |
| 74 | + "\x19", |
| 75 | + "\x1a", |
| 76 | + "\x1b", |
| 77 | + "\x1c", |
| 78 | + "\x1d", |
| 79 | + "\x1e", |
| 80 | + "\x1f", |
| 81 | + "\x7f", |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +def validate_header_name(header_name: str) -> None: |
| 86 | + """Validates that a header name conforms to RFC 7230. |
| 87 | + Only allows printable ASCII, no control chars, spaces, or separators. |
| 88 | + Rejects header names containing invalid characters. |
| 89 | + """ |
| 90 | + if not header_name: |
| 91 | + raise ValueError("Header name cannot be empty.") |
| 92 | + |
| 93 | + if not _HEADER_NAME_PATTERN.match(header_name): |
| 94 | + raise ValueError( |
| 95 | + f'Header name "{header_name}" contains invalid characters. ' |
| 96 | + "Header names must conform to RFC 7230 and cannot contain " |
| 97 | + 'control characters, spaces, or separators like ():<>@,;:\\"/[]?={}.' |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def validate_header_format(header_format: str) -> None: |
| 102 | + """Validates that a header format string doesn't contain CRLF injection. |
| 103 | +
|
| 104 | + This prevents header injection attacks where malicious format strings |
| 105 | + could inject additional headers via CRLF sequences. |
| 106 | +
|
| 107 | + Args: |
| 108 | + header_format: The format string to validate. |
| 109 | +
|
| 110 | + Raises: |
| 111 | + ValueError: If header_format contains CRLF sequences. |
| 112 | + """ |
| 113 | + if "\r" in header_format or "\n" in header_format: |
| 114 | + raise ValueError( |
| 115 | + "Header format string cannot contain CRLF (carriage return or line" |
| 116 | + " feed) characters due to header injection risk. Invalid format:" |
| 117 | + f" {repr(header_format)}" |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def sanitize_header_value(value: Any) -> str: |
| 122 | + """Sanitizes a header value to prevent injection attacks. |
| 123 | +
|
| 124 | + This is a wrapper that converts non-string values to strings and then |
| 125 | + applies core sanitization logic. |
| 126 | +
|
| 127 | + Args: |
| 128 | + value: The header value to sanitize (any type). |
| 129 | +
|
| 130 | + Returns: |
| 131 | + The sanitized header value as a string. |
| 132 | + """ |
| 133 | + if not isinstance(value, str): |
| 134 | + value = str(value) |
| 135 | + |
| 136 | + # Remove only characters that are truly dangerous for HTTP headers |
| 137 | + # These are control characters that can break parsing or enable injection |
| 138 | + # We DON'T remove all \r\n sequences as that would break legitimate multi-line headers |
| 139 | + # and violate RFC 7230 §3.2.4 which allows header folding |
| 140 | + sanitized_chars = [] |
| 141 | + for char in value: |
| 142 | + if char not in _DANGEROUS_CHARS: |
| 143 | + sanitized_chars.append(char) |
| 144 | + else: |
| 145 | + logger.warning( |
| 146 | + f"Removed dangerous character {repr(char)} from header value " |
| 147 | + "for security reasons" |
| 148 | + ) |
| 149 | + |
| 150 | + return "".join(sanitized_chars) |
| 151 | + |
| 152 | + |
| 153 | +def validate_header_value( |
| 154 | + state_key: str, value: Any, strict: bool = False |
| 155 | +) -> None: |
| 156 | + """Validates that a state value is suitable for use in a header. |
| 157 | +
|
| 158 | + Args: |
| 159 | + state_key: The key being validated. |
| 160 | + value: The value to validate. |
| 161 | + strict: If True, raises ValueError for non-primitive types. |
| 162 | +
|
| 163 | + Raises: |
| 164 | + ValueError: If strict=True and value is not a primitive type. |
| 165 | + """ |
| 166 | + if not isinstance(value, (str, int, float, bool)): |
| 167 | + msg = ( |
| 168 | + f'Value for state key "{state_key}" is of type ' |
| 169 | + f"{type(value).__name__}, which may not serialize correctly into a " |
| 170 | + "header. Consider pre-serializing complex values or using " |
| 171 | + "state_header_format." |
| 172 | + ) |
| 173 | + if strict: |
| 174 | + raise ValueError(msg) |
| 175 | + else: |
| 176 | + logger.warning(msg) |
0 commit comments