1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
csrf.php
См. документацию.
1<?php
2
3
4namespace Bitrix\Main\Engine\ActionFilter;
5
6
7use Bitrix\Main\Context;
8use Bitrix\Main\Engine\Controller;
9use Bitrix\Main\Error;
10use Bitrix\Main\Event;
11use Bitrix\Main\EventResult;
12
13final class Csrf extends Base
14{
15 public const HEADER_WITH_NEW_CSRF = 'X-Bitrix-New-Csrf';
16 public const ERROR_INVALID_CSRF = 'invalid_csrf';
17
21 private $enabled;
25 private $tokenName;
29 private $returnNew;
30
38 public function __construct(bool $enabled = true, string $tokenName = 'sessid', bool $returnNew = true)
39 {
40 $this->enabled = $enabled;
41 $this->tokenName = $tokenName;
42 $this->returnNew = $returnNew;
43 parent::__construct();
44 }
45
50 public function listAllowedScopes()
51 {
52 return [
53 Controller::SCOPE_AJAX,
54 ];
55 }
56
57 public function onBeforeAction(Event $event)
58 {
59 if (!$this->enabled)
60 {
61 return null;
62 }
63
64 if (!check_bitrix_sessid($this->tokenName))
65 {
66 $errorCustomData = [];
67 if ($this->returnNew)
68 {
69 $errorCustomData['csrf'] = bitrix_sessid();
70 Context::getCurrent()->getResponse()->addHeader(
71 self::HEADER_WITH_NEW_CSRF, $errorCustomData['csrf']
72 );
73 }
74
75 $this->addError(new Error(
76 'Invalid csrf token',
77 self::ERROR_INVALID_CSRF, $errorCustomData
78 ));
79
80 return new EventResult(EventResult::ERROR, null, null, $this);
81 }
82
83 return null;
84 }
85}
addError(Error $error)
Определения base.php:80
onBeforeAction(Event $event)
Определения csrf.php:57
const ERROR_INVALID_CSRF
Определения csrf.php:16
const HEADER_WITH_NEW_CSRF
Определения csrf.php:15
__construct(bool $enabled=true, string $tokenName='sessid', bool $returnNew=true)
Определения csrf.php:38
Определения error.php:15
Определения event.php:5
check_bitrix_sessid($varname='sessid')
Определения tools.php:4686
bitrix_sessid()
Определения tools.php:4656
$event
Определения prolog_after.php:141