Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 118 additions & 3 deletions classes/helpers/FrmXMLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,10 @@ private static function fill_field( $field, $form_id ) {
return array(
'id' => (int) $field->id,
'field_key' => (string) $field->field_key,
'name' => (string) $field->name,
'description' => (string) $field->description,
'name' => self::field_text_from_xml( $field->name, $field ),
'description' => self::field_text_from_xml( $field->description, $field ),
'type' => (string) $field->type,
'default_value' => FrmAppHelper::maybe_json_decode( (string) $field->default_value ),
'default_value' => self::field_value_from_xml( $field->default_value, $field ),
'field_order' => (int) $field->field_order,
'form_id' => (int) $form_id,
'required' => (int) $field->required,
Expand All @@ -624,6 +624,24 @@ private static function fill_field( $field, $form_id ) {
);
}

/**
* Decode marked text columns without interpreting unmarked JSON as an array.
*
* @since x.x
*
* @param SimpleXMLElement $value XML text column.
* @param object $field Exported field definition.
*
* @return string
*/
private static function field_text_from_xml( $value, $field ) {
if ( 'base64' === (string) $value['encoding'] ) {
return (string) self::field_value_from_xml( $value, $field );
}

return (string) $value;
}

/**
* Reads a field's options out of the file as an array.
*
Expand Down Expand Up @@ -1985,6 +2003,103 @@ public static function cdata( $str ) {
return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
}

/**
* Whether a field stores literal source rather than encoded compound values.
*
* @since x.x
*
* @param object|SimpleXMLElement $field Field being exported, or the raw XML field being imported.
*
* @return bool
*/
private static function field_value_is_literal( $field ) {
$field = self::normalize_field_for_literal_check( $field );

/**
* @since x.x
*
* @param bool $literal Whether XML must preserve this field's text exactly.
* @param stdClass $field Field with string id, field_key and type properties. Export and import pass the same shape.
*/
return (bool) apply_filters( 'frm_xml_field_value_is_literal', false, $field );
}

/**
* Give the literal value filter the same field shape on export and import.
*
* Import passes a SimpleXMLElement whose properties are objects, so a strict
* comparison such as 'code' === $field->type would never match there.
*
* @since x.x
*
* @param object|SimpleXMLElement $field Field being exported, or the raw XML field being imported.
*
* @return stdClass
*/
private static function normalize_field_for_literal_check( $field ) {
return (object) array(
'id' => isset( $field->id ) ? (string) $field->id : '',
'field_key' => isset( $field->field_key ) ? (string) $field->field_key : '',
'type' => isset( $field->type ) ? (string) $field->type : '',
);
}

/**
* Encode a field value without interpreting literal source as JSON or PHP serialization.
*
* @since x.x
*
* @param string $value Stored field value.
* @param object $field Field being exported.
*
* @return string XML character data.
*/
public static function field_value_to_xml( $value, $field ) {
if ( ! self::field_value_is_literal( $field ) ) {
return self::cdata( $value );
}

// XML normalizes carriage returns and rejects some otherwise valid source characters.
return '<![CDATA[' . base64_encode( $value ) . ']]>';
}

/**
* Mark literal values so imports can recover their exact bytes.
*
* @since x.x
*
* @param object $field Field being exported.
*
* @return array
*/
public static function field_value_xml_attributes( $field ) {
return self::field_value_is_literal( $field ) ? array( 'encoding' => 'base64' ) : array();
}

/**
* Decode compound field data while preserving literal source text.
*
* @since x.x
*
* @param SimpleXMLElement|string $value XML field value.
* @param object $field Destination field or exported field definition.
*
* @return array|string
*/
public static function field_value_from_xml( $value, $field ) {
if ( $value instanceof SimpleXMLElement && 'base64' === (string) $value['encoding'] ) {
// Pretty-printed or wrapped files can add whitespace, which strict decoding rejects.
$encoded = (string) preg_replace( '/\s+/', '', (string) $value );
$decoded = base64_decode( $encoded, true );

// Keep the raw text rather than silently wiping the value when it is not valid base64.
return false === $decoded ? (string) $value : $decoded;
}

$value = (string) $value;
return self::field_value_is_literal( $field ) ? $value : FrmAppHelper::maybe_json_decode( $value );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking. field_value_is_literal( $field ) is called with two different types. On export (forms_xml.php) $field is a stdClass with a string type. On import (fill_field() line 618) it is the raw SimpleXMLElement, so $field->type is an object. A listener written the natural way, 'code' === $field->type, returns true on export and false on import.

I reproduced it with a filter that records its argument. Result: [["stdClass", true], ["SimpleXMLElement", false]]. An unmarked <default_value><![CDATA[{"a":1}]]></default_value> on a code field was then JSON-decoded into array('a' => 1) instead of staying a string. The new test hides this because it passes (object) array( 'type' => 'code' ) for both directions.

Fix: normalise before calling the filter so both directions see the same shape, then add a test that feeds field_value_from_xml() a real SimpleXMLElement:

private static function field_value_is_literal( $field ) {
	$field = (object) array(
		'type' => (string) $field->type,
	);
	// Keep any other keys export callers already pass, if needed.
	return (bool) apply_filters( 'frm_xml_field_value_is_literal', false, $field );
}

Separately: when base64_decode( ..., true ) fails (line 2051), the method returns '' with no notice. A truncated or pretty-printed file silently wipes the default value. Strict mode also rejects whitespace inside the CDATA. Strip whitespace first (preg_replace( '/\s+/', '', (string) $value )) and fall back to the raw string rather than '' on failure.

}

/**
* Remove <US> character (unit separator) from exported strings
*
Expand Down
36 changes: 34 additions & 2 deletions classes/models/FrmEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,47 @@ public static function getOne( $id, $meta = false ) {
* @since 4.02.03
*
* @param object $entry
* @param array $field_types Field types already looked up, keyed by field ID or key.
* Shared across entries so each field type is only looked up once.
*
* @return void
*/
private static function prepare_entry( &$entry ) {
private static function prepare_entry( &$entry, &$field_types = array() ) {
if ( ! $entry ) {
return;
}

FrmAppHelper::unserialize_or_decode( $entry->description );
// TODO: Remove slashes on input only, not output.
$has_metas = property_exists( $entry, 'metas' );
$metas = $entry->metas ?? null;
unset( $entry->metas );
$entry = wp_unslash( $entry );

if ( ! is_array( $metas ) ) {
if ( $has_metas ) {
$entry->metas = wp_unslash( $metas );
}

return;
}

$check_types = has_filter( 'frm_should_unslash_entry_meta_value' );

foreach ( $metas as $field_id => $value ) {
$type = null;

if ( $check_types ) {
if ( ! isset( $field_types[ $field_id ] ) ) {
$field_types[ $field_id ] = (string) FrmField::get_type( $field_id );
}

$type = $field_types[ $field_id ];
}

$metas[ $field_id ] = FrmEntryMeta::maybe_unslash_field_value( $value, $field_id, $type );
}
$entry->metas = $metas;
}

/**
Expand All @@ -538,8 +568,10 @@ private static function prepare_entry( &$entry ) {
* @return void
*/
private static function prepare_entries( &$entries ) {
$field_types = array();

foreach ( $entries as $k => $entry ) {
self::prepare_entry( $entry );
self::prepare_entry( $entry, $field_types );
$entries[ $k ] = $entry;
}
}
Expand Down
44 changes: 40 additions & 4 deletions classes/models/FrmEntryMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public static function get_entry_meta_by_field( $entry_id, $field_id ) {

if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) {
$result = $cached->metas[ $field_id ];
return wp_unslash( $result );
return self::maybe_unslash_field_value( $result, $field_id );
}

$get_table = $wpdb->prefix . 'frm_item_metas';
Expand All @@ -382,7 +382,43 @@ public static function get_entry_meta_by_field( $entry_id, $field_id ) {
$field_type = FrmField::get_type( $field_id );
FrmFieldsHelper::prepare_field_value( $result, $field_type );

return wp_unslash( $result );
return self::maybe_unslash_field_value( $result, $field_id, $field_type );
}

/**
* Keep literal source backslashes when a field opts out of legacy unslashing.
*
* Applied by FrmEntry, get_entry_meta_by_field(), get_entry_metas_for_field() and getAll()
* so every read path returns the same value for a field. The field type is only

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking. The docblock says this filter is applied so "every read path returns the same value for a field", but FrmEntry::getOne( $id, true ) (and FrmEntry::getAll( ..., true ), which uses the same get_meta()) is not covered. FrmEntry::get_meta() collects the values into $entry->metas, and FrmEntry::prepare_entry() (FrmEntry.php:530) then runs wp_unslash( $entry ) over the whole entry object, metas included, without asking this filter. Reproduced at this head with frm_should_unslash_entry_meta_value returning false and a stored value of C:\temp\new:

get_entry_meta_by_field( $entry_id, $field_id )   => 'C:\temp\new'   (literal, as intended)
FrmEntry::getOne( $entry_id, true )->metas[ $f ]  => 'C:tempnew'        (backslashes stripped)

getOne( $id, true ) is the usual way entry values are read for display, editing, emails and exports, so a field that opted out of unslashing still loses its backslashes on the main path. That defeats the point of the filter. Same class of problem as the earlier get_entry_metas_for_field() finding.

Fix: make FrmEntryMeta::maybe_unslash_field_value() callable from FrmEntry (public with an @internal tag, or a small public wrapper), and in FrmEntry::prepare_entry() unslash the entry without its metas, then unslash each meta through it. metas is keyed by field ID (and by field key when frm_include_meta_keys is on), so pass the ID or key as $field_id:

$metas = $entry->metas ?? null;
unset( $entry->metas );
$entry = wp_unslash( $entry );

if ( is_array( $metas ) ) {
	foreach ( $metas as $meta_field_id => $meta_value ) {
		$metas[ $meta_field_id ] = FrmEntryMeta::maybe_unslash_field_value( $meta_value, $meta_field_id );
	}
	$entry->metas = $metas;
}

Note that get_meta() stores the entry in the frm_entry cache before prepare_entry() runs, so cached and uncached reads must go through this same path. Add a test next to test_unslash_filter_applies_to_every_read_path that asserts FrmEntry::getOne( $entry_id, true )->metas[ $field_id ] equals the stored value with the filter returning false, and 'C:tempnew' without it. If you decide getOne() is out of scope instead, correct this docblock and the PR body so they do not say "every read path", and say so in the companion Pro PR.

* looked up when something is hooked to the filter.
*
* @since x.x
*
* @internal
*
* @param mixed $value Stored or cached field value.
* @param int|string $field_id Field ID or key.
* @param string|null $type Field type, when the caller already knows it.
*
* @return mixed
*/
public static function maybe_unslash_field_value( $value, $field_id, $type = null ) {
if ( ! has_filter( 'frm_should_unslash_entry_meta_value' ) ) {
return wp_unslash( $value );
}

if ( null === $type ) {
$type = FrmField::get_type( $field_id );
}

/**
* @since x.x
*
* @param bool $unslash Whether the field value should be unslashed.
* @param string $type Field type.
* @param int|string $field_id Field ID or key.
*/
return apply_filters( 'frm_should_unslash_entry_meta_value', true, $type, $field_id ) ? wp_unslash( $value ) : $value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 4 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

}

/**
Expand Down Expand Up @@ -417,7 +453,7 @@ public static function get_entry_metas_for_field( $field_id, $order = '', $limit
unset( $k, $v );
}

return wp_unslash( $values );
return self::maybe_unslash_field_value( $values, $field_id );
}

/**
Expand Down Expand Up @@ -491,7 +527,7 @@ public static function getAll( $where = array(), $order_by = '', $limit = '', $s

foreach ( $results as $k => $result ) {
FrmAppHelper::unserialize_or_decode( $result->meta_value );
$results[ $k ]->meta_value = wp_unslash( $result->meta_value );
$results[ $k ]->meta_value = self::maybe_unslash_field_value( $result->meta_value, $result->field_id, $result->field_type );
unset( $k, $result );
}

Expand Down
11 changes: 6 additions & 5 deletions classes/models/FrmField.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,10 @@ public static function create( $values, $return = true ) {
*
* @since 7.1
*
* @param int $new_id Created field ID.
* @param int $new_id Created field ID.
* @param array $new_values Stored field columns.
*/
do_action( 'frm_field_created', $new_id );
do_action( 'frm_field_created', $new_id, $new_values );

if ( ! $return ) {
return false;
Expand Down Expand Up @@ -865,7 +866,6 @@ public static function update( $id, $values ) {
}
unset( $field );
}
unset( $values );

if ( $query_results ) {
wp_cache_delete( $id, 'frm_field' );
Expand All @@ -881,9 +881,10 @@ public static function update( $id, $values ) {
*
* @since 7.1
*
* @param int $id Updated field ID.
* @param int $id Updated field ID.
* @param array $values Stored field columns.
*/
do_action( 'frm_field_updated', $id );
do_action( 'frm_field_updated', $id, $values );
}

return $query_results;
Expand Down
7 changes: 4 additions & 3 deletions classes/views/xml/forms_xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@

foreach ( $fields as $field ) {
FrmXMLHelper::prepare_field_for_export( $field );
$default_value_attributes = FrmXMLHelper::field_value_xml_attributes( $field );
?>
<field>
<id><?php echo absint( $field->id ); ?></id>
<field_key><?php echo FrmXMLHelper::cdata( $field->field_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></field_key>
<name><?php echo FrmXMLHelper::cdata( $field->name ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></name>
<description><?php echo FrmXMLHelper::cdata( $field->description ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></description>
<name<?php FrmAppHelper::array_to_html_params( $default_value_attributes, true ); ?>><?php echo FrmXMLHelper::field_value_to_xml( $field->name, $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></name>
<description<?php FrmAppHelper::array_to_html_params( $default_value_attributes, true ); ?>><?php echo FrmXMLHelper::field_value_to_xml( $field->description, $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></description>
<type><?php echo FrmXMLHelper::cdata( $field->type ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></type>
<default_value><?php echo FrmXMLHelper::cdata( $field->default_value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></default_value>
<default_value<?php FrmAppHelper::array_to_html_params( $default_value_attributes, true ); ?>><?php echo FrmXMLHelper::field_value_to_xml( $field->default_value, $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></default_value>
<field_order><?php echo absint( $field->field_order ); ?></field_order>
<form_id><?php echo absint( $field->form_id ); ?></form_id>
<required><?php echo absint( $field->required ); ?></required>
Expand Down
Loading
Loading