Skip to content

Commit 7de7ae9

Browse files
authored
Add examples for cross-site-scripting (#60)
1 parent fce42e4 commit 7de7ae9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package detectors.cross_site_scripting;
7+
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.servlet.ModelAndView;
10+
11+
public class CrossSiteScripting {
12+
13+
// {fact rule=cross-site-scripting@v1.0 defects=1}
14+
public ModelAndView inputSanitizationNonCompliant(@RequestParam String favoriteColor) {
15+
ModelAndView modelAndView = new ModelAndView();
16+
modelAndView.setViewName("jsp/example.jsp");
17+
// Noncompliant: user-supplied parameter might contain malicious content.
18+
modelAndView.addObject("preferredColor", favoriteColor);
19+
return modelAndView;
20+
}
21+
// {/fact}
22+
23+
// {fact rule=cross-site-scripting@v1.0 defects=0}
24+
public ModelAndView inputSanitizationCompliant(@RequestParam String favoriteColor) {
25+
ModelAndView modelAndView = new ModelAndView();
26+
modelAndView.setViewName("jsp/example.jsp");
27+
// Compliant: user-supplied parameter must be in allow-list.
28+
if (favoriteColor.matches("[a-z]+")) {
29+
modelAndView.addObject("preferredColor", favoriteColor);
30+
} else {
31+
throw new IllegalArgumentException("Invalid color!");
32+
}
33+
return modelAndView;
34+
}
35+
// {/fact}
36+
37+
}

0 commit comments

Comments
 (0)