Skip to content

Commit b12f08b

Browse files
authored
chore(*): add check of get_request() and the missing ';' (#51)
Reason of adding this check: Although it's indeed redundent given our phase check. but it gives us extra protection because this a private API from openresty, it doesn't guaranteeded to be unchanged in the future. And also given the severity when it happens, it will likely cause a segfault, instead of a lua vm crach.
1 parent befcfb0 commit b12f08b

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

lualib/resty/kong/grpc.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,24 @@ int ngx_http_lua_kong_ffi_set_grpc_authority(ngx_http_request_t *r,
3030
local error = error
3131
local type = type
3232
local C = ffi.C
33-
local get_request = base.get_request
33+
local orig_get_request = base.get_request
3434
local get_phase = ngx.get_phase
3535

3636

3737
local NGX_OK = ngx.OK
3838
local NGX_ERROR = ngx.ERROR
3939

4040

41+
local function get_request()
42+
local r = orig_get_request()
43+
44+
if not r then
45+
error("no request found")
46+
end
47+
48+
return r
49+
end
50+
4151
do
4252
local ALLOWED_PHASES = {
4353
['rewrite'] = true,

lualib/resty/kong/tag.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local base = require "resty.core.base"
44

55
local C = ffi.C
66
local ffi_str = ffi.string
7-
local get_request = base.get_request
7+
local orig_get_request = base.get_request
88
local subsystem = ngx.config.subsystem
99

1010

@@ -29,13 +29,19 @@ elseif subsystem == "stream" then
2929

3030
end
3131

32-
local function get()
33-
local r = get_request()
32+
local function get_request()
33+
local r = orig_get_request()
3434

3535
if not r then
3636
error("no request found")
3737
end
3838

39+
return r
40+
end
41+
42+
local function get()
43+
local r = get_request()
44+
3945
local tag = ngx_lua_kong_get_static_tag(r)
4046

4147
if tag and tag.len > 0 then

lualib/resty/kong/tls.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ local C = ffi.C
5555
local ffi_string = ffi.string
5656
local get_string_buf = base.get_string_buf
5757
local size_ptr = base.get_size_ptr()
58-
local get_request = base.get_request
58+
local orig_get_request = base.get_request
5959

6060

6161
local DEFAULT_CERT_CHAIN_SIZE = 10240
@@ -66,15 +66,23 @@ local NGX_DECLINED = ngx.DECLINED
6666
local NGX_ABORT = -6
6767

6868

69+
local function get_request()
70+
local r = orig_get_request()
71+
72+
if not r then
73+
error("no request found")
74+
end
75+
76+
return r
77+
end
78+
6979
if ngx.config.subsystem == "http" then
7080
function _M.request_client_certificate(no_session_reuse)
7181
if get_phase() ~= 'ssl_cert' then
7282
error("API disabled in the current context")
7383
end
7484

7585
local r = get_request()
76-
-- no need to check if r is nil as phase check above
77-
-- already ensured it
7886

7987
local errmsg = C.ngx_http_lua_kong_ffi_request_client_certificate(r)
8088
if errmsg == nil then

lualib/resty/kong/var.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local assert = assert
1212
local tostring = tostring
1313
local tonumber = tonumber
1414
local getmetatable = getmetatable
15-
local get_request = base.get_request
15+
local orig_get_request = base.get_request
1616
local get_size_ptr = base.get_size_ptr
1717
local get_phase = ngx.get_phase
1818
local subsystem = ngx.config.subsystem
@@ -56,6 +56,16 @@ local value_ptr = ffi_new("unsigned char *[1]")
5656
local errmsg = base.get_errmsg_ptr()
5757

5858

59+
local function get_request()
60+
local r = orig_get_request()
61+
62+
if not r then
63+
error("no request found")
64+
end
65+
66+
return r
67+
end
68+
5969
local function load_indexes()
6070
if get_phase() ~= "init" then
6171
error("load_indexes can only be called in init phase")
@@ -87,9 +97,6 @@ end
8797

8898
local function var_get_by_index(index)
8999
local r = get_request()
90-
if not r then
91-
error("no request found")
92-
end
93100

94101
local value_len = get_size_ptr()
95102

@@ -114,9 +121,6 @@ end
114121

115122
local function var_set_by_index(index, value)
116123
local r = get_request()
117-
if not r then
118-
error("no request found")
119-
end
120124

121125
local value_len
122126
if value == nil then

src/ngx_http_lua_kong_ssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ ngx_http_lua_kong_ffi_disable_session_reuse(ngx_http_request_t *r)
141141
return NULL;
142142

143143
#else
144-
return "TLS support is not enabled in Nginx build"
144+
return "TLS support is not enabled in Nginx build";
145145
#endif
146146
}
147147

@@ -174,7 +174,7 @@ ngx_http_lua_kong_ffi_request_client_certificate(ngx_http_request_t *r)
174174
return NULL;
175175

176176
#else
177-
return "TLS support is not enabled in Nginx build"
177+
return "TLS support is not enabled in Nginx build";
178178
#endif
179179
}
180180

0 commit comments

Comments
 (0)