|
| 1 | +package jsonschema_validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "github.com/getkin/kin-openapi/openapi3" |
| 9 | + "github.com/webhookx-io/webhookx/pkg/errs" |
| 10 | + "github.com/webhookx-io/webhookx/pkg/http/response" |
| 11 | + "github.com/webhookx-io/webhookx/pkg/plugin" |
| 12 | + "github.com/webhookx-io/webhookx/pkg/types" |
| 13 | + "github.com/webhookx-io/webhookx/plugins/jsonschema_validator/jsonschema" |
| 14 | + "github.com/webhookx-io/webhookx/utils" |
| 15 | +) |
| 16 | + |
| 17 | +type Config struct { |
| 18 | + Draft string `json:"draft" validate:"required,oneof=6 default:6"` |
| 19 | + DefaultSchema string `json:"default_schema" validate:"omitempty,json,max=1048576"` |
| 20 | + Schemas map[string]*Schema `json:"schemas" validate:"dive"` |
| 21 | +} |
| 22 | + |
| 23 | +type Schema struct { |
| 24 | + Schema string `json:"schema" validate:"omitempty,json,max=1048576"` |
| 25 | +} |
| 26 | + |
| 27 | +type SchemaValidatorPlugin struct { |
| 28 | + plugin.BasePlugin[Config] |
| 29 | +} |
| 30 | + |
| 31 | +func New(config []byte) (plugin.Plugin, error) { |
| 32 | + p := &SchemaValidatorPlugin{} |
| 33 | + p.Name = "jsonschema-validator" |
| 34 | + |
| 35 | + if config != nil { |
| 36 | + if err := p.UnmarshalConfig(config); err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + } |
| 40 | + return p, nil |
| 41 | +} |
| 42 | + |
| 43 | +func unmarshalAndValidateSchema(schema string) (*openapi3.Schema, error) { |
| 44 | + openapiSchema := &openapi3.Schema{} |
| 45 | + err := openapiSchema.UnmarshalJSON([]byte(schema)) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("value must be a valid jsonschema") |
| 48 | + } |
| 49 | + err = openapiSchema.Validate(context.Background(), openapi3.EnableSchemaFormatValidation()) |
| 50 | + if err != nil { |
| 51 | + return openapiSchema, err |
| 52 | + } |
| 53 | + return openapiSchema, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (p *SchemaValidatorPlugin) ValidateConfig() error { |
| 57 | + err := utils.Validate(p.Config) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + e := errs.NewValidateError(errors.New("request validation")) |
| 63 | + |
| 64 | + var defaultErr error |
| 65 | + if p.Config.DefaultSchema != "" { |
| 66 | + _, err := unmarshalAndValidateSchema(p.Config.DefaultSchema) |
| 67 | + if err != nil { |
| 68 | + defaultErr = err |
| 69 | + e.Fields = map[string]interface{}{ |
| 70 | + "default_schema": err.Error(), |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + for event, schema := range p.Config.Schemas { |
| 76 | + field := fmt.Sprintf("schemas[%s]", event) |
| 77 | + if schema == nil || schema.Schema == "" { |
| 78 | + if defaultErr != nil { |
| 79 | + e.Fields[field] = map[string]string{ |
| 80 | + "schema": "invalid due to reusing the default_schema definition", |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + _, err = unmarshalAndValidateSchema(schema.Schema) |
| 85 | + if err != nil { |
| 86 | + e.Fields[field] = map[string]string{ |
| 87 | + "schema": err.Error(), |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if len(e.Fields) > 0 { |
| 93 | + return e |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (p *SchemaValidatorPlugin) ExecuteInbound(inbound *plugin.Inbound) (res plugin.InboundResult, err error) { |
| 99 | + var event map[string]any |
| 100 | + body := inbound.RawBody |
| 101 | + if err = json.Unmarshal(body, &event); err != nil { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + eventType, ok := event["event_type"].(string) |
| 106 | + if !ok || eventType == "" { |
| 107 | + res.Payload = body |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + data := event["data"] |
| 112 | + if data == nil { |
| 113 | + res.Payload = body |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + schema, ok := p.Config.Schemas[eventType] |
| 118 | + if !ok { |
| 119 | + res.Payload = body |
| 120 | + return |
| 121 | + } |
| 122 | + if schema == nil || schema.Schema == "" { |
| 123 | + if p.Config.DefaultSchema == "" { |
| 124 | + res.Payload = body |
| 125 | + return |
| 126 | + } |
| 127 | + schema = &Schema{ |
| 128 | + Schema: p.Config.DefaultSchema, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + validator := jsonschema.New([]byte(schema.Schema)) |
| 133 | + e := validator.Validate(&jsonschema.ValidatorContext{ |
| 134 | + HTTPRequest: &jsonschema.HTTPRequest{ |
| 135 | + R: inbound.Request, |
| 136 | + Data: data.(map[string]any), |
| 137 | + }, |
| 138 | + }) |
| 139 | + if e != nil { |
| 140 | + response.JSON(inbound.Response, 400, types.ErrorResponse{ |
| 141 | + Message: "Request Validation", |
| 142 | + Error: e, |
| 143 | + }) |
| 144 | + res.Terminated = true |
| 145 | + return |
| 146 | + } |
| 147 | + res.Payload = body |
| 148 | + return |
| 149 | +} |
0 commit comments