Skip to content

Commit

Permalink
clean_input takes an array of strings instead of a file handle
Browse files Browse the repository at this point in the history
Make clean_input more portable by changing its input to an array of
chomp'd strings, letting the caller choose where its data comes from.
The included CLI script's behavior is unchanged; input is still
read via STDIN.
  • Loading branch information
p0pr0ck5 committed Aug 28, 2016
1 parent 0af7a5f commit c65f52f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
52 changes: 20 additions & 32 deletions t/translate/03_clean_input.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ SecRule \
/;

{
open my $stdin, '<', \ $basic;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($basic);
my @out = clean_input(@in);
is_deeply(
\@out,
[ q/SecRule ARGS "foo" "id:12345,pass"/ ],
Expand All @@ -38,9 +37,8 @@ SecRule \
}

{
open my $stdin, '<', \ $trim_left;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($trim_left);
my @out = clean_input(@in);
is_deeply(
\@out,
[ q/SecRule ARGS "foo" "id:12345,pass"/ ],
Expand All @@ -49,9 +47,8 @@ SecRule \
}

{
open my $stdin, '<', \ $trim_right;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($trim_right);
my @out = clean_input(@in);
is_deeply(
\@out,
[ q/SecRule ARGS "foo" "id:12345,pass"/ ],
Expand All @@ -60,9 +57,8 @@ SecRule \
}

{
open my $stdin, '<', \ $trim_both;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($trim_both);
my @out = clean_input(@in);
is_deeply(
\@out,
[ q/SecRule ARGS "foo" "id:12345,pass"/ ],
Expand All @@ -71,9 +67,8 @@ SecRule \
}

{
open my $stdin, '<', \ $ignore_comment;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($ignore_comment);
my @out = clean_input(@in);
is_deeply(
\@out,
[],
Expand All @@ -82,9 +77,8 @@ SecRule \
}

{
open my $stdin, '<', \ $invalid_directive;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($invalid_directive);
my @out = clean_input(@in);
is_deeply(
\@out,
[],
Expand All @@ -93,9 +87,8 @@ SecRule \
}

{
open my $stdin, '<', \ $multi_line;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = (split /\n/, $multi_line);
my @out = clean_input(@in);
is_deeply(
\@out,
[ q/SecRule ARGS "foo" "id:12345,pass"/ ],
Expand All @@ -104,10 +97,8 @@ SecRule \
}

{
my $data = "$basic\n$multi_line\n";
open my $stdin, '<', \ $data;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($basic, split /\n/, $multi_line);
my @out = clean_input(@in);
is_deeply(
\@out,
[
Expand All @@ -119,10 +110,8 @@ SecRule \
}

{
my $data = "$basic\n$comment\n$multi_line";
open my $stdin, '<', \ $data;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = ($basic, $comment, split /\n/, $multi_line);
my @out = clean_input(@in);
is_deeply(
\@out,
[
Expand All @@ -134,9 +123,8 @@ SecRule \
}

{
open my $stdin, '<', \ $multi_line_action;
local *STDIN = $stdin;
my @out = clean_input(*STDIN);
my @in = (split /\n/, $multi_line_action);
my @out = clean_input(@in);
is_deeply(
\@out,
[ q/SecRule ARGS "foo" "id:12345, phase:1, block, setvar:tx.foo=bar, expirevar:tx.foo=60"/ ],
Expand Down
7 changes: 3 additions & 4 deletions tools/Modsec2LRW.pm
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,13 @@ sub valid_line {
}

sub clean_input {
my ($fh) = @_;
my (@input) = @_;

my (@lines, @line_buf);

while (my $line = <$fh>) {
chomp $line;

for my $line (@input) {
# ignore comments and blank lines
next if ! $line;
next if $line =~ m/^\s*$/;
next if $line =~ m/^\s*#/;

Expand Down
9 changes: 7 additions & 2 deletions tools/modsec2lua-resty-waf.pl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sub usage {


sub main {
my ($path, $quiet, $silent, $pretty, $force);
my ($path, $quiet, $silent, $pretty, $force, @input);

GetOptions(
'q|quiet' => \$quiet,
Expand All @@ -41,9 +41,14 @@ sub main {
# silent implies quiet
$quiet = 1 if $silent;

while (<>) {
chomp;
push @input, $_;
}

# ModSecurity ruleset parsing
# clean the input and build an array of tokens
my @parsed_lines = map { parse_tokens(tokenize($_)) } clean_input(*STDIN);
my @parsed_lines = map { parse_tokens(tokenize($_)) } clean_input(@input);

# ModSecurity knows where it lives in a chain
# via pointer arithmetic and internal state handling
Expand Down

0 comments on commit c65f52f

Please sign in to comment.