Skip to content

Commit ff19ac8

Browse files
author
Antti Kuosmanen
committed
Add some missing sanitisation and output escapes
Thanks plugins@wordpress.org for pointing these out!
1 parent 04b1c41 commit ff19ac8

6 files changed

Lines changed: 26 additions & 18 deletions

File tree

classes/class-cpt-wplf-form.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,14 @@ function save_cpt( $post_id ) {
389389

390390
// save title format
391391
if ( isset( $_POST['wplf_title_format'] ) ) {
392-
update_post_meta( $post_id, '_wplf_title_format', $_POST['wplf_title_format'] );
392+
$safe_title_format = $_POST['wplf_title_format']; // TODO: are there any applicable sanitize functions?
393+
394+
// A typical title format will include characters like <, >, %, -.
395+
// which means all sanitize_* fuctions will probably mess with the field
396+
// The only place the title formats are displayed are within value=""
397+
// attributes where of course they are escaped using esc_attr() so it
398+
// should be fine to save the meta field without further sanitisaton
399+
update_post_meta( $post_id, '_wplf_title_format', $safe_title_format );
393400
}
394401
}
395402

@@ -419,8 +426,8 @@ function wplf_form( $id , $content = '', $xclass = '' ) {
419426
?>
420427
<form class="libre-form libre-form-<?php echo $id . ' ' . $xclass; ?>">
421428
<?php echo apply_filters( 'wplf_form', $content ); ?>
422-
<input type="hidden" name="referrer" value="<?php echo get_the_permalink(); ?>">
423-
<input type="hidden" name="_form_id" value="<?php echo $id; ?>">
429+
<input type="hidden" name="referrer" value="<?php the_permalink(); ?>">
430+
<input type="hidden" name="_form_id" value="<?php esc_attr_e( $id ); ?>">
424431
</form>
425432
<?php
426433
$output = ob_get_clean();

classes/class-cpt-wplf-submission.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ public static function register_cpt() {
7575
function custom_columns_display_cpt( $column, $post_id ) {
7676
if( 'referrer' === $column ) {
7777
if( $referrer = get_post_meta($post_id, 'referrer', true) ) {
78-
echo '<a href="' . esc_url( $referrer ) . '">' . $referrer . '</a>';
78+
echo '<a href="' . esc_url_raw( $referrer ) . '">' . esc_url( $referrer ) . '</a>';
7979
}
8080
}
8181
if( 'form' === $column ) {
8282
if( $form_id = get_post_meta($post_id, '_form_id', true) ) {
8383
$form = get_post( $form_id );
84-
echo '<a href="' . get_edit_post_link( $form_id, '' ) . '" target="_blank">' . $form->post_title . '</a>';
84+
echo '<a href="' . get_edit_post_link( $form_id, '' ) . '" target="_blank">' . esc_html( $form->post_title ) . '</a>';
8585
}
8686
}
8787
}
@@ -186,9 +186,9 @@ function metabox_submission() {
186186
<tr>
187187
<th><strong><?php echo $field; ?></strong></th>
188188
<?php if( strlen( $value ) > 60 || strpos( $value, "\n" ) ) : ?>
189-
<td><textarea style="width:100%" readonly><?php echo $value; ?></textarea></td>
189+
<td><textarea style="width:100%" readonly><?php echo esc_textarea( $value ); ?></textarea></td>
190190
<?php else : ?>
191-
<td><input style="width:100%" type="text" value="<?php echo $value; ?>" readonly></td>
191+
<td><input style="width:100%" type="text" value="<?php esc_attr_e( $value ); ?>" readonly></td>
192192
<?php endif; ?>
193193
</tr>
194194
<?php endif; ?>

inc/wplf-ajax.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ function wplf_ajax_submit_handler() {
1313
do_action('wplf_pre_validate_submission');
1414

1515
// validate form fields
16-
// see wplf-form-validation.php
16+
// @see: wplf-form-validation.php
1717
$return = apply_filters( 'wplf_validate_submission', $return );
1818

1919
if( $return->ok ) {
20-
2120
// form existence has already been validated via filters
22-
$form = get_post( $_POST['_form_id'] );
21+
$form = get_post( intval( $_POST['_form_id'] ) );
2322

2423
// the title is the value of whatever the first field was in the form
2524
$title_format = get_post_meta( $form->ID, '_wplf_title_format', true );
@@ -31,7 +30,7 @@ function wplf_ajax_submit_handler() {
3130
foreach($toks[1] as $tok) {
3231
$replace = '';
3332
if( array_key_exists( $tok, $_POST ) ) {
34-
$replace = $_POST[$tok];
33+
$replace = sanitize_text_field( $_POST[$tok] );
3534
}
3635
$post_title = preg_replace('/%.+?%/', $replace, $post_title, 1);
3736
}

inc/wplf-form-actions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
/**
44
* Send a copy of the form fields email if feature is enabled
55
*/
6-
add_action( 'wplf_post_validate_submission', 'wplf_send_email_copy' );
6+
add_action( 'wplf_post_validate_submission', 'wplf_send_email_copy', 20 );
77
function wplf_send_email_copy( $return ) {
88
// do nothing if form validation failed
99
if( ! $return->ok ) {
1010
return;
1111
}
1212

13-
$form_id = $_POST['_form_id'];
14-
$form_title = get_the_title( $form_id );
13+
$form_id = intval( $_POST['_form_id'] ); // _form_id is already validated and we know it exists by this point
14+
$form_title = esc_html( get_the_title( $form_id ) );
1515
$form_meta = get_post_meta( $form_id );
16-
$referrer = $_POST['referrer'];
16+
$referrer = esc_url_raw( $_POST['referrer'] );
1717

1818
if( isset($form_meta['_wplf_email_copy_enabled']) && $form_meta['_wplf_email_copy_enabled'][0] ) {
1919
$to = isset($form_meta['_wplf_email_copy_to']) ? $form_meta['_wplf_email_copy_to'][0] : get_option( 'admin_email' );

inc/wplf-form-validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function wplf_validate_form_exists( $return ) {
1010
return $return;
1111
}
1212

13-
if( ! isset($_POST['_form_id']) || 'publish' != get_post_status( $_POST['_form_id'] ) || 'wplf-form' != get_post_type( $_POST['_form_id'] ) ) {
13+
if( ! isset( $_POST['_form_id'] ) || ! is_numeric( $_POST['_form_id'] ) || 'publish' != get_post_status( $_POST['_form_id'] ) || 'wplf-form' != get_post_type( $_POST['_form_id'] ) ) {
1414
$return->ok = 0;
1515
$return->error = sprintf( __("Form id %d doesn't exist!", 'wp-libre-form'), intval( $_POST['_form_id'] ) );
1616
}

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ function my_email_thankyou( $return ) {
6666
return;
6767
}
6868

69-
$to = '"' . $_POST['name'] . '" <' . sanitize_email( $_POST['email'] ) . '>';
69+
$name = sanitize_text_field( $_POST['name'] );
70+
$email = sanitize_email( $_POST['email'] );
71+
$to = '"' . $name . '" <' . $email . '>';
7072
$subject = __( 'Thank You For Submitting A Form' );
71-
$content = wp_sprintf( __('Thanks, %s for clicking Submit on this glorious HTML5 Form!'), $_POST['name'] );
73+
$content = wp_sprintf( __('Thanks, %s for clicking Submit on this glorious HTML5 Form!'), $name );
7274
wp_mail( $to, $subject, $content );
7375
}
7476
```

0 commit comments

Comments
 (0)