Skip to content

fix: redact encrypted fields from error log#12629

Merged
Revolyssup merged 35 commits into
apache:masterfrom
Revolyssup:revolyssup/remove-encrypt-field-from-log
Sep 30, 2025
Merged

fix: redact encrypted fields from error log#12629
Revolyssup merged 35 commits into
apache:masterfrom
Revolyssup:revolyssup/remove-encrypt-field-from-log

Conversation

@Revolyssup

@Revolyssup Revolyssup commented Sep 23, 2025

Copy link
Copy Markdown
Contributor

Currently sensitive encrypted fields are directly logged in plugins. This PR removes from all the places where sensitive information from plugins could be logged.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Sep 23, 2025
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Sep 23, 2025
@Revolyssup
Revolyssup marked this pull request as draft September 23, 2025 17:02
Comment thread apisix/balancer.lua Outdated
Comment on lines +199 to +210
for name, conf in pairs(ctx.var._ctx.consumer.plugins) do
local plugin = require("apisix.plugins."..name)
local schema
if plugin.type == "auth" then
schema = plugin.consumer_schema
else
schema = plugin.schema
end
redacted_ctx.var._ctx.consumer.plugins[name] = redact_encrypted(conf, schema)
local redacted_auth = redact_encrypted(redacted_ctx.var._ctx.consumer.auth_conf, schema)
redacted_ctx.var._ctx.consumer.auth_conf = redacted_auth
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this code snippet repeated multiple times, we should put it into a util function to reuse it.

Comment thread apisix/balancer.lua Outdated
local redacted_auth = redact_encrypted(redacted_ctx.var._ctx.consumer.auth_conf, schema)
redacted_ctx.var._ctx.consumer.auth_conf = redacted_auth
end
core.log.info("ctx: ", core.json.delay_encode(redacted_ctx, true))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should avoid to redact the log that will not be printed like json.delay_encode, my suggestion:

local max_delay_encode_items = 16
local delay_tab_idx = 0
local delay_tab_arr = {}
for i = 1, max_delay_encode_items do
    delay_tab_arr[i] = setmetatable({data = "", force = false}, {
        __tostring = function(self)
            if self.redact_func then
                self.data = deepcopy(self.data)
                self.redact_func(self.data)
            end
            local res, err = encode(self.data, self.force)
            if not res then
                ngx.log(ngx.WARN, "failed to encode: ", err,
                        " force: ", self.force)
            end

            return res
        end
    })
end


function _M.delay_encode(data, force, redact_func)
    delay_tab_idx = delay_tab_idx+1
    if delay_tab_idx > max_delay_encode_items then
        delay_tab_idx = 1
    end
    delay_tab_arr[delay_tab_idx].data = data
    delay_tab_arr[delay_tab_idx].force = force
    delay_tab_arr[delay_tab_idx].redact_func = redact_func
    return delay_tab_arr[delay_tab_idx]
end

only execute redact_func when this line of log be printed actually.

@Revolyssup
Revolyssup marked this pull request as ready for review September 24, 2025 07:27
Comment thread apisix/admin/resource.lua
## }
## After check, it only appears when watch recovers and returns events in bulk.
cat logs/error.log | grep "}, {" || (echo "failed: Log case 2 unexpected"; exit 1)
cat logs/error.log | grep "etcd events sent in bulk" || (echo "failed: Log case 2 unexpected"; exit 1)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since the entire res object is no longer logged. We programmatically assert in code and log this specific condition to make this test case pass.

local dir_res, err = waitdir(self)
log.info("waitdir key: ", self.key, " prev_index: ", self.prev_index + 1)
log.info("res: ", json.delay_encode(dir_res, true), ", err: ", err)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This log contained sensitive information and was being used to assert if there is a bulk operation. So the entire log is replaced with just the log about whether etcd events were sent in bulk.

Comment thread apisix/consumer.lua
Comment thread apisix/core/json.lua Outdated
local cjson = require("cjson.safe")
local json_encode = cjson.encode
local clear_tab = require("table.clear")
local table = require("apisix.core.table")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we do not need to update this library if we only remove some core.log(....)

Comment thread apisix/init.lua Outdated
nodes_ver, new_nodes)
core.log.info("parse route which contain domain: ",
core.json.delay_encode(route, true))
core.json.delay_encode(route, true, function (route)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same issue, we can remove this core.log.info

Comment thread apisix/plugin.lua Outdated
function _M.merge_consumer_route(route_conf, consumer_conf, consumer_group_conf, api_ctx)
core.log.info("route conf: ", core.json.delay_encode(route_conf))
core.log.info("consumer conf: ", core.json.delay_encode(consumer_conf))
core.log.info("consumer conf: ", core.json.delay_encode(consumer_conf, false,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/plugin.lua Outdated
end
core.log.info("check plugin schema, name: ", name, ", configurations: ",
core.json.delay_encode(plugin_conf, true))
core.json.delay_encode(redacted_plugin_conf, true))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/plugins/basic-auth.lua Outdated

function _M.rewrite(conf, ctx)
core.log.info("plugin access phase, conf: ", core.json.delay_encode(conf))
core.log.info("plugin access phase, conf: ", core.json.delay_encode(conf, false))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/plugins/hmac-auth.lua Outdated
return core.schema.check(schema, conf)
cur_schema = schema
end
core.log.info("input conf: ", core.json.delay_encode(conf_to_log, false))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/plugins/hmac-auth.lua Outdated
end
core.log.info("consumer: ", core.json.delay_encode(consumer, true))

core.log.info("consumer: ", core.json.delay_encode(consumer, true, function (cur_consumer)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/plugins/jwe-decrypt.lua Outdated
end
core.log.info("consumers: ", core.json.delay_encode(consumers))
return consumers[key]
local consumer = consumers[key]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/init.lua
resource.set_nodes_ver_and_nodes(route.value.upstream.resource_key,
nodes_ver, new_nodes)
-- remove plugin before logging to avoid logging sensitive info
local route_log = core.table.deepcopy(route)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this log is needed for test so only plugins are removed.

@membphis membphis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread apisix/plugin.lua


function _M.merge_consumer_route(route_conf, consumer_conf, consumer_group_conf, api_ctx)
core.log.info("route conf: ", core.json.delay_encode(route_conf))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does route_conf not print sensitive data?

@Revolyssup Revolyssup Sep 30, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Before merging, consumer_conf has the sensitive data. it would have been caught in test if route conf had sensitive data,.

@Revolyssup
Revolyssup merged commit 742fc43 into apache:master Sep 30, 2025
43 of 46 checks passed
@Revolyssup
Revolyssup deleted the revolyssup/remove-encrypt-field-from-log branch September 30, 2025 10:02
jizhuozhi pushed a commit to jizhuozhi/apisix that referenced this pull request Oct 18, 2025
shreemaan-abhishek pushed a commit to shreemaan-abhishek/apisix that referenced this pull request Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants