|
| 1 | +-- |
| 2 | +-- Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +-- contributor license agreements. See the NOTICE file distributed with |
| 4 | +-- this work for additional information regarding copyright ownership. |
| 5 | +-- The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +-- (the "License"); you may not use this file except in compliance with |
| 7 | +-- the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 11 | +-- Unless required by applicable law or agreed to in writing, software |
| 12 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +-- See the License for the specific language governing permissions and |
| 15 | +-- limitations under the License. |
| 16 | +-- |
| 17 | +local core = require("apisix.core") |
| 18 | +local ngx = ngx |
| 19 | +local ipairs = ipairs |
| 20 | +local table = table |
| 21 | +local re_compile = require("resty.core.regex").re_match_compile |
| 22 | +local re_find = ngx.re.find |
| 23 | + |
| 24 | +local plugin_name = "ai-prompt-guard" |
| 25 | + |
| 26 | +local schema = { |
| 27 | + type = "object", |
| 28 | + properties = { |
| 29 | + match_all_roles = { |
| 30 | + type = "boolean", |
| 31 | + default = false, |
| 32 | + }, |
| 33 | + match_all_conversation_history = { |
| 34 | + type = "boolean", |
| 35 | + default = false, |
| 36 | + }, |
| 37 | + allow_patterns = { |
| 38 | + type = "array", |
| 39 | + items = {type = "string"}, |
| 40 | + default = {}, |
| 41 | + }, |
| 42 | + deny_patterns = { |
| 43 | + type = "array", |
| 44 | + items = {type = "string"}, |
| 45 | + default = {}, |
| 46 | + }, |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +local _M = { |
| 51 | + version = 0.1, |
| 52 | + priority = 1072, |
| 53 | + name = plugin_name, |
| 54 | + schema = schema, |
| 55 | +} |
| 56 | + |
| 57 | +function _M.check_schema(conf) |
| 58 | + local ok, err = core.schema.check(schema, conf) |
| 59 | + if not ok then |
| 60 | + return false, err |
| 61 | + end |
| 62 | + |
| 63 | + -- Validate allow_patterns |
| 64 | + for _, pattern in ipairs(conf.allow_patterns) do |
| 65 | + local compiled = re_compile(pattern, "jou") |
| 66 | + if not compiled then |
| 67 | + return false, "invalid allow_pattern: " .. pattern |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + -- Validate deny_patterns |
| 72 | + for _, pattern in ipairs(conf.deny_patterns) do |
| 73 | + local compiled = re_compile(pattern, "jou") |
| 74 | + if not compiled then |
| 75 | + return false, "invalid deny_pattern: " .. pattern |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + return true |
| 80 | +end |
| 81 | + |
| 82 | +local function get_content_to_check(conf, messages) |
| 83 | + if conf.match_all_conversation_history then |
| 84 | + return messages |
| 85 | + end |
| 86 | + local contents = {} |
| 87 | + if #messages > 0 then |
| 88 | + local last_msg = messages[#messages] |
| 89 | + if last_msg then |
| 90 | + core.table.insert(contents, last_msg) |
| 91 | + end |
| 92 | + end |
| 93 | + return contents |
| 94 | +end |
| 95 | + |
| 96 | +function _M.access(conf, ctx) |
| 97 | + local body = core.request.get_body() |
| 98 | + if not body then |
| 99 | + core.log.error("Empty request body") |
| 100 | + return 400, {message = "Empty request body"} |
| 101 | + end |
| 102 | + |
| 103 | + local json_body, err = core.json.decode(body) |
| 104 | + if err then |
| 105 | + return 400, {message = err} |
| 106 | + end |
| 107 | + |
| 108 | + local messages = json_body.messages or {} |
| 109 | + messages = get_content_to_check(conf, messages) |
| 110 | + if not conf.match_all_roles then |
| 111 | + -- filter to only user messages |
| 112 | + local new_messages = {} |
| 113 | + for _, msg in ipairs(messages) do |
| 114 | + if msg.role == "user" then |
| 115 | + core.table.insert(new_messages, msg) |
| 116 | + end |
| 117 | + end |
| 118 | + messages = new_messages |
| 119 | + end |
| 120 | + if #messages == 0 then --nothing to check |
| 121 | + return 200 |
| 122 | + end |
| 123 | + -- extract only messages |
| 124 | + local content = {} |
| 125 | + for _, msg in ipairs(messages) do |
| 126 | + if msg.content then |
| 127 | + core.table.insert(content, msg.content) |
| 128 | + end |
| 129 | + end |
| 130 | + local content_to_check = table.concat(content, " ") |
| 131 | + -- Allow patterns check |
| 132 | + if #conf.allow_patterns > 0 then |
| 133 | + local any_allowed = false |
| 134 | + for _, pattern in ipairs(conf.allow_patterns) do |
| 135 | + if re_find(content_to_check, pattern, "jou") then |
| 136 | + any_allowed = true |
| 137 | + break |
| 138 | + end |
| 139 | + end |
| 140 | + if not any_allowed then |
| 141 | + return 400, {message = "Request doesn't match allow patterns"} |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + -- Deny patterns check |
| 146 | + for _, pattern in ipairs(conf.deny_patterns) do |
| 147 | + if re_find(content_to_check, pattern, "jou") then |
| 148 | + return 400, {message = "Request contains prohibited content"} |
| 149 | + end |
| 150 | + end |
| 151 | +end |
| 152 | + |
| 153 | +return _M |
0 commit comments