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

feat(request-transformer): honor untrusted_lua configuration values #10327

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
- **Request-Transformer**: fix an issue where requests would intermittently
be proxied with incorrect query parameters.
[10539](https://github.com/Kong/kong/pull/10539)
- **Request Transformer**: honor value of untrusted_lua configuration parameter
[#10327](https://github.com/Kong/kong/pull/10327)

### Changed

Expand Down
30 changes: 25 additions & 5 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local multipart = require "multipart"
local cjson = require("cjson.safe").new()
local pl_template = require "pl.template"
local pl_tablex = require "pl.tablex"
local sandbox = require "kong.tools.sandbox"

local table_insert = table.insert
local get_uri_args = kong.request.get_query
Expand All @@ -23,6 +24,8 @@ local pairs = pairs
local error = error
local rawset = rawset
local pl_copy_table = pl_tablex.deepcopy
local lua_enabled = sandbox.configuration.enabled
local sandbox_enabled = sandbox.configuration.sandbox_enabled

local _M = {}
local template_cache = setmetatable( {}, { __mode = "k" })
Expand Down Expand Up @@ -74,6 +77,17 @@ local function param_value(source_template, config_array, template_env)
return nil
end

if not lua_enabled then
-- Detect expressions in the source template
local expr = str_find(source_template, "%$%(.*%)")
if expr then
return nil, "loading of untrusted Lua code disabled because " ..
"'untrusted_lua' config option is set to 'off'"
end
-- Lua is disabled, no need to render the template
return source_template
end

-- find compiled templates for this plugin-configuration array
local compiled_templates = template_cache[config_array]
if not compiled_templates then
Expand Down Expand Up @@ -498,7 +512,9 @@ function _M.execute(conf)
}
local loader = lazy_loaders[key]
if not loader then
-- we don't have a loader, so just return nothing
if lua_enabled and not sandbox_enabled then
return _G[key]
end
return
end
-- set the result on the table to not load again
Expand All @@ -511,13 +527,17 @@ function _M.execute(conf)
end,
}

local template_env = setmetatable({
local template_env = {}
if lua_enabled and sandbox_enabled then
-- load the sandbox environment to be used to render the template
template_env = pl_copy_table(sandbox.configuration.environment)
-- here we can optionally add functions to expose to the sandbox, eg:
-- tostring = tostring, -- for example
-- tostring = tostring,
-- because headers may contain array elements such as duplicated headers
-- type is a useful function in these cases. See issue #25.
type = type,
}, __meta_environment)
template_env.type = type
end
setmetatable(template_env, __meta_environment)

transform_uri(conf, template_env)
transform_method(conf)
Expand Down
Loading