I
I
Igor Vasiliev2016-08-11 05:18:20
Yii
Igor Vasiliev, 2016-08-11 05:18:20

How to prevent xss vulnerability on yii2?

For example, you created an action in the controller:

public function actionPage($target = '') {
            $get = News::find()->where(['link' => $target])->one();
            return $this->render('page', ['target' => $target, 'get' => $get,]); 
  }

Then created a page, file page.php
<?php 
    use yii\helpers\Html; 
    $this->title = $get->title;
?> 
<h1>Hello <?= Html::encode($target) ?></h1> 
<p>Welcome to your <?=$get->world;?></p>
<h1><?=$get->header;?></h1>

And everything seems to be fine, get parameters are excellently transmitted, you look and rejoice. But there comes a moment when someone manually entered another link, and where the get parameters were substituted, it became empty, the page broke!!! Previously, when I wrote self-written sites, in these cases I did a redirect for all entered external links, so even if an attacker suddenly decides to enter some kind of malicious link, he will be redirected either to a page with a list or to a 404 error - there is no such page! And that's all))
QUESTION: no matter how stupid it sounds, tell me how to make a redirect to yii2 so that the entered external links lead to an error of incorrect or non-existing get-requests in order to avoid xss vulnerability and display empty pages?
In the case of numbers, I found out the number of records from the database, and set the condition that if the get request number is greater than the number of records, redirect to the list of articles. Negative and string get requests resulted in a 404 page, and I was calm. It would be possible to do the same with string data by creating an array of existing get requests, but then the xss vulnerability remains open. Enter whatever you want.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlikDex, 2016-08-11
@Isolution666

what about xss?
In your case, it will be enough:

public function actionPage($target = '') {
    $get = News::find()->where(['link' => $target])->one();

    if (null === $get) // В случае неудачного поиска  метод one() возвращает null всегда
        throw new NotFoundHttpException('Page not found');

    return $this->render('page', ['target' => $target, 'get' => $get,]); 
}

E
eskrano, 2016-08-11
@eskrano

Excuse me, but can you explain why you are writing this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question