Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | 2x 2x 6x 2x 1x 2x 2x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 4x | import apiFetch from '@wordpress/api-fetch';
import { dispatch } from '@wordpress/data';
import { useCallback, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Icon, check, closeSmall, shield } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { Button, Text } from '@wordpress/ui';
import { CardRow, ProtectCard, Stat } from '../../components/card';
import TabLink from '../../components/tab-link';
import isModuleActive from '../../data/is-module-active';
import { FIREWALL_PATH, SELF_CHECK_RULE_ID, runFirewallTest } from './firewall-test';
import RecentBlocks from './recent-blocks';
import TestDetails from './test-details';
import type { TestOutcome, TestResult } from './firewall-test';
import type { BlockedRequest, FirewallContext, FirewallState } from './types';
import './style.scss';
const UNAVAILABLE: FirewallState = {
available: false,
active: false,
blockedCount: null,
recentBlocks: [],
hasScan: false,
currentIp: '',
sharesData: false,
};
// One id for the test's snackbars, so the result replaces "Testing the firewall…".
const NOTICE_ID = 'jetpack-protect-firewall-test';
type NoticeStatus = 'success' | 'info' | 'warning' | 'error';
// Icon SVGs default to black; currentColor picks up the snackbar's light text.
const snackbarIcon = ( icon: JSX.Element ) => <Icon icon={ icon } fill="currentColor" />;
const OUTCOMES: Record<
TestOutcome | 'error',
{ status: NoticeStatus; message: () => string; icon?: JSX.Element }
> = {
blocked: {
status: 'success',
icon: snackbarIcon( check ),
message: () =>
__( 'The firewall blocked the test request. It’s working.', 'jetpack-protect-pkg' ),
},
silent: {
status: 'warning',
message: () =>
__(
'The firewall caught the test request but let it through, because it’s only logging requests.',
'jetpack-protect-pkg'
),
},
'not-blocked': {
status: 'error',
icon: snackbarIcon( closeSmall ),
message: () =>
__(
'The test request wasn’t blocked. The firewall may not be running on this site.',
'jetpack-protect-pkg'
),
},
off: {
status: 'info',
message: () =>
__(
'The test request got through, because the firewall is off. Turn it on and test again to see it blocked.',
'jetpack-protect-pkg'
),
},
error: {
status: 'error',
icon: snackbarIcon( closeSmall ),
message: () => __( 'The firewall test couldn’t run. Try again.', 'jetpack-protect-pkg' ),
},
};
/**
* Show the firewall test's progress or result as a snackbar.
*
* @param status - The notice's kind.
* @param content - The message.
* @param learnMore - Opens the test's steps; the result has it, progress doesn't.
* @param icon - A check when the firewall blocked the test, an X when it failed.
*/
function notify(
status: NoticeStatus,
content: string,
learnMore?: () => void,
icon?: JSX.Element
) {
dispatch( noticesStore ).createNotice( status, content, {
type: 'snackbar',
id: NOTICE_ID,
// @ts-expect-error -- Typed as a string, but the snackbar renders any element.
icon,
// Results stay until dismissed, so there's time to read them and open Learn more.
explicitDismiss: status !== 'info' || !! learnMore,
actions: learnMore
? [ { label: __( 'Learn more', 'jetpack-protect-pkg' ), onClick: learnMore } ]
: [],
} );
}
const formatCount = ( count: number ) =>
new Intl.NumberFormat( document.documentElement.lang || undefined ).format( count );
/**
* The firewall's Overview card: blocked requests, and a test that shows the firewall working.
*
* @param props - The dashboard context.
* @param props.state - The firewall's state from page load.
* @param props.settings - Jetpack settings, for the live on/off state.
* @param props.openTab - Switches dashboard tabs, for the link to Settings.
* @return The card.
*/
export default function FirewallOverviewCard( { state, settings, openTab }: FirewallContext ) {
const firewall = state ?? UNAVAILABLE;
const title = __( 'Firewall', 'jetpack-protect-pkg' );
const [ blocks, setBlocks ] = useState( {
blockedCount: firewall.blockedCount,
recentBlocks: firewall.recentBlocks ?? [],
} );
const [ isTesting, setIsTesting ] = useState( false );
const [ result, setResult ] = useState< TestResult | null >( null );
const [ recorded, setRecorded ] = useState( false );
const [ detailsOpen, setDetailsOpen ] = useState( false );
const active = isModuleActive( settings.settings, 'waf', firewall.active );
const sharesData = Boolean( settings.settings?.jetpack_waf_share_data ?? firewall.sharesData );
const runTest = useCallback( async () => {
setIsTesting( true );
notify( 'info', __( 'Testing the firewall…', 'jetpack-protect-pkg' ) );
try {
const lastId = blocks.recentBlocks[ 0 ]?.id ?? 0;
const testResult = await runFirewallTest( active );
const fresh = await apiFetch< {
blockedCount: number | null;
recentBlocks: BlockedRequest[];
} >( { path: `${ FIREWALL_PATH }/blocks` } );
setBlocks( fresh );
setResult( testResult );
setRecorded(
fresh.recentBlocks.some( block => block.id > lastId && block.ruleId === SELF_CHECK_RULE_ID )
);
const { status, message, icon } = OUTCOMES[ testResult.outcome ];
notify( status, message(), () => setDetailsOpen( true ), icon );
} catch {
notify( OUTCOMES.error.status, OUTCOMES.error.message(), undefined, OUTCOMES.error.icon );
} finally {
setIsTesting( false );
}
}, [ active, blocks.recentBlocks ] );
if ( ! firewall.available ) {
return (
<ProtectCard
icon={ shield }
title={ title }
status={ { label: __( 'Unavailable', 'jetpack-protect-pkg' ), intent: 'none' } }
>
<CardRow>
<Text variant="body-md">
{ __( 'This feature isn’t available on your site.', 'jetpack-protect-pkg' ) }
</Text>
</CardRow>
</ProtectCard>
);
}
return (
<ProtectCard
icon={ shield }
title={ title }
actions={
<Button variant="outline" size="compact" onClick={ runTest } loading={ isTesting }>
{ __( 'Test firewall', 'jetpack-protect-pkg' ) }
</Button>
}
>
<CardRow>
{ active ? (
<Stat
label={ __( 'All-time blocked requests', 'jetpack-protect-pkg' ) }
value={ blocks.blockedCount === null ? '—' : formatCount( blocks.blockedCount ) }
/>
) : (
<Text variant="body-md">
{ __(
'Turn on the firewall to block malicious requests before they reach your site.',
'jetpack-protect-pkg'
) }
</Text>
) }
</CardRow>
{ result && (
<TestDetails
result={ result }
recorded={ recorded }
open={ detailsOpen }
onOpenChange={ setDetailsOpen }
/>
) }
{ active && (
<CardRow>
<RecentBlocks blocks={ blocks.recentBlocks } canViewAll={ sharesData } />
</CardRow>
) }
<CardRow>
<TabLink tab="settings" onOpen={ openTab }>
{ active
? __( 'Configure Firewall', 'jetpack-protect-pkg' )
: __( 'Turn on in Settings', 'jetpack-protect-pkg' ) }
</TabLink>
</CardRow>
</ProtectCard>
);
}
|