30, 'high' => 12, 'medium' => 5, 'low' => 1); } public static function confidence_factor($confidence) { $factors = array('confirmed' => 1.0, 'likely' => 0.75, 'unverified' => 0.5); return isset($factors[$confidence]) ? $factors[$confidence] : 1.0; } /** * Score from a list of open findings (objects or arrays with check_id, severity, confidence) */ public static function from_findings($findings) { $weights = self::weights(); $per_rule = array(); $has_critical = false; $has_high = false; foreach ($findings as $finding) { $finding = (object) $finding; // Informational findings explain context; they never lower the score if (!isset($weights[$finding->severity]) || (isset($finding->kind) && $finding->kind === 'informational')) { continue; } $deduction = $weights[$finding->severity] * self::confidence_factor($finding->confidence); if (!isset($per_rule[$finding->check_id]) || $per_rule[$finding->check_id] < $deduction) { $per_rule[$finding->check_id] = $deduction; } $has_critical = $has_critical || $finding->severity === 'critical'; $has_high = $has_high || $finding->severity === 'high'; } // Many small hardening items should not outweigh one real problem: low findings cost at most 10 $low = 0; $other = 0; foreach ($per_rule as $deduction) { if ($deduction <= $weights['low']) { $low += $deduction; } else { $other += $deduction; } } $score = (int) round(max(0, 100 - $other - min(10, $low))); if ($has_critical) { $score = min($score, 49); } elseif ($has_high) { $score = min($score, 79); } return $score; } /** * Current score from the open findings in the database */ public static function current() { global $wpdb; $rows = $wpdb->get_results('SELECT check_id, severity, confidence, kind FROM ' . AOWPCS_Plugin::table('findings') . " WHERE state = 'open'"); return self::from_findings((array) $rows); } public static function band($score) { if ($score === null) { return array('key' => 'none', 'label' => __('Not scanned yet', 'all-in-one-wp-content-security')); } if ($score >= 90) { return array('key' => 'good', 'label' => __('Good', 'all-in-one-wp-content-security')); } if ($score >= 70) { return array('key' => 'fair', 'label' => __('Fair', 'all-in-one-wp-content-security')); } if ($score >= 50) { return array('key' => 'poor', 'label' => __('Poor', 'all-in-one-wp-content-security')); } return array('key' => 'risk', 'label' => __('At risk', 'all-in-one-wp-content-security')); } /** * Share of planned rules that could actually be evaluated (pass or issue), in percent */ public static function coverage($rule_status) { $total = count($rule_status); if ($total === 0) { return 0; } $evaluated = 0; foreach ($rule_status as $status) { if ($status === 'pass' || $status === 'issue') { $evaluated++; } } return (int) round($evaluated * 100 / $total); } }