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

ProperEscapingFunction: improve "action" match precision #670

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
*/
class ProperEscapingFunctionSniff extends Sniff {

/**
* Regular expression to match the end of HTML attributes.
*
* @var string
*/
const ATTR_END_REGEX = '`(?<attrname>href|src|url|(^|\s+)action)?=(?:\\\\)?["\']*$`i';

/**
* List of escaping functions which are being tested.
*
Expand Down Expand Up @@ -49,31 +56,6 @@ class ProperEscapingFunctionSniff extends Sniff {
T_NS_SEPARATOR => T_NS_SEPARATOR,
];

/**
* List of attributes associated with url outputs.
*
* @var array
*/
private $url_attrs = [
'href',
'src',
'url',
'action',
];

/**
* List of syntaxes for inside attribute detection.
*
* @var array
*/
private $attr_endings = [
'=',
'="',
"='",
"=\\'",
'=\\"',
];

/**
* Returns an array of tokens this test wants to listen for.
*
Expand Down Expand Up @@ -134,57 +116,23 @@ public function process_token( $stackPtr ) {
return;
}

if ( $escaping_type !== 'url' && $this->attr_expects_url( $content ) ) {
if ( preg_match( self::ATTR_END_REGEX, $content, $matches ) !== 1 ) {
return;
}

if ( $escaping_type !== 'url' && empty( $matches['attrname'] ) === false ) {
$message = 'Wrong escaping function. href, src, and action attributes should be escaped by `esc_url()`, not by `%s()`.';
$this->phpcsFile->addError( $message, $stackPtr, 'hrefSrcEscUrl', $data );
return;
}

if ( $escaping_type === 'html' && $this->is_html_attr( $content ) ) {
if ( $escaping_type === 'html' ) {
$message = 'Wrong escaping function. HTML attributes should be escaped by `esc_attr()`, not by `%s()`.';
$this->phpcsFile->addError( $message, $stackPtr, 'htmlAttrNotByEscHTML', $data );
return;
}
}

/**
* Tests whether provided string ends with open attribute which expects a URL value.
*
* @param string $content Haystack in which we look for an open attribute which exects a URL value.
*
* @return bool True if string ends with open attribute which expects a URL value.
*/
public function attr_expects_url( $content ) {
$attr_expects_url = false;
foreach ( $this->url_attrs as $attr ) {
foreach ( $this->attr_endings as $ending ) {
if ( $this->endswith( $content, $attr . $ending ) === true ) {
$attr_expects_url = true;
break;
}
}
}
return $attr_expects_url;
}

/**
* Tests whether provided string ends with open HMTL attribute.
*
* @param string $content Haystack in which we look for open HTML attribute.
*
* @return bool True if string ends with open HTML attribute.
*/
public function is_html_attr( $content ) {
$is_html_attr = false;
foreach ( $this->attr_endings as $ending ) {
if ( $this->endswith( $content, $ending ) === true ) {
$is_html_attr = true;
break;
}
}
return $is_html_attr;
}

/**
* Tests whether an attribute escaping function is being used outside of an HTML tag.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ echo 'data-param-url="' . Esc_HTML( $share_url ) . '"'; // Error.

?>

<form method="post" action="<?php echo esc_html(admin_url('admin.php?page='.$base_name.'&amp;mode=logs&amp;id='.$poll_id)); ?>">


<form method="post" action="<?php echo esc_html(admin_url('admin.php?page='.$base_name.'&amp;mode=logs&amp;id='.$poll_id)); ?>"><!-- Error. -->
<input data-action="<?php echo esc_attr( $my_var ); ?>"><!-- OK. -->
<a href='https://demo.com?foo=bar&my-action=<?php echo esc_attr( $var ); ?>'>link</a><!-- OK. -->

<a href="#link"><?php echo esc_attr( 'testing' ); // Error.
?> </a>
Expand Down Expand Up @@ -85,3 +85,13 @@ echo 'data-param-url="' . Esc_HTML::static_method( $share_url ) . '"'; // OK.

// Not a target for this sniff (yet).
printf( '<meta name="generator" content="%s">', esc_attr( $content ) ); // OK.
?>

// Making sure tabs and new lines before "action" are handled correctly.
<input class="something something-else something-more"
action="<?php echo esc_attr( $my_var ); ?>"><!-- Error. -->
<?php
echo '<input class="something something-else something-more"
action="', esc_url( $my_var ), '">'; // OK.
echo '<input class="something something-else something-more"
action="', esc_attr( $my_var ), '">'; // Error.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function getErrorList() {
79 => 1,
80 => 1,
82 => 1,
92 => 1,
97 => 1,
];
}

Expand Down