Skylm808 commented on code in PR #948: URL: https://github.com/apache/dubbo-go-pixiu/pull/948#discussion_r3369035830
########## pkg/filter/http/openapi/openapi.go: ########## @@ -0,0 +1,415 @@ +/* + * 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" + "io" + "net/http" + "os" + "path/filepath" + "strings" +) + +import ( + "github.com/pb33f/libopenapi" + openapiValidator "github.com/pb33f/libopenapi-validator" + validatorConfig "github.com/pb33f/libopenapi-validator/config" + validatorErrors "github.com/pb33f/libopenapi-validator/errors" + validatorPaths "github.com/pb33f/libopenapi-validator/paths" + validatorRadix "github.com/pb33f/libopenapi-validator/radix" + "github.com/pb33f/libopenapi/datamodel" + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" + + "github.com/pkg/errors" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/common/constant" + "github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter" + contexthttp "github.com/apache/dubbo-go-pixiu/pkg/context/http" + "github.com/apache/dubbo-go-pixiu/pkg/logger" +) + +const ( + // Kind is the kind of OpenAPI validation filter. + Kind = constant.HTTPOpenAPIFilter + + defaultMaxRequestBodyBytes = 1 << 20 +) + +var errOpenAPIRequestBodyTooLarge = errors.New("openapi request body too large") + +func init() { + filter.RegisterHttpFilter(&Plugin{}) +} + +type ( + Plugin struct { + } + + FilterFactory struct { + cfg *Config + validator openapiValidator.Validator + model *v3.Document + validationOptions *validatorConfig.ValidationOptions + maxRequestBody int64 + } + + Filter struct { + validator openapiValidator.Validator + model *v3.Document + validationOptions *validatorConfig.ValidationOptions + maxRequestBody int64 + } + + Config struct { + Path string `yaml:"path" json:"path,omitempty"` + // MaxRequestBodyBytes limits how much request body data OpenAPI validation may read. + // Zero uses the default limit. + MaxRequestBodyBytes int64 `yaml:"max_request_body_bytes" json:"max_request_body_bytes,omitempty"` + } +) + +func (p *Plugin) Kind() string { + return Kind +} + +func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) { + return &FilterFactory{cfg: &Config{}}, nil +} + +func (factory *FilterFactory) Config() any { + return factory.cfg +} + +func (factory *FilterFactory) Apply() error { + path, err := cleanOpenAPIPath(factory.cfg.Path) + if err != nil { + return err + } + factory.cfg.Path = path + + maxRequestBody, err := factory.cfg.effectiveMaxRequestBodyBytes() + if err != nil { + return err + } + + validator, model, validationOptions, err := loadValidatorFromFile(path) Review Comment: 确认这个风险存在,但根因在 `FilterManager.ReLoad()`,不是 OpenAPI filter。 本次 OpenAPI `Apply()` 已经会正常返回错误;问题是 `ReLoad()` 吞掉 error 后仍把 nil factory 放进 `filtersArray`,后续对 nil factory 调 `PrepareFilterChain()` 可能 panic。这个行为会影响所有 `Apply()` 可能失败的 filter,不只 OpenAPI。 建议作为单独 follow-up 修复 filter manager 的错误传播 / 失败 factory 安装逻辑。 -- 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]
