Back

71.42% Statements 10/14
100% Branches 12/12
66.66% Functions 2/3
71.42% Lines 10/14

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        4x     4x                                                     5x 3x   2x                   13x 5x   8x 6x   2x                                                        
import apiFetch from '@wordpress/api-fetch';
import { __, sprintf } from '@wordpress/i18n';
import type { BlockedRequest } from './types';
 
export const FIREWALL_PATH = '/jetpack/v4/protect-dashboard/firewall';
 
/** Matches `Automattic\\Jetpack\\Waf\\Waf_Self_Check::RULE_ID`. */
export const SELF_CHECK_RULE_ID = -2;
 
export type TestOutcome = 'blocked' | 'silent' | 'not-blocked' | 'off';
 
export type TestResult = {
	outcome: TestOutcome;
	/** The one-time URL the browser requested. */
	url: string;
	status: number;
	statusText: string;
	/** The `X-JetpackWAF-Blocked` header, which the firewall sends whenever it blocks. */
	wafHeader: string | null;
};
 
/**
 * What the test request's response says about the firewall.
 *
 * @param status    - The response status.
 * @param wafHeader - The `X-JetpackWAF-Blocked` header.
 * @param active    - Whether the firewall is on.
 * @return The outcome; a 403 without the header came from something else, such as the host.
 */
export function getTestOutcome(
	status: number,
	wafHeader: string | null,
	active: boolean
): TestOutcome {
	if ( ! wafHeader ) {
		return active ? 'not-blocked' : 'off';
	}
	return status === 403 ? 'blocked' : 'silent';
}
 
/**
 * A blocked request's description, for the recent blocks list.
 *
 * @param block - The blocked request.
 * @return The label.
 */
export function getBlockLabel( block: Pick< BlockedRequest, 'ruleId' | 'reason' > ): string {
	if ( block.reason === 'firewall test' ) {
		return __( 'Firewall test', 'jetpack-protect-pkg' );
	}
	if ( block.reason === 'ip block list' ) {
		return __( 'Blocked IP address', 'jetpack-protect-pkg' );
	}
	return (
		block.reason ||
		/* translators: %d is a firewall rule's ID. */
		sprintf( __( 'Rule %d', 'jetpack-protect-pkg' ), block.ruleId )
	);
}
 
/**
 * Request the one-time URL the firewall always blocks, the way a logged-out visitor would.
 *
 * @param active - Whether the firewall is on.
 * @return What happened.
 */
export async function runFirewallTest( active: boolean ): Promise< TestResult > {
	const { url } = await apiFetch< { url: string } >( {
		path: `${ FIREWALL_PATH }/test`,
		method: 'POST',
	} );
	const response = await fetch( url, { credentials: 'omit', cache: 'no-store' } );
	const wafHeader = response.headers.get( 'X-JetpackWAF-Blocked' );
	return {
		outcome: getTestOutcome( response.status, wafHeader, active ),
		url,
		status: response.status,
		statusText: response.statusText,
		wafHeader,
	};
}