Repository navigation
Expand file tree
/
Copy pathFindXSSTest.java
More file actions
166 lines (130 loc) · 4.24 KB
/
Copy pathFindXSSTest.java
File metadata and controls
166 lines (130 loc) · 4.24 KB
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
package utils;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class FindXSSTest {
// search — script tag detection
@Test
void search_detectsScriptTagWithAlert() {
assertTrue(FindXSS.search("<script>alert(1)</script>"));
}
@Test
void search_scriptTagWithoutAlertNotDetected() {
assertFalse(FindXSS.search("<script>console.log(1)</script>"));
}
@Test
void search_cleanHtmlNotDetected() {
assertFalse(FindXSS.search("<p>Hello World</p>"));
}
@Test
void search_emptyStringNotDetected() {
assertFalse(FindXSS.search(""));
}
@Test
void search_plainTextNotDetected() {
assertFalse(FindXSS.search("just some text"));
}
// search — event handler detection
@Test
void search_detectsOnclickAlert() {
assertTrue(FindXSS.search("<div onclick=\"alert(1)\">click</div>"));
}
@Test
void search_detectsOnerrorAlert() {
assertTrue(FindXSS.search("<img src=x onerror=\"alert(1)\">"));
}
@Test
void search_detectsOnmouseoverAlert() {
assertTrue(FindXSS.search("<span onmouseover=\"alert(1)\">hover</span>"));
}
@Test
void search_detectsOnpointeroverAlert() {
assertTrue(FindXSS.search("<div onpointerover=\"alert(1)\">MOVE HERE</div>"));
}
@Test
void search_detectsOnpointerenterAlert() {
assertTrue(FindXSS.search("<div onpointerenter=\"alert(1)\">hover</div>"));
}
@Test
void search_detectsOnanimationstartAlert() {
assertTrue(FindXSS.search("<div onanimationstart=\"alert(1)\">animation</div>"));
}
@Test
void search_detectsOntransitionendAlert() {
assertTrue(FindXSS.search("<div ontransitionend=\"alert(1)\">transition</div>"));
}
@Test
void search_detectsOnauxclickAlert() {
assertTrue(FindXSS.search("<div onauxclick=\"alert(1)\">click</div>"));
}
@Test
void search_eventWithoutAlertNotDetected() {
assertFalse(FindXSS.search("<div onclick=\"doSomething()\">click</div>"));
}
// search — URI-based detection
@Test
void search_detectsJavascriptUri() {
assertTrue(FindXSS.search("<a href=\"javascript:alert(1)\">link</a>"));
}
@Test
void search_detectsDataUri() {
assertTrue(FindXSS.search("<a href=\"data:text/html,<script>alert(1)</script>\">x</a>"));
}
@Test
void search_normalHrefNotDetected() {
assertFalse(FindXSS.search("<a href=\"http://example.com\">link</a>"));
}
// findCsrfAttackUrl — path-only validation
@Test
void findCsrfAttackUrl_validPath() {
assertTrue(FindXSS.findCsrfAttackUrl("http://localhost/admin/config", "/admin/config"));
}
@Test
void findCsrfAttackUrl_invalidPath() {
assertFalse(FindXSS.findCsrfAttackUrl("http://localhost/other/path", "/admin/config"));
}
@Test
void findCsrfAttackUrl_malformedUrl() {
assertFalse(FindXSS.findCsrfAttackUrl("not-a-url", "/admin/config"));
}
// findCsrfAttackUrl — path + query validation
@Test
void findCsrfAttackUrl_validPathAndQuery() {
assertTrue(
FindXSS.findCsrfAttackUrl(
"http://localhost/admin/config?userId=abc123", "/admin/config", "userId", "abc123"));
}
@Test
void findCsrfAttackUrl_wrongQueryValue() {
assertFalse(
FindXSS.findCsrfAttackUrl(
"http://localhost/admin/config?userId=wrong", "/admin/config", "userId", "abc123"));
}
@Test
void findCsrfAttackUrl_missingQuery() {
assertFalse(
FindXSS.findCsrfAttackUrl(
"http://localhost/admin/config", "/admin/config", "userId", "abc123"));
}
// findCsrf — img tag CSRF detection
@Test
void findCsrf_validImgCsrf() {
String msg = "<img src=\"http://localhost/root/grantComplete/csrflesson?userId=fakeId123\" />";
assertTrue(FindXSS.findCsrf(msg, "fakeId123"));
}
@Test
void findCsrf_noImgTag() {
assertFalse(FindXSS.findCsrf("<p>hello</p>", "fakeId"));
}
@Test
void findCsrf_imgWithWrongPath() {
String msg = "<img src=\"http://localhost/wrong/path?userId=fakeId\" />";
assertFalse(FindXSS.findCsrf(msg, "fakeId"));
}
@Test
void findCsrf_imgWithSpacesInTags() {
String msg =
"< img src=\"http://localhost/root/grantComplete/csrflesson?userId=fakeId123\" / >";
assertTrue(FindXSS.findCsrf(msg, "fakeId123"));
}
}