Skip to content

Commit

Permalink
bugfix: ngx.re: non-string values passed as string args might throw o…
Browse files Browse the repository at this point in the history
…ut errors.

This commit updates regex.lua to force the use of string types
in situations where the '#' operator is called. This resolves
potential thread abort situations where non-string types are used
as parameters to ngx.re.(g)sub, ngx.re.(g)match and ngx.re.find
as noted in issue #22.
  • Loading branch information
p0pr0ck5 authored and agentzh committed Feb 3, 2016
1 parent 430af53 commit 40445b1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/resty/core/regex.lua
Expand Up @@ -353,6 +353,10 @@ end


local function re_match_helper(subj, regex, opts, ctx, want_caps, res, nth)
-- we need to cast this to strings to avoid exceptions when they are
-- something else.
subj = tostring(subj)

local compiled, compile_once, flags = re_match_compile(regex, opts)
if compiled == nil then
-- compiled_once holds the error string
Expand Down Expand Up @@ -602,6 +606,7 @@ local function re_sub_func_helper(subj, regex, replace, opts, global)

-- exec the compiled regex

subj = tostring(subj)
local subj_len = #subj
local count = 0
local pos = 0
Expand Down Expand Up @@ -645,7 +650,7 @@ local function re_sub_func_helper(subj, regex, replace, opts, global)

local res = collect_captures(compiled, rc, subj, flags)

local bit = replace(res)
local bit = tostring(replace(res))
local bit_len = #bit

local new_dst_len = dst_len + prefix_len + bit_len
Expand Down Expand Up @@ -711,6 +716,7 @@ local function re_sub_str_helper(subj, regex, replace, opts, global)

-- exec the compiled regex

subj = tostring(subj)
local subj_len = #subj
local count = 0
local pos = 0
Expand Down
52 changes: 51 additions & 1 deletion t/re-match.t
Expand Up @@ -9,7 +9,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 5 + 3);
plan tests => repeat_each() * (blocks() * 5 + 1);

my $pwd = cwd();

Expand Down Expand Up @@ -541,3 +541,53 @@ NYI
--- error_log eval
qr/\[TRACE\s+\d+\s+/



=== TEST 14: subject is not a string type
--- http_config eval: $::HttpConfig
--- config
location /re {
content_by_lua '
local m = ngx.re.match(12345, [=[(\\d+)]=], "jo")

if m then
ngx.say(m[0])
ngx.say(m[1])
else
ngx.say("not matched")
end
';
}
--- request
GET /re
--- response_body
12345
12345
--- no_error_log
[error]
attempt to get length of local 'subj' (a number value)



=== TEST 15: subject is not a string type
--- http_config eval: $::HttpConfig
--- config
location /re {
content_by_lua '
local m = ngx.re.match(12345, "123", "jo")

if m then
ngx.say(m[0])
else
ngx.say("not matched")
end
';
}
--- request
GET /re
--- response_body
123
--- no_error_log
[error]
attempt to get length of local 'regex' (a number value)

67 changes: 67 additions & 0 deletions t/re-sub.t
Expand Up @@ -326,3 +326,70 @@ GET /t
bad argument type
NYI



=== TEST 9: string replace subj is not a string type
--- http_config eval: $::HttpConfig
--- config
location /re {
content_by_lua '
local newstr, n, err = ngx.re.sub(1234, "([0-9])[0-9]", 5, "jo")

ngx.say(newstr)
';
}
--- request
GET /re
--- response_body
534
--- no_error_log
[error]
attempt to get length of local 'subj' (a number value)



=== TEST 10: func replace return is not a string type (ngx.re.sub)
--- http_config eval: $::HttpConfig
--- config
location /re {
content_by_lua '
local lookup = function(m)
-- note we are returning a number type here
return 5
end

local newstr, n, err = ngx.re.sub("hello, 1234", "([0-9])[0-9]", lookup, "jo")
ngx.say(newstr)
';
}
--- request
GET /re
--- response_body
hello, 534
--- no_error_log
[error]
attempt to get length of local 'bit' (a number value)



=== TEST 11: func replace return is not a string type (ngx.re.gsub)
--- http_config eval: $::HttpConfig
--- config
location /re {
content_by_lua '
local lookup = function(m)
-- note we are returning a number type here
return 5
end

local newstr, n, err = ngx.re.gsub("hello, 1234", "([0-9])[0-9]", lookup, "jo")
ngx.say(newstr)
';
}
--- request
GET /re
--- response_body
hello, 55
--- no_error_log
[error]
attempt to get length of local 'bit' (a number value)

0 comments on commit 40445b1

Please sign in to comment.