Skip to content

Commit

Permalink
Update threats data handling in Protect
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Oct 14, 2024
1 parent 8c94083 commit b655fed
Show file tree
Hide file tree
Showing 33 changed files with 297 additions and 3,139 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,20 @@
* @package automattic/jetpack-protect
*/
class Test_Extension_Model extends BaseTestCase {

/**
* Get a sample threat
*
* @param int|string $id The sample threat's unique identifier.
* @return array
*/
private static function get_sample_threat( $id = 0 ) {
return array(
'id' => "test-threat-$id",
'signature' => 'Test.Threat',
'title' => "Test Threat $id",
'description' => 'This is a test threat.',
);
}

/**
* Tests for extension model's __construct() method.
*/
public function test_extension_model_construct() {
$test_data = array(
'name' => 'Test Extension',
'slug' => 'test-extension',
$test_data = array(
'slug' => 'test-extension-1',
'name' => 'Test Extension 1',
'version' => '1.0.0',
'threats' => array(
self::get_sample_threat( 0 ),
self::get_sample_threat( 1 ),
self::get_sample_threat( 2 ),
),
'checked' => true,
'type' => 'plugins',
);

// Initialize multiple instances of Extension_Model to test varying initial params
$test_extensions = array(
new Extension_Model( $test_data ),
new Extension_Model( (object) $test_data ),
'type' => 'plugin',
);
$test_extension = new Extension_Model( $test_data );

foreach ( $test_extensions as $extension ) {
foreach ( $extension->threats as $loop_index => $threat ) {
// Validate the threat data is converted into Threat_Models
$this->assertSame( 'Automattic\Jetpack\Protect_Models\Threat_Model', get_class( $threat ) );

// Validate the threat data is set properly
foreach ( self::get_sample_threat( $loop_index ) as $key => $value ) {
$this->assertSame( $value, $threat->{ $key } );
}
}
foreach ( $test_data as $key => $value ) {
$this->assertSame( $value, $test_extension->$key );
}
}
}
71 changes: 50 additions & 21 deletions projects/packages/protect-models/tests/php/test-threat-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,63 @@ class Test_Threat_Model extends BaseTestCase {
* Tests for threat model's __construct() method.
*/
public function test_threat_model_construct() {
// Initialize multiple instances of Extension_Threat to test varying initial params
$test_data = array(
'id' => 'abc-123-abc-123',
'signature' => 'Test.Threat',
'title' => 'Test Threat',
'description' => 'This is a test threat.',
'first_detected' => '2022-01-01T00:00:00.000Z',
'fixed_in' => '1.0.1',
'severity' => 4,
'fixable' => (object) array(
'fixer' => 'update',
'target' => '1.0.1',
'extension_status' => 'active',
array(
'id' => 'test-threat-1',
'signature' => 'Test.Threat',
'title' => 'Test Threat 1',
'description' => 'This is a test threat.',
'extension' => array(
'slug' => 'test-extension-1',
'name' => 'Test Extension 1',
'version' => '1.0.0',
'type' => 'plugin',
),
),
array(
'id' => 'test-threat-2',
'signature' => 'Test.Threat',
'title' => 'Test Threat 2',
'description' => 'This is a test threat.',
'extension' => array(
'slug' => 'test-extension-2',
'name' => 'Test Extension 2',
'version' => '1.0.0',
'type' => 'theme',
),
),
array(
'id' => 'test-threat-3',
'signature' => 'Test.Threat',
'title' => 'Test Threat 3',
'description' => 'This is a test threat.',
),
'status' => 'current',
'filename' => '/srv/htdocs/wp-content/uploads/threat.jpg.php',
'context' => (object) array(),
);

// Initialize multiple instances of Threat_Model to test varying initial params
$test_threats = array(
new Threat_Model( $test_data ),
new Threat_Model( (object) $test_data ),
$test_threats = array_map(
function ( $threat_data ) {
return new Threat_Model( $threat_data );
},
$test_data
);

foreach ( $test_threats as $threat ) {
foreach ( $test_threats as $loop_index => $threat ) {
// Validate the threat data is normalized into model classes
$this->assertSame( 'Automattic\Jetpack\Protect_Models\Threat_Model', get_class( $threat ) );
if ( isset( $threat->extension ) ) {
$this->assertSame( 'Automattic\Jetpack\Protect_Models\Extension_Model', get_class( $threat->extension ) );
}

// Validate the threat data is set properly
foreach ( $test_data as $key => $value ) {
$this->assertSame( $value, $threat->{ $key } );
foreach ( $test_data[ $loop_index ] as $key => $value ) {
if ( 'extension' === $key ) {
foreach ( $value as $extension_key => $extension_value ) {
$this->assertSame( $extension_value, $threat->extension->$extension_key );
}
continue;
}
$this->assertSame( $value, $threat->$key );
}
}
}
Expand Down
Loading

0 comments on commit b655fed

Please sign in to comment.