array(), 'resolved' => array(), 'persistent' => array(), 'escalated' => array(), ); $token = Lock::acquire( 900 ); if ( false === $token ) { EventLog::record( 'scan_locked', array( 'scan_type' => $type ) ); return array( 'scan_id' => 0, 'status' => 'locked', 'diff' => $empty, 'error' => 'Another scan is already running.', ); } $scan_id = 0; $t0 = microtime( true ); try { self::extend_time_limit(); $scan_id = ScanRepository::start( $type, self::feed_version(), $scheduled_for ); $run = self::run_checks( Registry::checks(), new Context(), self::budget_seconds() ); $previous = array(); $prev_scan = ScanRepository::latest_completed( $scan_id ); if ( $prev_scan ) { $previous = self::exclude_unreliable( ScanRepository::findings( (int) $prev_scan['id'] ), $run['failed_ids'] ); } $current = self::exclude_unreliable( $run['findings'], $run['failed_ids'] ); $diff = ScanRepository::diff( $previous, $current ); $status = $run['partial'] ? 'partial' : 'completed'; ScanRepository::complete( $scan_id, $run['findings'], $run['states'], $status, $run['summary'] ); // complete() only has second resolution; store the precise duration. global $wpdb; $wpdb->update( Schema::table( 'scans' ), array( 'duration_ms' => max( 0, (int) round( ( microtime( true ) - $t0 ) * 1000 ) ) ), array( 'id' => $scan_id ), array( '%d' ), array( '%d' ) ); EventLog::record( 'scan_completed', array( 'scan_id' => $scan_id, 'scan_type' => $type, 'status' => $status, 'findings' => count( $run['findings'] ), ) ); do_action( 'gsfp_scan_completed', $scan_id, $diff, $type ); return array( 'scan_id' => $scan_id, 'status' => $status, 'diff' => $diff, 'error' => $run['summary'], ); } catch ( \Throwable $e ) { $error = 'Scan failed: ' . get_class( $e ); if ( $scan_id > 0 ) { ScanRepository::fail( $scan_id, $error ); } EventLog::record( 'scan_failed', array( 'scan_id' => $scan_id, 'scan_type' => $type, 'error' => $error, ) ); do_action( 'gsfp_scan_failed', $scan_id, $error, $type ); return array( 'scan_id' => $scan_id, 'status' => 'failed', 'diff' => $empty, 'error' => $error, ); } finally { Lock::release( $token ); } } /** Version of the advisory data in use (live feed when newer, otherwise bundled). */ public static function feed_version() { $data = ( new Context() )->advisories(); return isset( $data['feed_version'] ) ? (string) $data['feed_version'] : 'unknown'; } /** * Pure-ish orchestration (no persistence) so it can be unit-tested with fixture Contexts. * * @param \GSFP\Checks\CheckInterface[] $checks * @param int $budget Seconds. * @return array{findings:Finding[],states:array,partial:bool,failed_ids:string[],summary:string} */ public static function run_checks( array $checks, Context $context, $budget ) { $started = microtime( true ); $findings = array(); $states = array(); $failed = array(); $errors = array(); foreach ( $checks as $check ) { $id = (string) $check->id(); if ( ( microtime( true ) - $started ) > $budget ) { $states[ $id ] = 'timeout'; $failed[] = $id; $errors[] = $id . ': skipped (time budget exceeded)'; continue; } try { foreach ( $check->run( $context ) as $finding ) { if ( $finding instanceof Finding ) { $findings[] = $finding; } } $states[ $id ] = 'ok'; } catch ( \Throwable $e ) { $states[ $id ] = 'error'; $failed[] = $id; $errors[] = $id . ': ' . get_class( $e ); // Never include the exception message: it may carry paths or secrets. $findings[] = new Finding( array( 'rule_id' => $id, 'title' => 'Check could not complete: ' . $check->title(), 'category' => (string) $check->category(), 'subject' => 'check-error', 'severity' => 'info', 'status' => 'error', 'confidence' => 'high', 'evidence' => array( 'exception' => get_class( $e ) ), 'detected_at' => $context->now(), 'explanation' => 'This check failed unexpectedly, so its result is unknown. This is not a pass.', 'limitations' => 'Other checks were not affected.', ) ); } } return array( 'findings' => $findings, 'states' => $states, 'partial' => ! empty( $failed ), 'failed_ids' => $failed, 'summary' => implode( '; ', $errors ), ); } /** * Drop findings of failed checks so a failed check never shows as "resolved"/"new" in the diff. * * @param Finding[] $findings * @param string[] $failed_ids Check ids; a finding belongs to a check when its rule_id equals the id or starts with "id-". * @return Finding[] */ public static function exclude_unreliable( array $findings, array $failed_ids ) { if ( empty( $failed_ids ) ) { return $findings; } return array_values( array_filter( $findings, function ( Finding $f ) use ( $failed_ids ) { foreach ( $failed_ids as $id ) { $rule = (string) $f->get( 'rule_id' ); if ( $rule === $id || 0 === strpos( $rule, $id . '-' ) ) { return false; } } return true; } ) ); } private static function budget_seconds() { return max( 5, (int) apply_filters( 'gsfp_scan_time_budget', self::DEFAULT_BUDGET_SECONDS ) ); } private static function extend_time_limit() { // set_time_limit may be disabled by the host; never fatal on that. if ( function_exists( 'set_time_limit' ) && false === strpos( (string) ini_get( 'disable_functions' ), 'set_time_limit' ) ) { @set_time_limit( 120 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,Squiz.PHP.DiscouragedFunctions.Discouraged -- best effort; set_time_limit may be disabled by the host } } }