Skip to content

Fixes #6141 - Warnings when print_r() is disabled for security reasons#6176

Closed
jimmyandrade wants to merge 5 commits intoAutomattic:masterfrom
jimmyandrade:master
Closed

Fixes #6141 - Warnings when print_r() is disabled for security reasons#6176
jimmyandrade wants to merge 5 commits intoAutomattic:masterfrom
jimmyandrade:master

Conversation

@jimmyandrade
Copy link
Copy Markdown
Contributor

Fixes #6141 - Warnings when print_r() is disabled for security reasons

Changes proposed in this Pull Request:

  • Now, before using print_r() to print $all_values data on Feedback post content, Jetpack will check if print_r function was not disabled.

Jimmy Andrade added 2 commits January 26, 2017 12:06
Fixes a warning when a visitor submit a contact form in a website hosted with print_r disabled.
(Jetpack Contact Form uses print_r to store all values in Feedback post content so that search will pick up this data).
@jeherve jeherve added [Feature] Forms [Pri] Low [Status] Needs Review This PR is ready for review. Enhancement Changes to an existing feature — removing, adding, or changing parts of it labels Jan 26, 2017
@jeherve jeherve added this to the 2/17 - February milestone Jan 26, 2017
Copy link
Copy Markdown
Contributor

@georgestephanis georgestephanis left a comment

Choose a reason for hiding this comment

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

Honestly, this is way too complicated of a solution for a rare server configuration. If anything, I'd suggest possibly doing something like ...

$all_values_string = apply_filters( 'grunion_print_r_all_values', @print_r( $all_values, true ), $all_values );

and then dropping $all_values into the line below instead. The @ should silence any warnings you may get, and the filter would let you rebuild the string for searchability manually as the code is intended to behave.

But in either case, I'm not sure if it's a good idea for us to set a precedent of working around disabling basic functions of a language.

}

$post_content_string = $comment_content . "\n<!--more-->\n" . "AUTHOR: {$comment_author}\n
AUTHOR EMAIL: {$comment_author_email}\n
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.

The prior string used to insert just a \n between lines, the new code does that and then also does an actual new line (I've not examined it closely enough to identify if it's \r\n or just another \n) followed by tabs. This would change it so that the formatting is not consistent with the prior entries. :\

While it is more readable in code, it's less reliable for any code trying to parse out the content (not the best way to do things, but the things I've seen ...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another thing I had not paid attention to.
To keep the code readable while avoiding inconsistency in formatting, I switched to:

$post_content_string =  $comment_content . "\n<!--more-->\n"
	. "AUTHOR: {$comment_author}\n"
	. "AUTHOR EMAIL: {$comment_author_email}\n"
	. "AUTHOR URL: {$comment_author_url}\n"
	. "SUBJECT: {$subject}\n"
	. "IP: {$comment_author_IP}\n"
	. $all_values_string;

* with this work around. */
add_filter( 'wp_insert_post_data', array( $plugin, 'insert_feedback_filter' ), 10, 2 );

$disabled_functions = explode( ',', ini_get( 'disable_functions' ) );
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.

Not the best way to do this. Any item in disable_functions can be detected as disabled by testing it against function_exists() as per https://secure.php.net/manual/en/function.function-exists.php#67947

We won't be able to redefine it, but it's a tidier way to check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @georgestephanis. Thanks for all the feedback.

Firstly, about "rare server configuration". As I said before, Locaweb is one of the most common hosting providers in Brazil, and some of accounts are configured with print_r disabled. It is possible that other people will experience this same problem.

Regarding the use of the filter, it is really a better strategy...

$all_values_string = apply_filters( 'grunion_print_all_values', @print_r( $all_values, true ), $all_values );

I did not really know that function_exists detected disabled functions, so because of your hint I've switched to:

if ( function_exists( 'print_r' ) ) {
   // ...
}

