|
37 | 37 | APPLICATION_FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"
|
38 | 38 |
|
39 | 39 |
|
| 40 | +class OpenAPIRequestValidationMiddleware(BaseMiddlewareHandler): |
| 41 | + """ |
| 42 | + OpenAPI request validation middleware - validates only incoming requests. |
| 43 | +
|
| 44 | + This middleware should be used first in the middleware chain to validate |
| 45 | + requests before they reach user middlewares. |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + """Initialize the request validation middleware.""" |
| 50 | + pass |
| 51 | + |
| 52 | + def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) -> Response: |
| 53 | + logger.debug("OpenAPIRequestValidationMiddleware handler") |
| 54 | + |
| 55 | + route: Route = app.context["_route"] |
| 56 | + |
| 57 | + values: dict[str, Any] = {} |
| 58 | + errors: list[Any] = [] |
| 59 | + |
| 60 | + # Process path values, which can be found on the route_args |
| 61 | + path_values, path_errors = _request_params_to_args( |
| 62 | + route.dependant.path_params, |
| 63 | + app.context["_route_args"], |
| 64 | + ) |
| 65 | + |
| 66 | + # Normalize query values before validate this |
| 67 | + query_string = _normalize_multi_query_string_with_param( |
| 68 | + app.current_event.resolved_query_string_parameters, |
| 69 | + route.dependant.query_params, |
| 70 | + ) |
| 71 | + |
| 72 | + # Process query values |
| 73 | + query_values, query_errors = _request_params_to_args( |
| 74 | + route.dependant.query_params, |
| 75 | + query_string, |
| 76 | + ) |
| 77 | + |
| 78 | + # Normalize header values before validate this |
| 79 | + headers = _normalize_multi_header_values_with_param( |
| 80 | + app.current_event.resolved_headers_field, |
| 81 | + route.dependant.header_params, |
| 82 | + ) |
| 83 | + |
| 84 | + # Process header values |
| 85 | + header_values, header_errors = _request_params_to_args( |
| 86 | + route.dependant.header_params, |
| 87 | + headers, |
| 88 | + ) |
| 89 | + |
| 90 | + values.update(path_values) |
| 91 | + values.update(query_values) |
| 92 | + values.update(header_values) |
| 93 | + errors += path_errors + query_errors + header_errors |
| 94 | + |
| 95 | + # Process the request body, if it exists |
| 96 | + if route.dependant.body_params: |
| 97 | + (body_values, body_errors) = _request_body_to_args( |
| 98 | + required_params=route.dependant.body_params, |
| 99 | + received_body=self._get_body(app), |
| 100 | + ) |
| 101 | + values.update(body_values) |
| 102 | + errors.extend(body_errors) |
| 103 | + |
| 104 | + if errors: |
| 105 | + # Raise the validation errors |
| 106 | + raise RequestValidationError(_normalize_errors(errors)) |
| 107 | + |
| 108 | + # Re-write the route_args with the validated values |
| 109 | + app.context["_route_args"] = values |
| 110 | + |
| 111 | + # Call the next middleware |
| 112 | + return next_middleware(app) |
| 113 | + |
| 114 | + def _get_body(self, app: EventHandlerInstance) -> dict[str, Any]: |
| 115 | + """ |
| 116 | + Get the request body from the event, and parse it according to content type. |
| 117 | + """ |
| 118 | + content_type = app.current_event.headers.get("content-type", "").strip() |
| 119 | + |
| 120 | + # Handle JSON content |
| 121 | + if not content_type or content_type.startswith(APPLICATION_JSON_CONTENT_TYPE): |
| 122 | + return self._parse_json_data(app) |
| 123 | + |
| 124 | + # Handle URL-encoded form data |
| 125 | + elif content_type.startswith(APPLICATION_FORM_CONTENT_TYPE): |
| 126 | + return self._parse_form_data(app) |
| 127 | + |
| 128 | + else: |
| 129 | + raise NotImplementedError("Only JSON body or Form() are supported") |
| 130 | + |
| 131 | + def _parse_json_data(self, app: EventHandlerInstance) -> dict[str, Any]: |
| 132 | + """Parse JSON data from the request body.""" |
| 133 | + try: |
| 134 | + return app.current_event.json_body |
| 135 | + except json.JSONDecodeError as e: |
| 136 | + raise RequestValidationError( |
| 137 | + [ |
| 138 | + { |
| 139 | + "type": "json_invalid", |
| 140 | + "loc": ("body", e.pos), |
| 141 | + "msg": "JSON decode error", |
| 142 | + "input": {}, |
| 143 | + "ctx": {"error": e.msg}, |
| 144 | + }, |
| 145 | + ], |
| 146 | + body=e.doc, |
| 147 | + ) from e |
| 148 | + |
| 149 | + def _parse_form_data(self, app: EventHandlerInstance) -> dict[str, Any]: |
| 150 | + """Parse URL-encoded form data from the request body.""" |
| 151 | + try: |
| 152 | + body = app.current_event.decoded_body or "" |
| 153 | + # parse_qs returns dict[str, list[str]], but we want dict[str, str] for single values |
| 154 | + parsed = parse_qs(body, keep_blank_values=True) |
| 155 | + |
| 156 | + result: dict[str, Any] = {key: values[0] if len(values) == 1 else values for key, values in parsed.items()} |
| 157 | + return result |
| 158 | + |
| 159 | + except Exception as e: # pragma: no cover |
| 160 | + raise RequestValidationError( # pragma: no cover |
| 161 | + [ |
| 162 | + { |
| 163 | + "type": "form_invalid", |
| 164 | + "loc": ("body",), |
| 165 | + "msg": "Form data parsing error", |
| 166 | + "input": {}, |
| 167 | + "ctx": {"error": str(e)}, |
| 168 | + }, |
| 169 | + ], |
| 170 | + ) from e |
| 171 | + |
| 172 | + |
| 173 | +class OpenAPIResponseValidationMiddleware(BaseMiddlewareHandler): |
| 174 | + """ |
| 175 | + OpenAPI response validation middleware - validates only outgoing responses. |
| 176 | +
|
| 177 | + This middleware should be used last in the middleware chain to validate |
| 178 | + responses only from route handlers, not from user middlewares. |
| 179 | + """ |
| 180 | + |
| 181 | + def __init__( |
| 182 | + self, |
| 183 | + validation_serializer: Callable[[Any], str] | None = None, |
| 184 | + has_response_validation_error: bool = False, |
| 185 | + ): |
| 186 | + """ |
| 187 | + Initialize the response validation middleware. |
| 188 | +
|
| 189 | + Parameters |
| 190 | + ---------- |
| 191 | + validation_serializer : Callable, optional |
| 192 | + Optional serializer to use when serializing the response for validation. |
| 193 | + Use it when you have a custom type that cannot be serialized by the default jsonable_encoder. |
| 194 | +
|
| 195 | + has_response_validation_error: bool, optional |
| 196 | + Optional flag used to distinguish between payload and validation errors. |
| 197 | + By setting this flag to True, ResponseValidationError will be raised if response could not be validated. |
| 198 | + """ |
| 199 | + self._validation_serializer = validation_serializer |
| 200 | + self._has_response_validation_error = has_response_validation_error |
| 201 | + |
| 202 | + def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) -> Response: |
| 203 | + logger.debug("OpenAPIResponseValidationMiddleware handler") |
| 204 | + |
| 205 | + route: Route = app.context["_route"] |
| 206 | + |
| 207 | + # Call the next middleware (should be the route handler) |
| 208 | + response = next_middleware(app) |
| 209 | + |
| 210 | + # Process the response |
| 211 | + return self._handle_response(route=route, response=response) |
| 212 | + |
| 213 | + def _handle_response(self, *, route: Route, response: Response): |
| 214 | + # Process the response body if it exists |
| 215 | + if response.body and response.is_json(): |
| 216 | + response.body = self._serialize_response( |
| 217 | + field=route.dependant.return_param, |
| 218 | + response_content=response.body, |
| 219 | + has_route_custom_response_validation=route.custom_response_validation_http_code is not None, |
| 220 | + ) |
| 221 | + |
| 222 | + return response |
| 223 | + |
| 224 | + def _serialize_response( |
| 225 | + self, |
| 226 | + *, |
| 227 | + field: ModelField | None = None, |
| 228 | + response_content: Any, |
| 229 | + include: IncEx | None = None, |
| 230 | + exclude: IncEx | None = None, |
| 231 | + by_alias: bool = True, |
| 232 | + exclude_unset: bool = False, |
| 233 | + exclude_defaults: bool = False, |
| 234 | + exclude_none: bool = False, |
| 235 | + has_route_custom_response_validation: bool = False, |
| 236 | + ) -> Any: |
| 237 | + """ |
| 238 | + Serialize the response content according to the field type. |
| 239 | + """ |
| 240 | + if field: |
| 241 | + errors: list[dict[str, Any]] = [] |
| 242 | + value = _validate_field(field=field, value=response_content, loc=("response",), existing_errors=errors) |
| 243 | + if errors: |
| 244 | + # route-level validation must take precedence over app-level |
| 245 | + if has_route_custom_response_validation: |
| 246 | + raise ResponseValidationError( |
| 247 | + errors=_normalize_errors(errors), |
| 248 | + body=response_content, |
| 249 | + source="route", |
| 250 | + ) |
| 251 | + if self._has_response_validation_error: |
| 252 | + raise ResponseValidationError(errors=_normalize_errors(errors), body=response_content, source="app") |
| 253 | + |
| 254 | + raise RequestValidationError(errors=_normalize_errors(errors), body=response_content) |
| 255 | + |
| 256 | + if hasattr(field, "serialize"): |
| 257 | + return field.serialize( |
| 258 | + value, |
| 259 | + include=include, |
| 260 | + exclude=exclude, |
| 261 | + by_alias=by_alias, |
| 262 | + exclude_unset=exclude_unset, |
| 263 | + exclude_defaults=exclude_defaults, |
| 264 | + exclude_none=exclude_none, |
| 265 | + ) |
| 266 | + return jsonable_encoder( |
| 267 | + value, |
| 268 | + include=include, |
| 269 | + exclude=exclude, |
| 270 | + by_alias=by_alias, |
| 271 | + exclude_unset=exclude_unset, |
| 272 | + exclude_defaults=exclude_defaults, |
| 273 | + exclude_none=exclude_none, |
| 274 | + custom_serializer=self._validation_serializer, |
| 275 | + ) |
| 276 | + else: |
| 277 | + # Just serialize the response content returned from the handler. |
| 278 | + return jsonable_encoder(response_content, custom_serializer=self._validation_serializer) |
| 279 | + |
| 280 | + def _prepare_response_content( |
| 281 | + self, |
| 282 | + res: Any, |
| 283 | + *, |
| 284 | + exclude_unset: bool, |
| 285 | + exclude_defaults: bool = False, |
| 286 | + exclude_none: bool = False, |
| 287 | + ) -> Any: |
| 288 | + """ |
| 289 | + Prepares the response content for serialization. |
| 290 | + """ |
| 291 | + if isinstance(res, BaseModel): |
| 292 | + return _model_dump( |
| 293 | + res, |
| 294 | + by_alias=True, |
| 295 | + exclude_unset=exclude_unset, |
| 296 | + exclude_defaults=exclude_defaults, |
| 297 | + exclude_none=exclude_none, |
| 298 | + ) |
| 299 | + elif isinstance(res, list): |
| 300 | + return [ |
| 301 | + self._prepare_response_content(item, exclude_unset=exclude_unset, exclude_defaults=exclude_defaults) |
| 302 | + for item in res |
| 303 | + ] |
| 304 | + elif isinstance(res, dict): |
| 305 | + return { |
| 306 | + k: self._prepare_response_content(v, exclude_unset=exclude_unset, exclude_defaults=exclude_defaults) |
| 307 | + for k, v in res.items() |
| 308 | + } |
| 309 | + elif dataclasses.is_dataclass(res): |
| 310 | + return dataclasses.asdict(res) # type: ignore[arg-type] |
| 311 | + return res |
| 312 | + |
| 313 | + |
40 | 314 | class OpenAPIValidationMiddleware(BaseMiddlewareHandler):
|
41 | 315 | """
|
42 | 316 | OpenAPIValidationMiddleware is a middleware that validates the request against the OpenAPI schema defined by the
|
|
0 commit comments