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

MessageSource::getMessageFilePath() - filename injection #18912

Closed
uaoleg opened this issue Sep 29, 2021 · 15 comments
Closed

MessageSource::getMessageFilePath() - filename injection #18912

uaoleg opened this issue Sep 29, 2021 · 15 comments
Labels
severity:security status:ready for adoption Feel free to implement this issue.
Milestone

Comments

@uaoleg
Copy link
Contributor

uaoleg commented Sep 29, 2021

Yii::$app->language can contain any string, and this string is directly passed into MessageSource::getMessageFilePath() and used in filename without any validation:

$messageFile = Yii::getAlias($this->basePath) . "/$language/";

That could be a potetial security issue:

The message file for category 'yii' does not exist: /var/www/app/vendor/yiisoft/yii2/messages/"><script >alert(String.fromCharCode(88,83,83))</script>/yii.php Fallback file does not exist as well: /var/www/app/vendor/yiisoft/yii2/messages/">/yii.php

@bizley
Copy link
Member

bizley commented Sep 29, 2021

Is there any place where end user could provide the language directly and that message would pop up? I cannot find such case.

@WinterSilence
Copy link
Contributor

Yii don't have "fools protection"

@uaoleg
Copy link
Contributor Author

uaoleg commented Sep 29, 2021

@bizley the case is any website that provides language selection on UI
@WinterSilence it's not obvious at all that I have to escape somehow the language name

@WinterSilence
Copy link
Contributor

@uaoleg you must escape ANY user data - it's contained in ANY security guide

@uaoleg
Copy link
Contributor Author

uaoleg commented Sep 29, 2021

@WinterSilence no, I should not. For example if user has name O'Reilly I won't escape it to save in DB, I will save it as is. And framework's ORM will do the job

@WinterSilence
Copy link
Contributor

@uaoleg read more about second-order SQL injection
I don't know if I translated it correctly, it's called in Russian: SQL-инъекция второго порядка

@bizley
Copy link
Member

bizley commented Sep 29, 2021

the case is any website that provides language selection on UI

Since the framework is not using that value (I mean "provided by user") directly we rely on the developer to validate it when passing to the component.

@uaoleg
Copy link
Contributor Author

uaoleg commented Sep 29, 2021

@bizley it's not obvious that this property is used as a part of filepath. I'll create a simple PR which will fix this issue

public $language = 'en-US';

@bizley
Copy link
Member

bizley commented Sep 29, 2021

Hm, ok, please do. Maybe we need to add some more clarification for that.

@samdark samdark added this to the 2.0.44 milestone Sep 29, 2021
@samdark samdark added the status:ready for adoption Feel free to implement this issue. label Sep 29, 2021
@samdark
Copy link
Member

samdark commented Sep 29, 2021

We should escape it. Something like preg_replace('/[^\w_-]/', '_', $language) should be sufficient.

@uaoleg
Copy link
Contributor Author

uaoleg commented Sep 29, 2021

Created a PR #18913

@WinterSilence
Copy link
Contributor

@samdark class must generate error instead escape: if (preg_match('/[^a-zA-Z_-]/', $language) === 1) { throw new ... }

@samdark
Copy link
Member

samdark commented Sep 30, 2021

Agree 👍

@samdark
Copy link
Member

samdark commented Sep 30, 2021

@uaoleg would you please adjust the pull request? It should throw \InvalidArgumentException.

@uaoleg
Copy link
Contributor Author

uaoleg commented Sep 30, 2021

@samdark done

@samdark samdark closed this as completed Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity:security status:ready for adoption Feel free to implement this issue.
Projects
None yet
Development

No branches or pull requests

4 participants