Skip to content

Commit

Permalink
Update threats data handling in Protect
Browse files Browse the repository at this point in the history
changelog

Update My Jetpack for latest compatibility with protect-status package

changelog
  • Loading branch information
nateweller committed Oct 21, 2024
1 parent 533f197 commit 2af226a
Show file tree
Hide file tree
Showing 43 changed files with 406 additions and 3,565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,14 @@ export const ScanAndThreatStatus = () => {
const {
protect: { scanData },
} = getMyJetpackWindowInitialState();
const { plugins, themes, num_threats: numThreats = 0 } = scanData || {};
const numThreats = scanData.threats.length;

const criticalScanThreatCount = useMemo( () => {
const { core, database, files, num_plugins_threats, num_themes_threats } = scanData || {};
const pluginsThreats = num_plugins_threats
? plugins.reduce( ( accum, plugin ) => accum.concat( plugin.threats ), [] )
: [];
const themesThreats = num_themes_threats
? themes.reduce( ( accum, theme ) => accum.concat( theme.threats ), [] )
: [];
const allThreats = [
...pluginsThreats,
...themesThreats,
...( core?.threats ?? [] ),
...database,
...files,
];
return allThreats.reduce(
return scanData.threats.reduce(
( accum, threat ) => ( threat.severity >= 5 ? ( accum += 1 ) : accum ),
0
);
}, [ plugins, themes, scanData ] );
}, [ scanData.threats ] );

