Skip to content

The "previous" exception object can not be escaped #2447

@gmazzap

Description

@gmazzap

Bug Description

We can only escape strings. Exception constructors accept a third argument "previous" which is an exception object and can not be escaped.

I would expect EscapeOutputSniff to only look at the first argument of exception constructors.

Minimal Code Snippet

throw new \Exception(esc_html("Something when wrong"), 0, $previous);

throw new \Exception(esc_html("Something when wrong"), previous: $previous);

throw new \Exception(message: esc_html("Something when wrong"), previous: $previous);

throw new \Exception(previous: $previous);

Error Code

All the throw statements above emit: WordPress.Security.EscapeOutput.ExceptionNotEscaped

Custom Ruleset

N/A

Environment

Question Answer
PHP version all
PHP_CodeSniffer version ^3.9.2
WordPressCS version ^3.1.0
PHPCSUtils version ^1.0.11
PHPCSExtra version ^1.2.1
WordPressCS install type Composer global, Composer project local
IDE (if relevant) --

Additional Context (optional)

I saw that the EscapeOutputSniff class finds the parameter used to construct the exception, then it loops all of them to search for escaping tokens.

I think this issue could be solved only looking at the 1st positional parameter or message, if named. And so replace usage of:

$params = PassedParameters::getParameters( $this->phpcsFile, $call_token );

with:

$messageParam = PassedParameters::getParameter( $this->phpcsFile, $call_token, 1, 'message' );

And then check the parameter, if found.

I'm glad to provide a PR that does this (and test), if wanted.

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions