File: IframeController.php

package info (click to toggle)
icingaweb2 2.12.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,124 kB
  • sloc: php: 73,980; javascript: 5,009; sql: 333; xml: 75; sh: 72; makefile: 5
file content (110 lines) | stat: -rw-r--r-- 3,648 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Controllers;

use Icinga\Web\Session;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\Web\Compat\CompatController;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Tabs;

/**
 * Display external or internal links within an iframe
 */
class IframeController extends CompatController
{
    /**
     * Display iframe w/ the given URL
     */
    public function indexAction(): void
    {
        $url = Url::fromPath($this->params->getRequired('url'));
        $urlHash = $this->getRequest()->getHeader('X-Icinga-URLHash');
        $expectedHash = hash('sha256', $url->getAbsoluteUrl() . Session::getSession()->getId());
        $iframeUrl = Url::fromPath('iframe', ['url' => $url->getAbsoluteUrl()]);

        if (! in_array($url->getScheme(), ['http', 'https'], true)) {
            $this->httpBadRequest('Invalid URL scheme');
        }

        $this->injectTabs();

        $this->getTabs()->setRefreshUrl($iframeUrl);

        if ($urlHash) {
            if ($urlHash !== $expectedHash) {
                $this->httpBadRequest('Invalid URL hash');
            }
        } else {
            $this->addContent(Html::tag('div', ['class' => 'iframe-warning'], [
                Html::tag('h2', $this->translate('Attention!')),
                Html::tag('p', ['class' => 'note'], $this->translate(
                    'You are about to open untrusted content embedded in Icinga Web! Only proceed,'
                    .' by clicking the link below, if you recognize and trust the source!'
                )),
                Html::tag('a', ['data-url-hash' => $expectedHash, 'href' => Html::escape($iframeUrl)], $url),
                Html::tag('p', ['class' => 'reason'], [
                    new Icon('circle-info'),
                    Text::create($this->translate(
                        'You see this warning because you do not seem to have followed a link in Icinga Web.'
                        . ' You can bypass this in the future by configuring a navigation item instead.'
                    ))
                ])
            ]));

            return;
        }

        $this->getTabs()->setHash($expectedHash);

        $this->addContent(Html::tag(
            'div',
            ['class' => 'iframe-container'],
            Html::tag('iframe', [
                'src' => $url,
                'sandbox' => 'allow-same-origin allow-scripts allow-popups allow-forms',
            ])
        ));
    }

    private function injectTabs(): void
    {
        $this->tabs = new class extends Tabs {
            private $hash;

            public function setHash($hash)
            {
                $this->hash = $hash;

                return $this;
            }

            protected function assemble()
            {
                $tabHtml = substr($this->tabs->render(), 34, -5);
                if ($this->refreshUrl !== null) {
                    $tabHtml = preg_replace(
                        [
                            '/(?<=class="refresh-container-control spinner" href=")([^"]*)/',
                            '/(\s)(?=href)/'
                        ],
                        [
                            $this->refreshUrl->getAbsoluteUrl(),
                            ' data-url-hash="' . $this->hash . '" '
                        ],
                        $tabHtml
                    );
                }

                BaseHtmlElement::add(HtmlString::create($tabHtml));
            }
        };

        $this->controls->setTabs($this->tabs);
    }
}