Skip to content

Commit

Permalink
Protect: Integrate ThreatsDataViews
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Nov 14, 2024
1 parent 1d6045f commit a79cda8
Show file tree
Hide file tree
Showing 56 changed files with 229 additions and 3,533 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions projects/js-packages/scan/changelog/add-additional-scan-types
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added more types for working with scan results.
23 changes: 23 additions & 0 deletions projects/js-packages/scan/src/types/fixers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,26 @@ export type ThreatFixStatusSuccess = {
};

export type ThreatFixStatus = ThreatFixStatusError | ThreatFixStatusSuccess;

/**
* Fixers Status
*
* Overall status of all fixers.
*/
type FixersStatusBase = {
ok: boolean; // Discriminator for overall success
};

export type FixersStatusError = FixersStatusBase & {
ok: false;
error: string;
};

export type FixersStatusSuccess = FixersStatusBase & {
ok: true;
threats: {
[ key: number ]: ThreatFixStatus;
};
};

export type FixersStatus = FixersStatusSuccess | FixersStatusError;
1 change: 1 addition & 0 deletions projects/js-packages/scan/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './fixers';
export * from './status';
export * from './threats';
33 changes: 33 additions & 0 deletions projects/js-packages/scan/src/types/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Threat } from '..';

export type ScanStatus = {
/** The current status of the scanner. */
status: 'unavailable' | 'provisioning' | 'idle' | 'scanning' | 'scheduled';

/** The IDs of fixable threats. */
fixableThreatIds: number[];

/** The current scan progress, only available from the Scan API. */
currentProgress: number | null;

/** The data source for the scan status. */
dataSource: 'protect_report' | 'scan_api';

/** Whether the site currently has extensions not checked in the latest scan. */
hasUncheckedItems: boolean;

/** The time the last scan was checked, in YYYY-MM-DD HH:MM:SS format. */
lastChecked: string | null;

/** Whether there was an error in the scan results. */
error: boolean | null;

/** The error code. */
errorCode: string | null;

/** The error message. */
errorMessage: string | null;

/** The detected threats. */
threats: Threat[];
};
5 changes: 5 additions & 0 deletions projects/plugins/protect/changelog/add-threats-data-views
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: minor
Type: changed

Added DataViews component for viewing scan results.

1 change: 1 addition & 0 deletions projects/plugins/protect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@automattic/jetpack-base-styles": "workspace:*",
"@automattic/jetpack-components": "workspace:*",
"@automattic/jetpack-connection": "workspace:*",
"@automattic/jetpack-scan": "workspace:*",
"@tanstack/react-query": "5.20.5",
"@tanstack/react-query-devtools": "5.20.5",
"@wordpress/api-fetch": "7.11.0",
Expand Down
30 changes: 3 additions & 27 deletions projects/plugins/protect/src/class-scan-history.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,43 +207,19 @@ public static function fetch_from_api() {
* Normalize API Data
* Formats the payload from the Scan API into an instance of History_Model.
*
* @phan-suppress PhanDeprecatedProperty -- Maintaining backwards compatibility.
*
* @param object $scan_data The data returned by the scan API.
* @return History_Model
*/
private static function normalize_api_data( $scan_data ) {
$history = new History_Model();
$history->num_threats = 0;
$history->num_core_threats = 0;
$history->num_plugins_threats = 0;
$history->num_themes_threats = 0;

$history = new History_Model();
$history->last_checked = $scan_data->last_checked;

if ( empty( $scan_data->threats ) || ! is_array( $scan_data->threats ) ) {
return $history;
}

foreach ( $scan_data->threats as $threat ) {
if ( isset( $threat->extension->type ) ) {
if ( 'plugin' === $threat->extension->type ) {
self::handle_extension_threats( $threat, $history, 'plugin' );
continue;
}

if ( 'theme' === $threat->extension->type ) {
self::handle_extension_threats( $threat, $history, 'theme' );
continue;
}
}

if ( 'Vulnerable.WP.Core' === $threat->signature ) {
self::handle_core_threats( $threat, $history );
continue;
}

self::handle_additional_threats( $threat, $history );
foreach ( $scan_data->threats as $source_threat ) {
$history->threats[] = new Threat_Model( $source_threat );
}

return $history;
Expand Down
7 changes: 3 additions & 4 deletions projects/plugins/protect/src/js/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type FixersStatus, type ScanStatus } from '@automattic/jetpack-scan';
import apiFetch from '@wordpress/api-fetch';
import camelize from 'camelize';
import { FixersStatus } from './types/fixers';
import { ScanStatus } from './types/scans';
import { WafStatus } from './types/waf';

const API = {
Expand Down Expand Up @@ -72,7 +71,7 @@ const API = {
path: `jetpack-protect/v1/fix-threats`,
method: 'POST',
data: { threat_ids: threatIds },
} ),
} ).then( camelize ),

getFixersStatus: ( threatIds: number[] ): Promise< FixersStatus > => {
const path = threatIds.reduce( ( carryPath, threatId ) => {
Expand All @@ -82,7 +81,7 @@ const API = {
return apiFetch( {
path,
method: 'GET',
} );
} ).then( camelize );
},

ignoreThreat: ( threatId: number ) =>
Expand Down
12 changes: 4 additions & 8 deletions projects/plugins/protect/src/js/components/admin-page/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { useConnection } from '@automattic/jetpack-connection';
import { __, sprintf } from '@wordpress/i18n';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import useScanStatusQuery from '../../data/scan/use-scan-status-query';
import useNotices from '../../hooks/use-notices';
import useProtectData from '../../hooks/use-protect-data';
import useWafData from '../../hooks/use-waf-data';
import Notice from '../notice';
import Tabs, { Tab } from '../tabs';
Expand All @@ -19,11 +19,7 @@ const AdminPage = ( { children } ) => {
const { isRegistered } = useConnection();
const { isSeen: wafSeen } = useWafData();
const navigate = useNavigate();
const {
counts: {
current: { threats: numThreats },
},
} = useProtectData();
const { data: status } = useScanStatusQuery();

// Redirect to the setup page if the site is not registered.
useEffect( () => {
Expand All @@ -48,11 +44,11 @@ const AdminPage = ( { children } ) => {
link="/scan"
label={
<span className={ styles.tab }>
{ numThreats > 0
{ status.threats.length > 0
? sprintf(
// translators: %d is the number of threats found.
__( 'Scan (%d)', 'jetpack-protect' ),
numThreats
status.threats.length
)
: __( 'Scan', 'jetpack-protect' ) }
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ThreatFixHeader from '../threat-fix-header';
import UserConnectionGate from '../user-connection-gate';
import styles from './styles.module.scss';

const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
const FixThreatModal = ( { threat } ) => {
const { setModal } = useModal();
const { fixThreats, isLoading: isFixersLoading } = useFixers();

Expand All @@ -21,7 +21,7 @@ const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
const handleFixClick = () => {
return async event => {
event.preventDefault();
await fixThreats( [ id ] );
await fixThreats( [ threat.id ] );
setModal( { type: null } );
};
};
Expand All @@ -37,10 +37,7 @@ const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
</Text>

<div className={ styles.list }>
<ThreatFixHeader
threat={ { id, fixable, label, icon, severity } }
fixAllDialog={ false }
/>
<ThreatFixHeader threat={ threat } fixAllDialog={ false } />
</div>

<div className={ styles.footer }>
Expand Down

This file was deleted.

Loading

0 comments on commit a79cda8

Please sign in to comment.