Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Implement the .line filter
Browse files Browse the repository at this point in the history
Close #48
  • Loading branch information
jvoisin committed Oct 23, 2017
1 parent a50fe60 commit a8ab648
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Filters
- ``function(name)``: match on function ``name``
- ``function_r(regexp)``: the function matching the ``regexp``
- ``hash(sha256)``: match on the file's `sha256 <https://en.wikipedia.org/wiki/SHA-2>`_ sum
- ``line(line_number)``: match on the file's line.
- ``param(name)``: match on the function's parameter ``name``
- ``param_r(regexp)``: match on the function's parameter ``regexp``
- ``param_type(type)``: match on the function's parameter ``type``
Expand Down
2 changes: 2 additions & 0 deletions src/sp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef struct {
pcre *r_param;
sp_php_type param_type;
int pos;
unsigned int line;

char *ret;
pcre *r_ret;
Expand Down Expand Up @@ -185,6 +186,7 @@ typedef struct {
#define SP_TOKEN_VALUE ".value("
#define SP_TOKEN_VALUE_REGEXP ".value_r("
#define SP_TOKEN_VALUE_ARG_POS ".pos("
#define SP_TOKEN_LINE_NUMBER ".line("

// cookies encryption
#define SP_TOKEN_NAME ".cookie("
Expand Down
13 changes: 13 additions & 0 deletions src/sp_config_keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ int parse_disabled_functions(char *line) {
int ret = 0;
bool enable = true, disable = false;
char *pos = NULL;
char *line_number = NULL;
sp_disabled_function *df = pecalloc(sizeof(*df), 1, 1);
df->pos = -1;

Expand Down Expand Up @@ -172,6 +173,7 @@ int parse_disabled_functions(char *line) {
{parse_php_type, SP_TOKEN_RET_TYPE, &(df->ret_type)},
{parse_str, SP_TOKEN_LOCAL_VAR, &(df->var)},
{parse_str, SP_TOKEN_VALUE_ARG_POS, &(pos)},
{parse_str, SP_TOKEN_LINE_NUMBER, &(line_number)},
{0}};

ret = parse_keywords(sp_config_funcs_disabled_functions, line);
Expand Down Expand Up @@ -252,6 +254,17 @@ int parse_disabled_functions(char *line) {
}
}

if (line_number) {
errno = 0;
char *endptr;
df->line = strtoul(line_number, &endptr, 10);
if (errno != 0 || endptr == line_number) {
sp_log_err("config", "Failed to parse arg '%s' of `line` on line %zu.",
line_number, sp_line_no);
return -1;
}
}

if (df->function) {
df->functions_list = parse_functions_list(df->function);
}
Expand Down
6 changes: 6 additions & 0 deletions src/sp_disabled_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ bool should_disable(zend_execute_data* execute_data) {
}
}

if (config_node->line) {
if (config_node->line != zend_get_executed_lineno()) {
goto next;
}
}

if (client_ip && config_node->cidr &&
(false == cidr_match(client_ip, config_node->cidr))) {
goto next;
Expand Down
1 change: 1 addition & 0 deletions src/tests/config/disabled_functions_broken_line.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sp.disable_function.function("system").line("qwe").drop();
1 change: 1 addition & 0 deletions src/tests/config/disabled_functions_line.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sp.disable_function.function("system").line("3").drop();
15 changes: 15 additions & 0 deletions src/tests/disabled_functions_param_broken_line.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Disable functions - match on a specific line - broken configuration
--SKIPIF--
<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
--INI--
sp.configuration_file={PWD}/config/disabled_functions_broken_line.ini
--FILE--
<?php
system("echo 1337");
system("echo 1338");
?>
--EXPECTF--
[snuffleupagus][0.0.0.0][config][error] Failed to parse arg 'qwe' of `line` on line 1.
1337
1338
14 changes: 14 additions & 0 deletions src/tests/disabled_functions_param_line.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Disable functions - match on a specific line
--SKIPIF--
<?php if (!extension_loaded("snuffleupagus")) die "skip"; ?>
--INI--
sp.configuration_file={PWD}/config/disabled_functions_line.ini
--FILE--
<?php
system("echo 1337");
system("id");
?>
--EXPECTF--
1337
[snuffleupagus][0.0.0.0][disabled_function][drop] The call to the function 'system' in %a/disabled_functions_param_line.php:3 has been disabled.

0 comments on commit a8ab648

Please sign in to comment.