80
80
my $input = { " pattern" => $json -> {pattern },
81
81
" input" => $attackString ,
82
82
};
83
- my $tmpFile = " /tmp/validate-vuln-$$ .json" ;
84
- &writeToFile(" file" => $tmpFile , " contents" => encode_json($input ));
83
+ my $tmpQueryFile = " /tmp/validate-vuln-$$ -queryFile.json" ;
84
+ my $tmpStdoutFile = " /tmp/validate-vuln-$$ -validator-stdout.json" ;
85
+ &writeToFile(" file" => $tmpQueryFile , " contents" => encode_json($input ));
85
86
86
87
# Invoke the appropriate validator.
87
88
my $validator = $language2validator {$json -> {language }};
88
89
89
90
# Use KILL because Ruby blocks TERM during regex match (??).
90
- my ($rc , $deathSignal , $out ) = &cmd(" timeout --signal=KILL $json ->{timeLimit}s $validator $tmpFile " );
91
- unlink $tmpFile ;
91
+ my ($rc , $deathSignal , $out ) = &cmd(" timeout --signal=KILL $json ->{timeLimit}s $validator $tmpQueryFile > $tmpStdoutFile " );
92
92
# On timeout, rc is 124 if using TERM and 128+9 if using KILL
93
93
# The right-shift of 8 in &cmd turns 128+9 into 9
94
94
my $timedOut = ($rc eq 124 or $deathSignal eq 9) ? 1 : 0;
95
- &log (" rc $rc timedOut $ timedOut out \n $out " );
95
+ &log (" rc $rc deathSignal $deathSignal timedOut $timedOut " );
96
96
97
97
# Append appropriate values to $result
98
98
if ($timedOut ) {
105
105
106
106
# If it didn't time out, we should have valid JSON output.
107
107
# Was the regex valid?
108
- my $validatorRes = decode_json($out );
108
+ my $content = &slurpFile($tmpStdoutFile );
109
+ my $validatorRes = decode_json($content );
109
110
$result -> {validPattern } = $validatorRes -> {validPattern };
110
111
}
112
+
113
+ unlink $tmpQueryFile ;
114
+ unlink $tmpStdoutFile ;
111
115
}
112
116
113
117
print STDOUT encode_json($result ) . " \n " ;
114
118
exit 0;
115
119
116
120
# #####################
117
121
122
+ sub slurpFile {
123
+ my ($file ) = @_ ;
124
+ {
125
+ open F, $file or die " Can't read $file : $! " ;
126
+ local $/ ; # enable slurp mode, locally.
127
+ my $contents = <F>;
128
+ close F;
129
+ return $contents ;
130
+ }
131
+ }
132
+
118
133
# input: %args: keys: file contents
119
134
# output: $file
120
135
sub writeToFile {
@@ -133,8 +148,8 @@ sub cmd {
133
148
&log ($cmd );
134
149
my $out = ` $cmd ` ;
135
150
136
- my $deathSignal = $? & 127;
137
151
my $rc = $? >> 8;
152
+ my $deathSignal = $rc & 127;
138
153
139
154
return ($rc , $deathSignal , $out );
140
155
}
0 commit comments