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 2022/03/09 03:03:43 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #6546: feat: set cors allow origins by plugin metadata

spacewander commented on a change in pull request #6546:
URL: https://github.com/apache/apisix/pull/6546#discussion_r822247428



##########
File path: apisix/plugins/cors.lua
##########
@@ -239,10 +292,13 @@ function _M.header_filter(conf, ctx)
     local req_origin =  ctx.original_request_origin
     -- Try allow_origins first, if mismatched, try allow_origins_by_regex.
     local allow_origins
-    allow_origins = process_with_allow_origins(conf, ctx, req_origin)
+    allow_origins = process_with_allow_origins(conf.allow_origins, ctx, req_origin)
     if not match_origins(req_origin, allow_origins) then
         allow_origins = process_with_allow_origins_by_regex(conf, ctx, req_origin)
     end
+    if not match_origins(req_origin, allow_origins) then

Review comment:
       ```suggestion
       if not allow_origins then
   ```

##########
File path: t/plugin/cors3.t
##########
@@ -0,0 +1,383 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level("info");
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->no_error_log && !$block->error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: validate metadata allow_origins
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.cors")
+            local schema_type = require("apisix.core").schema.TYPE_METADATA
+            local function validate(val)
+                local conf = {}
+                conf.allow_origins = val
+                return plugin.check_schema(conf, schema_type)
+            end
+
+            local good = {
+                key_1 = "*",
+                key_2 = "**",
+                key_3 = "null",
+                key_4 = "http://y.com.uk",
+                key_5 = "https://x.com",
+                key_6 = "https://x.com,http://y.com.uk",
+                key_7 = "https://x.com,http://y.com.uk,http://c.tv",
+                key_8 = "https://x.com,http://y.com.uk:12000,http://c.tv",
+            }
+            local ok, err = validate(good)
+            if not ok then
+                ngx.say("failed to validate ", g, ", ", err)
+            end
+
+            local bad = {
+                "",
+                "*a",
+                "*,http://y.com",
+                "nulll",
+                "http//y.com.uk",
+                "x.com",
+                "https://x.com,y.com.uk",
+                "https://x.com,*,https://y.com.uk",
+                "https://x.com,http://y.com.uk,http:c.tv",
+            }
+            for _, b in ipairs(bad) do
+                local ok, err = validate({key = b})
+                if ok then
+                    ngx.say("failed to reject ", b)
+                end
+            end
+
+            ngx.say("done")
+        }
+    }
+--- response_body
+done
+
+
+
+=== TEST 2: set plugin metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/plugin_metadata/cors',
+                ngx.HTTP_PUT,
+                [[{
+                    "allow_origins": {
+                        "key_1": "https://domain.com",
+                        "key_2": "https://sub.domain.com,https://sub2.domain.com",
+                        "key_3": "*"
+                    },
+                    "inactive_timeout": 1
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t

Review comment:
       Let's use this script to simplify the duplicate fields:
   ```
   #!/usr/bin/env python
   # coding: utf-8
   # Usage: ./simpify.py $test_filename
   import sys
   with open(sys.argv[1]) as f:
       lines = [l.rstrip() for l in f.readlines()]
   newlines = []
   for i, l in enumerate(lines):
       if ((l == "GET /t" and lines[i-1] == "--- request") or
           (l == "[error]" and lines[i-1] == "--- no_error_log")):
           newlines = newlines[:-1]
           continue
       newlines.append(l)
   res = "\n".join(newlines)
   # print(res)
   with open(sys.argv[1], "w") as f:
      f.write(res + '\n') 
   ```




-- 
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: notifications-unsubscribe@apisix.apache.org

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