$all_values_string = print_r( $all_values, true );
}
// If print_r is disabled, try to use var_export as an alternative solution
elseif ( ! in_array( 'var_export', $disabled_functions ) ) {
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.

As per WP Coding Style, conditionals should be on the same line as the prior closing brace. So this line should be:

} elseif ( ! in_array( 'var_export', $disabled_functions ) ) {

with the comment either at the end of the line, or below and indented.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you! I had not realized that.

$all_values_string = var_export( $all_values, true );
}
else {
$all_values_string = '';
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.

If neither print_r or var_export are available, shouldn't this manually iterate through and build the string itself for searchability, rather than just setting it to empty?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is true. So, I wrote something like this:

$all_values_string = "Array\n(\n";
foreach ( $all_values as $key => $value )  {
   $all_values_string .= "\t[$key] => $value\n";
}
$all_values_string .= ')';

@jeherve jeherve added [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Jan 26, 2017
* Adds the @ to silence any warnings on print_r or var_export;
* Adds a filter that let developers rebuild Feedback post string for searchability manually as the code is intended to behave;
* Using function_exists() instead of ini_get( 'disable_functions' ) to detect if a function has been disabled;
* Fixing  WP Coding Style issues about conditionals and closing braces;
* Added a way to iterate through Feedback data array when print_r and var_export are disabled;
* Fixing an issue that could cause Feedback data formatting being not consistent, but maintaining the code readability.
@jeherve jeherve added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 27, 2017
Copy link
Copy Markdown
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to apply those changes! I have a few remarks that I added inline below. I hope this helps!

I also wanted to address your concern:

Firstly, about "rare server configuration". As I said before, Locaweb is one of the most common hosting providers in Brazil, and some of accounts are configured with print_r disabled. It is possible that other people will experience this same problem.

This makes me wonder if you're on an old server that hasn't been updated. The Contact Form module was introduced in Jetpack more than 4 years ago, and was available as a stand-alone plugin before that. Yet that's the first report we've had about this bug.

It might be worth asking your hosting provider if they would consider moving you to a different server where print_r isn't disabled.

// Avoid warnings when print_r() has been disabled on hosting provider for security reasons
// function_exists will return false for functions disabled with the disable_functions ini directive
if ( function_exists( 'print_r' ) ) {
$all_values_string = apply_filters( 'grunion_print_all_values', @print_r( $all_values, true ), $all_values );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I did that. Thank you so much.

@jeherve jeherve added [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Jan 27, 2017
@georgestephanis
Copy link
Copy Markdown
Contributor

Honestly, I'm not sure if the filter is even necessary. Couldn't folks just use a core filter on insert posts of the respective post type to append the fields? I'm unsure offhand if there'd a scope concern there.

@jimmyandrade
Copy link
Copy Markdown
Contributor Author

@georgestephanis, I confess that I do not know the WP core filters very well. Just like you, I'm not sure either. Well... I decided to keep this filter because it might be that someone wants to customize $all_values for some reason... (I hope that's not a problem)

* Now, function_exists is being used in a more expressive way;
* When both the print_r and var_export functions are disabled, printing of the variables will be in charge of the filter grunion_print_all_values;
* Added a docblock before the new grunion_print_all_values filter.
@jeherve jeherve added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 27, 2017
@jimmyandrade
Copy link
Copy Markdown
Contributor Author

Awaiting review after the latest proposed changes...

@georgestephanis
Copy link
Copy Markdown
Contributor

@jimmyandrade Can you give #6234 a shot and see if that resolves your issue please?

@jeherve
Copy link
Copy Markdown
Member

jeherve commented Jan 30, 2017

Replaced by #6234

@jeherve jeherve closed this Jan 30, 2017
@jeherve jeherve modified the milestones: 2/17 - February, Not Currently Planned Jan 30, 2017
@kraftbj kraftbj removed the [Status] Needs Review This PR is ready for review. label Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] Forms [Pri] Low Touches WP.com Files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warnings when print_r() is disabled for security reasons

5 participants