Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions apache2/msc_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,26 +374,28 @@
lua_getglobal(L, "__rule");
rule = (msre_rule *)lua_topointer(L, -1);

/* A lua_CFunction returns the number of values it pushed, so always
* return 0 (the script gets nil), never a negative value or the
* status of msre_action_setvar_execute().
*/
if(nargs != 2) {
msr_log(msr, 8, "m.setvar: Failed m.setvar funtion must has 2 arguments");
return -1;
msr_log(msr, 8, "m.setvar: Failed m.setvar function must have 2 arguments");
return 0;
}
var_value = luaL_checkstring (L, 2);
var_name = luaL_checkstring (L, 1);

lua_pop(L,2);

if(var_value == NULL || var_name == NULL)
return -1;

chr = strchr((char *)var_name,0x2e);

if(chr == NULL) {
msr_log(msr, 8, "m.setvar: Must specify a collection using dot character - ie m.setvar(tx.myvar,mydata)");
return -1;
return 0;
}

return msre_action_setvar_execute(msr,msr->msc_rule_mptmp,rule,(char *)var_name,(char *)var_value);
msre_action_setvar_execute(msr,msr->msc_rule_mptmp,rule,(char *)var_name,(char *)var_value);

Check failure on line 397 in apache2/msc_lua.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

cast from 'const char *' to 'char *' drops const qualifier

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaGZ2Uqenp9-ELYlTC8I&open=AaGZ2Uqenp9-ELYlTC8I&pullRequest=3736

Check failure on line 397 in apache2/msc_lua.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

cast from 'const char *' to 'char *' drops const qualifier

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaGZ2Uqenp9-ELYlTC8J&open=AaGZ2Uqenp9-ELYlTC8J&pullRequest=3736
return 0;
}

static const struct luaL_Reg mylib[] = {
Expand Down
57 changes: 57 additions & 0 deletions tests/regression/rule/00-script.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,60 @@
GET => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
),
},
{
type => "rule",
comment => "SecRuleScript m.setvar without a collection returns nil",
conf => qq(
SecRuleEngine On
SecDebugLog $ENV{DEBUG_LOG}
SecDebugLogLevel 9
SecRuleScript "setvar-error-nodot.lua" "phase:2,pass"
),
match_log => {
debug => [ qr/m.setvar: Must specify a collection using dot character.*m.setvar\(\) without a collection returned: nil/s, 1 ],
},
match_response => {
status => qr/^200$/,
},
request => new HTTP::Request(
GET => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
),
},
{
type => "rule",
comment => "SecRuleScript m.setvar with one argument returns nil",
conf => qq(
SecRuleEngine On
SecDebugLog $ENV{DEBUG_LOG}
SecDebugLogLevel 9
SecRuleScript "setvar-error-onearg.lua" "phase:2,pass"
),
match_log => {
debug => [ qr/m.setvar: Failed m.setvar function must have 2 arguments.*m.setvar\(\) with one argument returned: nil/s, 1 ],
},
match_response => {
status => qr/^200$/,
},
request => new HTTP::Request(
GET => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
),
},
{
type => "rule",
comment => "SecRuleScript m.setvar sets a TX variable and returns nil",
conf => qq(
SecRuleEngine On
SecDebugLog $ENV{DEBUG_LOG}
SecDebugLogLevel 9
SecRuleScript "setvar-tx.lua" "phase:2,pass"
),
match_log => {
debug => [ qr/m.setvar\(\) with TX returned: nil.*tx.a is now: x/s, 1 ],
},
match_response => {
status => qr/^200$/,
},
request => new HTTP::Request(
GET => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
),
},
5 changes: 5 additions & 0 deletions tests/regression/server_root/conf/setvar-error-nodot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function main()
local r = m.setvar("novar", "x");
m.log(4, "m.setvar() without a collection returned: " .. tostring(r));
return nil;
end
5 changes: 5 additions & 0 deletions tests/regression/server_root/conf/setvar-error-onearg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function main()
local r = m.setvar("tx.a");
m.log(4, "m.setvar() with one argument returned: " .. tostring(r));
return nil;
end
6 changes: 6 additions & 0 deletions tests/regression/server_root/conf/setvar-tx.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function main()
local r = m.setvar("tx.a", "x");
m.log(4, "m.setvar() with TX returned: " .. tostring(r));
m.log(4, "tx.a is now: " .. tostring(m.getvar("tx.a")));
return nil;
end
Loading