diff --git a/t/translate/03_clean_input.t b/t/translate/03_clean_input.t index 9e16a670..fdca909f 100644 --- a/t/translate/03_clean_input.t +++ b/t/translate/03_clean_input.t @@ -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"/ ], @@ -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"/ ], @@ -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"/ ], @@ -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"/ ], @@ -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, [], @@ -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, [], @@ -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"/ ], @@ -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, [ @@ -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, [ @@ -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"/ ], diff --git a/tools/Modsec2LRW.pm b/tools/Modsec2LRW.pm index 9d2b03fc..d2634541 100644 --- a/tools/Modsec2LRW.pm +++ b/tools/Modsec2LRW.pm @@ -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*#/; diff --git a/tools/modsec2lua-resty-waf.pl b/tools/modsec2lua-resty-waf.pl index b0a36e1e..a9c70886 100755 --- a/tools/modsec2lua-resty-waf.pl +++ b/tools/modsec2lua-resty-waf.pl @@ -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, @@ -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