← Back
Editing: Redact.php
<?php namespace WPForms\Logger; /** * Redact the submitted values of sensitive fields, such as Password, in debug output and logs. * * @since 2.0.5.3 */ class Redact { /** * Placeholder that replaces a redacted value. * * @since 2.0.5.3 */ private const PLACEHOLDER = '[redacted]'; /** * Deepest nesting level that is searched. * * @since 2.0.5.3 */ private const MAX_DEPTH = 32; /** * Fields of the forms read so far, keyed by form ID. * * One submission writes several records for the same form, and the form handler decodes it on every call. * * @since 2.0.5.3 * * @var array */ private static $form_fields = []; /** * Redact the submitted values of sensitive fields, which sit under generic keys. * * @since 2.0.5.3 * * @param mixed $data Data to redact. * @param int $form_id ID of the form the data belongs to, `0` when unknown. * * @return mixed Data with sensitive field values replaced by `[redacted]`. */ public static function redact_sensitive_fields( $data, int $form_id ) { $default_types = [ 'password' ]; /** * Filters the field types whose submitted values are redacted in debug output and logs. * * @since 2.0.5.3 * * @param string[] $types Field types. * @param int $form_id ID of the form the data belongs to, `0` when unknown. */ $types = apply_filters( 'wpforms_logger_redact_sensitive_fields_types', $default_types, $form_id ); $types = is_array( $types ) ? $types : $default_types; $all_ids = []; $field_ids = []; foreach ( self::get_form_fields( $form_id ) as $field ) { if ( ! isset( $field['id'], $field['type'] ) ) { continue; } $all_ids[] = (string) $field['id']; if ( in_array( $field['type'], $types, true ) ) { $field_ids[] = (string) $field['id']; } } $data = self::redact_fields_value( $data, $types, $field_ids ); $mappings = $field_ids ? self::get_field_mappings( $data, $all_ids, $field_ids ) : []; if ( ! $mappings ) { return $data; } // An integration logs a mapped field under its own parameter name, which only its mapping links to the field. $names = array_unique( array_map( 'strtolower', array_merge( ...array_values( $mappings ) ) ) ); return self::redact_mapped_fields( $data, array_map( 'strval', array_keys( $mappings ) ), $names ); } /** * Get the fields of the form the redacted data belongs to. * * @since 2.0.5.3 * * @param int $form_id Form ID, `0` when unknown. * * @return array Form fields, empty when the form is unknown. */ private static function get_form_fields( int $form_id ): array { if ( ! $form_id ) { return []; } // Any post save changes it, so a form edited in a long request is read again. $last_changed = wp_cache_get_last_changed( 'posts' ); if ( isset( self::$form_fields[ $form_id ] ) && self::$form_fields[ $form_id ]['last_changed'] === $last_changed ) { return self::$form_fields[ $form_id ]['fields']; } // The record is written whoever triggered the request, so the read must not depend on their capabilities. $form_handler = wpforms()->obj( 'form' ); $read_args = [ 'content_only' => true, 'cap' => false, ]; $form_data = $form_handler ? $form_handler->get( $form_id, $read_args ) : []; self::$form_fields[ $form_id ] = [ 'last_changed' => $last_changed, 'fields' => is_array( $form_data ) && is_array( $form_data['fields'] ?? null ) ? $form_data['fields'] : [], ]; return self::$form_fields[ $form_id ]['fields']; } /** * Find the field mappings of an integration, such as `[ 'user_pass' => '2' ]`, that name a sensitive field. * * A mapping is an array whose every non-empty scalar value is a field ID of the form. * * @since 2.0.5.3 * * @param mixed $value Value to search. * @param string[] $all_ids IDs of all fields of the form. * @param string[] $field_ids IDs of the sensitive fields of the form. * @param int $depth Current nesting level. * @param string $path Path of the value in the searched data. * * @return array Names mapped to a sensitive field, keyed by the path of their mapping. */ private static function get_field_mappings( $value, array $all_ids, array $field_ids, int $depth = 0, string $path = '' ): array { if ( $depth > self::MAX_DEPTH || ! is_array( $value ) ) { return []; } $scalars = array_map( 'strval', array_filter( $value, static function ( $item ) { return ( is_string( $item ) || is_int( $item ) ) && $item !== ''; } ) ); $is_mapping = $scalars && ! array_diff( $scalars, $all_ids ); $names = $is_mapping ? array_filter( array_keys( array_intersect( $scalars, $field_ids ) ), 'is_string' ) : []; $nested = [ $names ? [ $path => array_values( $names ) ] : [] ]; foreach ( $value as $key => $item ) { $nested[] = self::get_field_mappings( $item, $all_ids, $field_ids, $depth + 1, $path . "\0" . $key ); } return array_merge( ...$nested ); } /** * Redact the values logged under the names a field mapping gives the sensitive fields. * * @since 2.0.5.3 * * @param mixed $value Value to redact. * @param string[] $mapping_paths Paths of the field mappings in the data. * @param string[] $names Mapped names of the sensitive fields, lowercase. * @param int $depth Current nesting level. * @param string $path Path of the value in the data. * * @return mixed Value with the mapped values replaced by `[redacted]`. */ private static function redact_mapped_fields( $value, array $mapping_paths, array $names, int $depth = 0, string $path = '' ) { if ( $depth > self::MAX_DEPTH || ! is_array( $value ) ) { return $value; } // A mapping only links names to field IDs, so it stays readable. $is_mapping = in_array( $path, $mapping_paths, true ); foreach ( $value as $key => $item ) { if ( ! $is_mapping && ! self::is_empty( $item ) && in_array( strtolower( (string) $key ), $names, true ) ) { $value[ $key ] = self::PLACEHOLDER; continue; } $value[ $key ] = self::redact_mapped_fields( $item, $mapping_paths, $names, $depth + 1, $path . "\0" . $key ); } return $value; } /** * Redact the sensitive field values of a single array, recursively. * * @since 2.0.5.3 * * @param mixed $value Value to redact. * @param string[] $types Sensitive field types. * @param string[] $field_ids IDs of the sensitive fields of the form. * @param int $depth Current nesting level. * * @return mixed Value with sensitive field values replaced by `[redacted]`. */ private static function redact_fields_value( $value, array $types, array $field_ids, int $depth = 0 ) { // Deeper data is replaced as a whole by `wpforms_debug_redact_sensitive_data_value()`. if ( $depth > self::MAX_DEPTH || ! is_array( $value ) ) { return $value; } // A processed field keeps the submitted value under `value` and `value_raw`. if ( isset( $value['type'] ) && in_array( $value['type'], $types, true ) ) { foreach ( array_intersect_key( $value, array_flip( [ 'value', 'value_raw' ] ) ) as $field_key => $field_value ) { $value[ $field_key ] = self::is_empty( $field_value ) ? $field_value : self::PLACEHOLDER; } return $value; } foreach ( $value as $key => $item ) { if ( $key === 'fields' && is_array( $item ) ) { $item = self::redact_submitted_fields( $item, $field_ids ); } $value[ $key ] = self::redact_fields_value( $item, $types, $field_ids, $depth + 1 ); } return $value; } /** * Redact the sensitive fields of a raw submission, which carries no field types. * * @since 2.0.5.3 * * @param array $fields Submitted fields, keyed by field ID. * @param string[] $field_ids IDs of the sensitive fields of the form. * * @return array Fields with sensitive values replaced by `[redacted]`. */ private static function redact_submitted_fields( array $fields, array $field_ids ): array { foreach ( $fields as $id => $item ) { // A Repeater row submits its copy of a field as `<field ID>_<row>`. $field_id = explode( '_', (string) $id )[0]; // A processed field has a type and is redacted by its type, keeping its other keys. if ( ! self::is_empty( $item ) && ! isset( $item['type'] ) && in_array( $field_id, $field_ids, true ) ) { $fields[ $id ] = self::PLACEHOLDER; } } return $fields; } /** * Check whether a value is empty, so it is kept as it is rather than redacted. * * Not `empty()`: a password of `0` is a value too. * * @since 2.0.5.3 * * @param mixed $value Value to check. * * @return bool */ private static function is_empty( $value ): bool { return in_array( $value, [ '', null, [] ], true ); } }
Save File
Cancel