Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
csrf.php
1<?php
2
3
5
6
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 [
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}
static getCurrent()
Definition context.php:241
__construct(bool $enabled=true, string $tokenName='sessid', bool $returnNew=true)
Definition csrf.php:38