Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Implement HTTP Accept request header to be randomized with probability #12

Merged
merged 1 commit into from
Jul 7, 2023
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
62 changes: 57 additions & 5 deletions cakefuzzer/phpfiles/AppInstrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ protected function _parseConfig($config) {

# Assign Accept header if not provided. TODO: Move to different part of the code.
if(empty($_SERVER['HTTP_ACCEPT'])) {
$accept = array('application/json',
'application/xml',
'text/csv',
'text/html',
'text/plain'
$accept = array('application/json' => 20,
'application/xml' => 20,
'text/csv' => 10,
'text/html' => 40,
'text/plain' => 10
);
$accept = $this->_prepareProbableArray($accept);
$_SERVER['HTTP_ACCEPT'] = $accept[array_rand($accept)];
}

Expand Down Expand Up @@ -320,4 +321,55 @@ private function __cake_fuzzer_prepare_path($config) {
$_SERVER["QUERY_STRING"] = $path;
return $path;
}

/**
* Prepare the array to include probabilities of choices.
* It's done by creating a flat array that contains more identical items
* of higher probability and fewer items of lower probability.
*
* @return array
*/
protected function _prepareProbableArray($array) {
// This should throw error instead of return empty array.
if(!$this->_verifyProbableArray($array)) return array();

// First calculate GCD, then divide all probabilities by the GCD
$gcd = max($array);
foreach($array as $k=>$v) $gcd = $this->_calculateGcd($gcd, $v);

$probable_array = array();
foreach($array as $k=>$v) {
$probable_array = array_merge($probable_array, array_fill(0, $v/$gcd, $k));
}
return $probable_array;
}

/**
* Verify if the array with probabilities is correctly formed.
* Check if probabilities are integer and sum of them is equal to 100
*
* @return bool
*/
protected function _verifyProbableArray($array) {
$sum = 0;
foreach($array as $k=>$v) {
if(!is_int($v)) return false;
$sum += $v;
}
return $sum === 100;
}

/**
* Calculate greatest common division of two numbers
*
* @return int
*/
protected function _calculateGcd($a, $b) {
while ($b<>0) {
$c = $a;
$a = $b;
$b = $c%$b;
}
return $a;
}
}
1 change: 1 addition & 0 deletions tools/iteration_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
GROUPS = {
"output.first_http_line": "Response Status",
"output.method": "Method",
"output._SERVER.HTTP_ACCEPT": "HTTP_ACCEPT",
}


Expand Down