From 1c2aef5184ac0fabdbd25e73adce592619a818e8 Mon Sep 17 00:00:00 2001 From: samugi Date: Mon, 17 Jun 2024 13:03:41 +0200 Subject: [PATCH] feat(dynamic_hook): support non request-scoped hooks When a disabled and non-request scoped hook was called outside of a request scope, it was producing an error while trying to index ngx.ctx This commit adds a check to early return from is_group_enabled when outside of the request scope This allows for hooks to still work when used in other contexts where they can be enabled via the always_enable() function --- kong/dynamic_hook/init.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kong/dynamic_hook/init.lua b/kong/dynamic_hook/init.lua index 31b13b216af..2710e682d46 100644 --- a/kong/dynamic_hook/init.lua +++ b/kong/dynamic_hook/init.lua @@ -1,3 +1,5 @@ +local get_request = require "resty.core.base".get_request + local ngx = ngx local type = type local pcall = pcall @@ -233,6 +235,10 @@ function _M.is_group_enabled(group_name) return true end + if not get_request() then + return false + end + local dynamic_hook = ngx.ctx.dynamic_hook if not dynamic_hook then return false