Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MaliciousCodeTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Filter\Input\MaliciousCode;
10 
11 class MaliciousCodeTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $filter;
15 
16  protected function setUp()
17  {
18  $this->filter = new MaliciousCode();
19  parent::setUp();
20  }
21 
27  public function testFilter($input, $expectedOutput)
28  {
29  $this->assertEquals(
30  $expectedOutput,
31  $this->filter->filter($input),
32  'Malicious code is not filtered out correctly.'
33  );
34  }
35 
39  public function filterDataProvider()
40  {
41  return [
42  'Comments' => ['Comment /** This is omitted */ is removed', 'Comment is removed'],
43  'Tabs' => ["Tabs \t\t are removed", 'Tabs are removed'],
44  'JS' => ['JS JavaScript : is removed', 'JS is removed'],
45  'Import' => ['Import @import directive is removed', 'Import directive is removed'],
46  'JS in styles (array of strings to be filtered)' => [
47  [
48  '<element style="behavior:url(malicious.example.com)"></element>',
49  '<img src="test.gif" style="height: expression(compatMode==\'CSS1Compat\'? 200px : 300px")/>',
50  ],
51  [
52  '<element ></element>',
53  '<img src="test.gif" />'
54  ],
55  ],
56  'JS attributes (array of strings to be filtered)' => [
57  [
58  '<element ondblclick="SomeJavaScriptCode">',
59  '<element onclick="SomeJavaScriptCode">',
60  '<element onkeydown="SomeJavaScriptCode">',
61  '<element onkeypress="SomeJavaScriptCode">',
62  '<element onkeyup="SomeJavaScriptCode">',
63  '<element onmousedown="SomeJavaScriptCode">',
64  '<element onmousemove="SomeJavaScriptCode">',
65  '<element onmouseout="SomeJavaScriptCode">',
66  '<element onmouseover="SomeJavaScriptCode">',
67  '<element onmouseup="SomeJavaScriptCode">',
68  '<element onload="SomeJavaScriptCode">',
69  '<element onunload="SomeJavaScriptCode">',
70  '<element onerror="SomeJavaScriptCode" />',
71  ],
72  [
73  '<element >',
74  '<element >',
75  '<element >',
76  '<element >',
77  '<element >',
78  '<element >',
79  '<element >',
80  '<element >',
81  '<element >',
82  '<element >',
83  '<element >',
84  '<element >',
85  '<element />',
86  ],
87  ],
88  'Prohibited tags (array of strings to be filtered)' => [
89  [
90  'Tag is removed <script>SomeScript</script>',
91  'Tag is removed <meta>SomeMeta</meta>',
92  'Tag is removed <link>SomeLink</link>',
93  'Tag is removed <frame>SomeFrame</frame>',
94  'Tag is removed <iframe>SomeIFrame</iframe>',
95  'Tag is removed <object>SomeObject</object>',
96  ],
97  [
98  'Tag is removed SomeScript',
99  'Tag is removed SomeMeta',
100  'Tag is removed SomeLink',
101  'Tag is removed SomeFrame',
102  'Tag is removed SomeIFrame',
103  'Tag is removed SomeObject',
104  ],
105  ],
106  'Base64' => [
107  '<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />',
108  '<img alt="Embedded Image" />',
109  ],
110  'Nested malicious tags' => [
111  '<scri<script>pt>alert(1);</scri<script>pt>',
112  'alert(1);',
113  ]
114  ];
115  }
116 
120  public function testAddExpression()
121  {
122  $customExpression = '/<\/?(customMalicious).*>/Uis';
123  $this->filter->addExpression($customExpression);
124  $this->assertEquals(
126  'Custom malicious tag is removed customMalicious',
127  $this->filter->filter(
128  "Custom \tmalicious tag\t\t is removed <customMalicious>customMalicious</customMalicious>"
129  ),
130  'Custom filters are not applied correctly.'
131  );
132  }
133 
137  public function testSetExpression()
138  {
139  $customExpression = '/<\/?(customMalicious).*>/Uis';
140  $this->filter->setExpressions([$customExpression]);
141  $this->assertEquals(
143  "Custom \tmalicious tag\t\t is removed customMalicious",
144  $this->filter->filter(
145  "Custom \tmalicious tag\t\t is removed <customMalicious>customMalicious</customMalicious>"
146  ),
147  'Native filters should have been replaced with custom ones.'
148  );
149  }
150 }