AlexStocks commented on code in PR #909: URL: https://github.com/apache/dubbo-go-pixiu/pull/909#discussion_r3098103820
########## pkg/filter/http/apiconfig/openapi/validator.go: ########## @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package openapi + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "math" + "net/http" + "strings" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/router" +) + +func ValidateRequest(req *http.Request, plan *ValidationPlan) error { + if req == nil || plan == nil { + return nil + } + + if err := validatePathParameters(req, plan); err != nil { + return err + } + + if err := validateQueryParameters(req, plan.QueryParameters); err != nil { + return err + } + + if err := validateHeaderParameters(req, plan.HeaderParameters); err != nil { + return err + } + + if err := validateRequestBody(req, plan.RequestBody); err != nil { + return err + } + + return nil +} + +func validatePathParameters(req *http.Request, plan *ValidationPlan) error { + if plan.RoutePattern == "" || len(plan.PathParameters) == 0 { + return nil + } + + values := router.GetURIParams(&router.API{URLPattern: plan.RoutePattern}, *req.URL) + for _, param := range plan.PathParameters { + value := values.Get(param.Name) + if param.Required && strings.TrimSpace(value) == "" { + return fmt.Errorf("path parameter %q is required", param.Name) + } + if value == "" { + continue + } + if len(param.Enum) > 0 && !contains(param.Enum, value) { + return fmt.Errorf("path parameter %q must be one of %v", param.Name, param.Enum) + } + } + + return nil +} + +func validateQueryParameters(req *http.Request, params []ParameterValidation) error { Review Comment: [P1] 这里的参数校验目前只处理了 `required` 和 `enum`,没有消费 `compileParameter()` 已经编译出来的 `Type`,也没有覆盖 `minimum` / `maximum` / `minLength` / `maxLength`。按这条实现,像 `type: integer` 的 query/path/header 参数传入 `abc` 会直接放过;我本地最小复现 `page=abc` 也返回了成功。文档已经把这些约束列为 V1 支持范围,这里如果不补参数级别的 schema 校验,运行时行为会和 OpenAPI 描述不一致。 ########## pkg/filter/http/apiconfig/api_config.go: ########## @@ -74,12 +79,22 @@ func (factory *FilterFactory) Apply() error { return nil } + if factory.cfg.EnableOpenAPIValidation && factory.cfg.OpenAPIPath != "" { Review Comment: [P1] 这里把 `Dynamic` 分支直接短路返回了,导致同一个过滤器配置里即使同时设置了 `openapi_path` 和 `enable_openapi_validation`,本地 OpenAPI 文件也永远不会加载,而且运行时没有任何报错或告警。当前 `ApiConfigConfig` 又没有把这组组合声明为非法配置,所以最终效果会变成“看起来开了校验,实际上完全没生效”。如果 V1 不支持 `dynamic + openapi_path` 组合,建议在这里显式返回配置错误;否则就需要调整执行顺序,避免静默忽略这部分配置。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
