From c03d410b0a87bcc6cad66b5226cc006d042dc765 Mon Sep 17 00:00:00 2001 From: bunkerity Date: Fri, 9 Apr 2021 14:23:52 +0200 Subject: [PATCH] refactored whitelisting of user-agents --- confs/site/main-lua.conf | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/confs/site/main-lua.conf b/confs/site/main-lua.conf index 4ef8a5d..213d026 100644 --- a/confs/site/main-lua.conf +++ b/confs/site/main-lua.conf @@ -96,21 +96,17 @@ end -- check if user-agent is allowed if use_user_agent and ngx.var.bad_user_agent == "yes" then - local headers = ngx.req.get_headers() - local ua = headers["User-Agent"] - if not whitelist_user_agent ~= "" then - local k_ua_white, v_ua_white = next(whitelist_user_agent, nil) - while v_ua_white do - local rst_whitelist = string.match(ua, v_ua_white) - if rst_whitelist ~= nil and rst_whitelist ~= "" then - ngx.log(ngx.WARN, "[ALLOW] User-Agent " .. ngx.var.http_user_agent .. " is whitelisted") - ngx.exit(ngx.OK) - end - k_ua_white, v_ua_white = next(whitelist_user_agent, k_ua_white) - end + for k, v in pairs(whitelist_user_agent) then + if string.match(ngx.var.http_user_agent, v) then + ngx.log(ngx.WARN, "[ALLOW] User-Agent " .. ngx.var.http_user_agent .. " is whitelisted") + block = false + break + fi + end + if block then + ngx.log(ngx.WARN, "[BLOCK] User-Agent " .. ngx.var.http_user_agent .. " is blacklisted") + ngx.exit(ngx.HTTP_FORBIDDEN) end - ngx.log(ngx.WARN, "[BLOCK] User-Agent " .. ngx.var.http_user_agent .. " is blacklisted") - ngx.exit(ngx.HTTP_FORBIDDEN) end -- check if referrer is allowed