22
33namespace GlpiPlugin \Formcreator \Tests ;
44use PluginFormcreatorForm ;
5+ use PluginFormcreatorFormAnswer ;
6+ use PluginFormcreatorSection ;
57
68abstract class CommonTargetTestCase extends CommonTestCase
79{
10+ public function beforeTestMethod ($ method ) {
11+ parent ::beforeTestMethod ($ method );
12+ switch ($ method ) {
13+ case 'testXSS ' :
14+ $ this ->login ('glpi ' , 'glpi ' );
15+ break ;
16+ }
17+ }
18+
819 /**
920 * Test handling of uuid when adding an item
1021 */
@@ -56,4 +67,35 @@ public function testPrepareInputForUpdate_uuid() {
5667 $ this ->array ($ output )->HasKey ('uuid ' );
5768 $ this ->string ($ output ['uuid ' ])->isEqualTo ('foo ' );
5869 }
59- }
70+
71+ public function testXSS () {
72+ $ question = $ this ->getQuestion ([
73+ 'fieldtype ' => 'text ' ,
74+ ]);
75+ $ section = new PluginFormcreatorSection ();
76+ $ section ->update ([
77+ 'id ' => $ question ->fields ['plugin_formcreator_sections_id ' ],
78+ 'name ' => 'section ' ,
79+ ]);
80+ $ form = PluginFormcreatorForm::getByItem ($ question );
81+ $ testedClassName = $ this ->getTestedClassName ();
82+ $ target = new $ testedClassName ();
83+ $ target ->add ([
84+ 'name ' => $ this ->getUniqueString (),
85+ 'plugin_formcreator_forms_id ' => $ form ->getID (),
86+ 'target_name ' => '##answer_ ' . $ question ->getID () . '## ' ,
87+ 'content ' => '##FULLFORM## ' ,
88+ ]);
89+ $ formAnswer = new PluginFormcreatorFormAnswer ();
90+ $ formAnswer ->add ([
91+ 'plugin_formcreator_forms_id ' => $ form ->getID (),
92+ 'formcreator_field_ ' . $ question ->getID () => '"><img src=x onerror="alert(1337)" x=x>" ' ,
93+ ]);
94+ $ generated = $ formAnswer ->targetList [0 ] ?? null ;
95+ $ this ->object ($ generated );
96+ $ this ->string ($ generated ->fields ['name ' ])
97+ ->isEqualTo ('"><img src=x onerror="alert(1337)" x=x>" ' );
98+ $ this ->string ($ generated ->fields ['content ' ])
99+ ->isEqualTo ('<h1>Form data</h1><h2>section</h2><div><b>1) question : </b>"&#62;&#60;img src=x onerror="alert(1337)" x=x&#62;"</div> ' );
100+ }
101+ }
0 commit comments