File size: 5,622 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Framework;

use Piwik\Common;
use Piwik\Option;

/**
 * TODO: doc
 *
 * TODO: describe need to keep strings as small as possible
 */
class XssTesting
{
    public const OPTION_NAME = 'Tests.xssEntries';

    public function forTwig($type, $sanitize = false)
    {
        $n = $this->addXssEntry($type, 'twig');

        $result = "<script>_x($n)</script>";
        if ($sanitize) {
            // NOTE: since API\Request does sanitization, API methods do not. when calling them, we must
            // sometimes do sanitization ourselves.
            $result = Common::sanitizeInputValue($result);
        }
        return $result;
    }

    /**
     * @deprecated
     */
    public function forAngular($type, $sanitize = false)
    {
        return $this->forVueJs($type, $sanitize);
    }

    public function forVueJs($type, $sanitize = false)
    {
        $n = $this->addXssEntry($type, 'vuejs');

        $result = "{{_Vue.h.constructor`_x($n)`()}}";
        if ($sanitize) {
            $result = Common::sanitizeInputValue($result);
        }
        return $result;
    }

    private function addXssEntry($attackVectorType, $injectionType)
    {
        $entries = $this->getXssEntries();
        $xssName = $injectionType . '-(' . $attackVectorType . ')';

        $index = array_search($xssName, $entries);
        if ($index !== false) {
            return $index;
        }

        $key = count($entries);
        $entries[$key] = $xssName;
        $this->setXssEntries($entries);
        return $key;
    }

    public function getXssEntries()
    {
        $value = Option::get(self::OPTION_NAME);
        return json_decode($value, $isAssoc = true) ?: [];
    }

    private function setXssEntries($entries)
    {
        $value = json_encode($entries);
        Option::set(self::OPTION_NAME, $value);
    }

    private function getJavaScriptCode()
    {
        $entries = json_encode($this->getXssEntries());

        $js = <<<JS
window._xssEntryTypes = $entries;
window._x = function triggerXss(id) {
    var message = "XSS triggered (id = " + id + "): " + window._xssEntryTypes[id];
    $(document.body).append(message);
    var e = (new Error(message));
    console.log(e.stack || e.message);
};
JS;
        return $js;
    }

    public static function getJavaScriptAddEvent()
    {
        $xssTesting = new XssTesting();
        return ['Template.jsGlobalVariables', \Piwik\DI::value(function (&$out) use ($xssTesting) {
            $out .= $xssTesting->getJavaScriptCode();
        })];
    }

    /**
     * Since the XSS entries option is stored in the OmniFixture dump AND is modified when setting up
     * UITestFixture, we want to make sure it has all the right entries. Otherwise some failures will be
     * harder to debug.
     */
    public function sanityCheck()
    {
        $expectedEntries = [
            'twig-(site name)',
            'twig-(goal name)',
            'twig-(goal description)',
            'vuejs-(Piwik test two)',
            'vuejs-(second goal)',
            'vuejs-(goal description)',
            'twig-(pageurl)',
            'twig-(page title)',
            'twig-(referrerUrl)',
            'twig-(keyword)',
            'twig-(customdimension)',
            'twig-(customvarname)',
            'twig-(customvarval)',
            'twig-(userid)',
            'twig-(lang)',
            'twig-(city)',
            'twig-(region)',
            'twig-(country)',
            'vuejs-(pageurl)',
            'vuejs-(page title)',
            'vuejs-(referrerUrl)',
            'vuejs-(keyword)',
            'vuejs-(customdimension)',
            'vuejs-(customvarname)',
            'vuejs-(customvarval)',
            'vuejs-(userid)',
            'vuejs-(lang)',
            'vuejs-(city)',
            'vuejs-(region)',
            'vuejs-(country)',
            'twig-(annotation)',
            'vuejs-(Annotation note 3)',
            'twig-(excludedparameter)',
            'vuejs-(excludedparameter)',
            'twig-(scheduledreport)',
            'twig-(dimensionname)',
            'twig-(category)',
            'twig-(reportname)',
            'twig-(reportdoc)',
            'twig-(subcategory)',
            'twig-(processedmetricname)',
            'twig-(processedmetricdocs)',
            'vuejs-(dimensionname)',
            'vuejs-(category)',
            'vuejs-(reportname)',
            'vuejs-(reportdoc)',
            'vuejs-(subcategory)',
            'vuejs-(processedmetricname)',
            'vuejs-(processedmetricdocs)',
            'vuejs-(scheduledreport)',
            'twig-(segment)',
            'vuejs-(From Europe segment)',
            'twig-(dashboard name0)',
            'vuejs-(dashboard name1)',
            'vuejs-(datatablerow)',
            'twig-(datatablerow)',
        ];

        $actualEntries = $this->getXssEntries();
        $actualEntries = array_unique($actualEntries);
        $actualEntries = array_filter($actualEntries);
        $actualEntries = array_values($actualEntries);

        try {
            \PHPUnit\Framework\Assert::assertEquals($expectedEntries, $actualEntries);
        } catch (\Exception $ex) {
            print "XssTesting::sanityCheck() failed, got: " . var_export($actualEntries, true)
                . "\nexpected: " . var_export($expectedEntries, true);
        }
    }

    public function dangerousLink($desc)
    {
        return 'javascript:alert("' . $desc . '")';
    }
}