Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use JSONSchema to verify the original data submitted by users #986

Merged
merged 18 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions api/filter/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 filter

import (
"bytes"
"io/ioutil"
"net/http"
"strings"

"github.com/gin-gonic/gin"

"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/utils/consts"
"github.com/apisix/manager-api/log"
)

var resources = map[string]string{
"routes": "route",
"upstreams": "upstream",
"services": "service",
"consumers": "consumer",
"ssl": "ssl",
}

func SchemaCheck() gin.HandlerFunc {
return func(c *gin.Context) {
pathPrefix := "/apisix/admin/"
resource := strings.Replace(c.Request.URL.Path, pathPrefix, "", 1)
tokers marked this conversation as resolved.
Show resolved Hide resolved

idx := strings.LastIndex(resource, "/")
if idx > 1 {
resource = resource[:idx]
}
method := strings.ToUpper(c.Request.Method)

if method != "PUT" && method != "POST" {
c.Next()
return
}
schemaKey, ok := resources[resource]
if !ok {
c.Next()
return
}

reqBody, err := c.GetRawData()
if err != nil {
log.Errorf("read request body failed: %s", err)
c.AbortWithStatusJSON(http.StatusBadRequest, consts.ErrInvalidRequest)
return
}

// other filter need it
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))

validator, err := store.NewAPISIXSchemaValidator("main." + schemaKey)
if err != nil {
log.Errorf("init validator failed: %s", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, consts.ErrSchemaValidateFailed)
return
}

if err := validator.Validate(reqBody); err != nil {
log.Warnf("data validate failed: %s", err)
c.AbortWithStatusJSON(http.StatusBadRequest, consts.ErrSchemaValidateFailed)
return
}

c.Next()
}
}
42 changes: 42 additions & 0 deletions api/internal/core/store/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,45 @@ func (v *APISIXJsonSchemaValidator) Validate(obj interface{}) error {

return nil
}

type APISIXSchemaValidator struct {
schema *gojsonschema.Schema
}

func NewAPISIXSchemaValidator(jsonPath string) (Validator, error) {
schemaDef := conf.Schema.Get(jsonPath).String()
if schemaDef == "" {
log.Warnf("schema validate failed: schema not found, path: %s", jsonPath)
return nil, fmt.Errorf("schema validate failed: schema not found, path: %s", jsonPath)
}

s, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(schemaDef))
if err != nil {
log.Warnf("new schema failed: %w", err)
return nil, fmt.Errorf("new schema failed: %w", err)
}
return &APISIXSchemaValidator{
schema: s,
}, nil
}

func (v *APISIXSchemaValidator) Validate(obj interface{}) error {
ret, err := v.schema.Validate(gojsonschema.NewBytesLoader(obj.([]byte)))
if err != nil {
log.Warnf("schema validate failed: %w", err)
return fmt.Errorf("schema validate failed: %w", err)
}

if !ret.Valid() {
errString := buffer.Buffer{}
for i, vErr := range ret.Errors() {
if i != 0 {
errString.AppendString("\n")
}
errString.AppendString(vErr.String())
}
return fmt.Errorf("schema validate failed: %s", errString.String())
}

return nil
}
41 changes: 41 additions & 0 deletions api/internal/core/store/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,44 @@ func TestAPISIXJsonSchemaValidator_Route_checkRemoteAddr(t *testing.T) {
assert.Equal(t, tc.wantValidateErr, err, tc.caseDesc)
}
}

func TestAPISIXSchemaValidator_Validate(t *testing.T) {
validator, err := NewAPISIXSchemaValidator("main.consumer")
assert.Nil(t, err)

// normal config, should pass
reqBody := `{
"id": "jack",
"username": "jack",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description"
}`
err = validator.Validate([]byte(reqBody))
assert.Nil(t, err)

// config with not exists field, should be failed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not exists => non existent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

reqBody = `{
"username": "jack",
"not-exist": "val",
tokers marked this conversation as resolved.
Show resolved Hide resolved
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"desc": "test description"
}`
err = validator.Validate([]byte(reqBody))
assert.NotNil(t, err)
assert.EqualError(t, err, "schema validate failed: (root): Additional property not-exist is not allowed")

}
2 changes: 1 addition & 1 deletion api/internal/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SetUpRouter() *gin.Engine {
r := gin.New()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("session", store))
r.Use(filter.CORS(), filter.Authentication(), filter.RequestId(), filter.RecoverHandler())
r.Use(filter.CORS(), filter.Authentication(), filter.RequestId(), filter.SchemaCheck(), filter.RecoverHandler())
r.Use(static.Serve("/", static.LocalFile(conf.WebDir, false)))
r.NoRoute(func(c *gin.Context) {
c.File(fmt.Sprintf("%s/index.html", conf.WebDir))
Expand Down
5 changes: 3 additions & 2 deletions api/internal/utils/consts/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package consts
import "github.com/shiningrush/droplet/data"

const (
ErrCodeDemoBiz = 20000
ErrBadRequest = 20001
)

var (
// base error please refer to github.com/shiningrush/droplet/data, such as data.ErrNotFound, data.ErrConflicted
ErrDemoBiz = data.BaseError{Code: ErrCodeDemoBiz, Message: "this is just a demo error"}
ErrInvalidRequest = data.BaseError{Code: ErrBadRequest, Message: "invalid request"}
ErrSchemaValidateFailed = data.BaseError{Code: ErrBadRequest, Message: "JSONSchema validate failed"}
)
59 changes: 59 additions & 0 deletions api/test/e2e/json_schema_validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 e2e

import (
"net/http"
"testing"
)

func TestSchema_not_exist_field(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "config route with non-existent fields",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/routes/r1",
Method: http.MethodPut,
Body: `{
"uri": "/hello",
"nonexistent": "test non-existent",
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "172.16.238.20",
"port": 1980,
"weight": 1
}]
}
}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusBadRequest,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to check the response body

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any news?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

},
{
caseDesc: "make sure the route create failed",
Object: APISIXExpect(t),
Method: http.MethodGet,
Path: "/hello",
ExpectStatus: http.StatusNotFound,
Sleep: sleepTime,
},
}

for _, tc := range tests {
testCaseCheck(tc)
}
}