2
2
3
3
namespace GlpiPlugin \Formcreator \Tests ;
4
4
use PluginFormcreatorForm ;
5
+ use PluginFormcreatorFormAnswer ;
6
+ use PluginFormcreatorSection ;
5
7
6
8
abstract class CommonTargetTestCase extends CommonTestCase
7
9
{
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
+
8
19
/**
9
20
* Test handling of uuid when adding an item
10
21
*/
@@ -56,4 +67,35 @@ public function testPrepareInputForUpdate_uuid() {
56
67
$ this ->array ($ output )->HasKey ('uuid ' );
57
68
$ this ->string ($ output ['uuid ' ])->isEqualTo ('foo ' );
58
69
}
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