Skip to content

Commit

Permalink
perf(core) limit router PCRE match and global PCRE match to a lower b…
Browse files Browse the repository at this point in the history
…acktracking limit

Co-authored-by: Datong Sun <[email protected]>
  • Loading branch information
javierguerragiraldez and dndx authored Jun 2, 2021
1 parent c963667 commit 8bc5d81
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,11 @@
#nginx_admin_client_body_buffer_size = 10m # Defines the buffer size for reading
# the request body on Admin API.

#nginx_http_lua_regex_match_limit = 100000 # Global `MATCH_LIMIT` for PCRE
# regex matching. The default of `100000` should ensure
# at worst any regex Kong executes could finish within
# roughly 2 seconds.

#------------------------------------------------------------------------------
# DATASTORE
#------------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion kong/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ local max = math.max
local band = bit.band
local bor = bit.bor

-- limits regex degenerate times to the low miliseconds
local REGEX_PREFIX = "(*LIMIT_MATCH=10000)"
local SLASH = byte("/")

local ERR = ngx.ERR
Expand Down Expand Up @@ -497,7 +499,7 @@ local function marshall_route(r)
local path = normalize_regex(path)

-- regex URI
local strip_regex = path .. [[(?<uri_postfix>.*)]]
local strip_regex = REGEX_PREFIX .. path .. [[(?<uri_postfix>.*)]]
local has_captures = has_capturing_groups(path)

local uri_t = {
Expand Down
1 change: 1 addition & 0 deletions kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ nginx_upstream_keepalive_timeout = NONE
nginx_http_upstream_keepalive = NONE
nginx_http_upstream_keepalive_requests = NONE
nginx_http_upstream_keepalive_timeout = NONE
nginx_http_lua_regex_match_limit = 100000
client_max_body_size = 0
client_body_buffer_size = 8k
Expand Down

0 comments on commit 8bc5d81

Please sign in to comment.