Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Security" warnings about unescaped output #802

Open
joho1968 opened this issue Nov 19, 2024 · 2 comments
Open

"Security" warnings about unescaped output #802

joho1968 opened this issue Nov 19, 2024 · 2 comments

Comments

@joho1968
Copy link

Sorry about nagging about this, but I quite often fail to see how this "escaping output" thing is supposed to work in an actual scenario.

// Mimic class-IXR-server output
$charset = function_exists( 'get_option' ) ? get_option( 'blog_charset' ) : '';
if ( ! empty( $charset ) ) {
    $xml_safe = '<?xml version="1.0" encoding="' . wp_kses_no_null( $charset ) . '"?>' . "\n";
} else {
    $xml_safe = '<?xml version="1.0"?>' . "\n";
}
header( 'Connection: close' );
if ( ! empty( $charset ) ) {
    header( 'Content-Type: text/xml; charset=' . wp_kses_no_null( $charset ) );
} else {
    header( 'Content-Type: text/xml' );
}
header( 'Date: ' . gmdate( 'r' ) );
echo $xml_safe;
echo wp_kses_no_null( $error->getXml() );

This generates warning to the effect of

ERROR 	
WordPress.Security.EscapeOutput.OutputNotEscaped 	
All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '$xml_safe'.

ERROR 	
WordPress.Security.EscapeOutput.OutputNotEscaped 	
All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found 'wp_kses_no_null'. 

Why? And what, more precisely, am I supposed to use here ...? 🤔

The same thing applies when I create a concatenated string containing HTML for output like:

$html = '<div>This and that</div>';
$html .= '<p>Here is more output</p>';
echo $html;

This will also generate "security warnings" ...

@davidperezgar
Copy link
Member

davidperezgar commented Nov 20, 2024

I suggest to instead of using a variable that gets all data, echo every line so it will be escaped in all lines.

@joho1968
Copy link
Author

I suggest to instead of using a variable that gets all data, echo every line so it will be escaped in all lines.

That is, of course, an option. And maybe I need to re-think this specifically for WordPress coding... 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants