You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/11 08:48:40 UTC

[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #986: fix: Use JSONSchema to verify the original data submitted by users

nic-chen commented on a change in pull request #986:
URL: https://github.com/apache/apisix-dashboard/pull/986#discussion_r540783484



##########
File path: api/filter/schema.go
##########
@@ -0,0 +1,105 @@
+/*
+ * 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"
+	"encoding/json"
+	"fmt"
+	"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)
+
+		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 {
+			errMsg := fmt.Sprintf("init validator failed: %s", err)
+			c.AbortWithStatusJSON(http.StatusBadRequest, consts.InvalidParam(errMsg))
+			log.Errorf(errMsg)

Review comment:
       fixed.

##########
File path: api/filter/schema.go
##########
@@ -0,0 +1,105 @@
+/*
+ * 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"
+	"encoding/json"
+	"fmt"
+	"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)

Review comment:
       fixed.

##########
File path: api/internal/core/store/validate_test.go
##########
@@ -437,3 +437,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.
+	reqBody = `{
+	      "username": "jack",
+              "not-exist": "val",

Review comment:
       fixed.

##########
File path: api/internal/core/store/validate_test.go
##########
@@ -437,3 +437,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.

Review comment:
       fixed.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org