Skip to content

[5.8] Strip slashes for unique rule helper - #27940

Merged
taylorotwell merged 1 commit into
laravel:5.8from
themsaid:pr/22187
Mar 20, 2019
Merged

[5.8] Strip slashes for unique rule helper#27940
taylorotwell merged 1 commit into
laravel:5.8from
themsaid:pr/22187

Conversation

@themsaid

@themsaid themsaid commented Mar 19, 2019

Copy link
Copy Markdown
Member

This removes the need to introduce a breaking change while prevents manipulating the generated rule string.

{
$this->idColumn = str_replace(',', '', $idColumn ?? $model->getKeyName());
$this->ignore = str_replace(',', '', $model->{$this->idColumn});
$this->idColumn = addslashes($idColumn ?? $model->getKeyName());

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.

Why would addslashes be relevant for the column, which I would expect is never a user provided input?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hmm I wonder why a “user” would request anything with any special characters.... maybe because they are maliciously trying things to a site?

@deleugpn deleugpn Mar 20, 2019

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.

I think mfn's point is the same as the one Taylor made in the blog post: the developer using Laravel should never expose the ability for their user to define the database column through an input.
Worst case scenario the user input should be decoupled from the database, which would never be vulnerable.

@gocanto gocanto Mar 28, 2019

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.

this could have simple avoided by type-hinting the method


$this->ignore = str_replace(',', '', $id);
$this->idColumn = str_replace(',', '', $idColumn ?? 'id');
$this->ignore = addslashes($id);

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.

Stupid q: is this escaping so the value can be literally used in a DB query, where no further escaping happens?

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.

Using addslashes() to escape a value for a DB query is quite dangerous.

@mfn mfn Mar 19, 2019

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.

My thoughts, thus I'm asking. Not very familiar with the exact code, though.

But I know at least for Postgres it doesn't even work correctly.

@taylorotwell taylorotwell Mar 20, 2019

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.

We aren't adding slashes for the DB query. We are adding slashes for the str_getcsv call in the validator. This has nothing to do with trying to escape data for a DB query.

To clarify, the vulnerability is that a user can construct a string that is sent through the str_getcsv call by the validator and uses a column of their choosing in the DB query. The value is bound as a PDO parameter. However, columns are not. Therefore, we need to ensure that the "id" portion of the unique validation rule is a single string and can't be manipulated with commas / quotes to create multiple values when parsed by str_getcsv.

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.

👍 thanks for clarification.

I think however that the code could use some comments as to why all these special handling.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Because a vulnerability was reported. It was possible to perform a XSS using the ID

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.

It's not XSS.

@Jurigag Jurigag Mar 20, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I know this is old article but i guess it tells you why addslashes is not perfect solution here - http://shiflett.org/blog/2006/addslashes-versus-mysql-real-escape-string Not sure though how it's realted to what @taylorotwell wrote, i guess someone needs to check it out.

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.

Keep in mind that Taylor already clarified, this is not for escaping for the database but for another case.

Also, the code already changed in the meantime and the addslashes was moved to the __toString and only for the ignore and not the column.

@taylorotwell
taylorotwell merged commit 5015a79 into laravel:5.8 Mar 20, 2019
@franzliedke

Copy link
Copy Markdown
Contributor

A regression test would be cool. 😉

@sirmx100

Copy link
Copy Markdown

Good job guys! You saved probably a trillion dollars by acting so fast on this to many organizations who wouldn't even had known! If i was filthy rich I'd buy each of you

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

Successfully merging this pull request may close these issues.