if ( isPluginActive && isSiteConnected ) {
if ( hasProtectPaidPlan ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ export const useLastScanText = () => {
themes,
protect: { scanData },
} = getMyJetpackWindowInitialState();
const {
plugins: fromScanPlugins,
themes: fromScanThemes,
last_checked: lastScanTime = null,
} = scanData || {};
const { last_checked: lastScanTime = null } = scanData || {};

const pluginsCount = fromScanPlugins.length || Object.keys( plugins ).length;
const themesCount = fromScanThemes.length || Object.keys( themes ).length;
const pluginsCount = Object.keys( plugins ).length;
const themesCount = Object.keys( themes ).length;

const timeSinceLastScan = lastScanTime ? timeSince( Date.parse( lastScanTime ) ) : false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ export function useProtectTooltipCopy(): TooltipContent {
themes,
protect: { scanData, wafConfig: wafData },
} = getMyJetpackWindowInitialState();
const {
plugins: fromScanPlugins,
themes: fromScanThemes,
num_threats: numThreats = 0,
} = scanData || {};
const numThreats = scanData.threats.length;
const {
jetpack_waf_automatic_rules: isAutoFirewallEnabled,
blocked_logins: blockedLoginsCount,
brute_force_protection: hasBruteForceProtection,
} = wafData || {};

const pluginsCount = fromScanPlugins.length || Object.keys( plugins ).length;
const themesCount = fromScanThemes.length || Object.keys( themes ).length;
const pluginsCount = Object.keys( plugins ).length;
const themesCount = Object.keys( themes ).length;

const settingsLink = useMemo( () => {
if ( isProtectPluginActive ) {
Expand Down
5 changes: 5 additions & 0 deletions projects/packages/my-jetpack/changelog/protect-status-compat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Package compatibility updates, no functional changes.


24 changes: 7 additions & 17 deletions projects/packages/my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type ThreatItem = {
fixed_in: string;
description: string | null;
source: string | null;
extension: {
slug: string;
name: string;
version: string;
type: 'plugin' | 'theme' | 'core';
};
// Scan API properties (paid plan)
context: string | null;
filename: string | null;
Expand All @@ -58,15 +64,6 @@ type ThreatItem = {
status: number | null;
};

type ScanItem = {
checked: boolean;
name: string;
slug: string;
threats: ThreatItem[];
type: string;
version: string;
};

interface Window {
myJetpackInitialState?: {
siteSuffix: string;
Expand Down Expand Up @@ -211,22 +208,15 @@ interface Window {
};
protect: {
scanData: {
core: ScanItem;
threats: ThreatItem[];
current_progress?: string;
data_source: string;
database: string[];
error: boolean;
error_code?: string;
error_message?: string;
files: string[];
has_unchecked_items: boolean;
last_checked: string;
num_plugins_threats: number;
num_themes_threats: number;
num_threats: number;
plugins: ScanItem[];
status: string;
themes: ScanItem[];
};
wafConfig: {
automatic_rules_available: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: major
Type: changed

Changed the formatting of threat data.
37 changes: 0 additions & 37 deletions projects/packages/protect-models/src/class-extension-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ class Extension_Model {
*/
public $version;

/**
* A collection of threats related to this version of the extension.
*
* @var array<Threat_Model>
*/
public $threats = array();

/**
* Whether the extension has been checked for threats.
*
Expand Down Expand Up @@ -77,34 +70,4 @@ public function __construct( $extension = array() ) {
}
}
}

/**
* Set Threats
*
* @param array<Threat_Model|array|object> $threats An array of threat data to add to the extension.
*/
public function set_threats( $threats ) {
if ( ! is_array( $threats ) ) {
$this->threats = array();
return;
}

// convert each provided threat item into an instance of Threat_Model
$threats = array_map(
function ( $threat ) {
if ( is_a( $threat, 'Threat_Model' ) ) {
return $threat;
}

if ( is_object( $threat ) ) {
$threat = (array) $threat;
}

return new Threat_Model( $threat );
},
$threats
);

$this->threats = $threats;
}
}
60 changes: 2 additions & 58 deletions projects/packages/protect-models/src/class-history-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,12 @@ class History_Model {
*/
public $last_checked;

/**
* The number of threats.
*
* @var int
*/
public $num_threats;

/**
* The number of core threats.
*
* @var int
*/
public $num_core_threats;

/**
* The number of plugin threats.
*
* @var int
*/
public $num_plugins_threats;

/**
* The number of theme threats.
*
* @var int
*/
public $num_themes_threats;

/**
* WordPress core.
*
* @var array<Extension_Model>
*/
public $core = array();

/**
* Status themes.
*
* @var array<Extension_Model>
*/
public $themes = array();

/**
* Status plugins.
*
* @var array<Extension_Model>
*/
public $plugins = array();

/**
* File threats.
*
* @var array<Extension_Model>
*/
public $files = array();

/**
* Database threats.
*
* @var array<Extension_Model>
* @var array<Threat_Model>
*/
public $database = array();
public $threats = array();

/**
* Whether there was an error loading the history.
Expand Down
58 changes: 3 additions & 55 deletions projects/packages/protect-models/src/class-status-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ class Status_Model {
*/
public $last_checked;

/**
* The number of threats.
*
* @var int
*/
public $num_threats;

/**
* The number of plugin threats.
*
* @var int
*/
public $num_plugins_threats;

/**
* The number of theme threats.
*
* @var int
*/
public $num_themes_threats;

/**
* The current report status.
*
Expand All @@ -61,39 +40,11 @@ class Status_Model {
public $fixable_threat_ids = array();

/**
* WordPress core status.
*
* @var object
*/
public $core;

/**
* Status themes.
*
* @var array<Extension_Model>
*/
public $themes = array();

/**
* Status plugins.
*
* @var array<Extension_Model>
*/
public $plugins = array();

/**
* File threats.
*
* @var array<Extension_Model>
*/
public $files = array();

/**
* Database threats.
* Threats.
*
* @var array<Extension_Model>
* @var array<Threat_Model>
*/
public $database = array();
public $threats = array();

/**
* Whether the site includes items that have not been checked.
Expand Down Expand Up @@ -136,9 +87,6 @@ class Status_Model {
* @param array $status The status data to load into the class instance.
*/
public function __construct( $status = array() ) {
// set status defaults
$this->core = new \stdClass();

foreach ( $status as $property => $value ) {
if ( property_exists( $this, $property ) ) {
$this->$property = $value;
Expand Down
11 changes: 11 additions & 0 deletions projects/packages/protect-models/src/class-threat-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ class Threat_Model {
*/
public $source;

/**
* The threat's extension information.
*
* @var null|Extension_Model
*/
public $extension;

/**
* Threat Constructor
*
Expand All @@ -114,6 +121,10 @@ public function __construct( $threat ) {
}

foreach ( $threat as $property => $value ) {
if ( 'extension' === $property ) {
$this->extension = new Extension_Model( $value );
continue;
}
if ( property_exists( $this, $property ) ) {
$this->$property = $value;
}
Expand Down
Loading

0 comments on commit 2af226a

Please sign in to comment.