99import com .cybersource .flex .models .KeyResult ;
1010import com .cybersource .flex .vdp .VDPEnpoints ;
1111import java .security .PublicKey ;
12+ import java .util .HashMap ;
1213import java .util .Map ;
1314import javax .annotation .PostConstruct ;
15+ import javax .servlet .http .HttpServletResponse ;
1416import javax .servlet .http .HttpSession ;
1517import org .springframework .beans .factory .annotation .Autowired ;
1618import org .springframework .beans .factory .annotation .Value ;
1719import org .springframework .stereotype .Controller ;
1820import org .springframework .ui .Model ;
21+ import org .springframework .web .bind .annotation .ModelAttribute ;
1922import org .springframework .web .bind .annotation .RequestMapping ;
2023import org .springframework .web .bind .annotation .RequestMethod ;
2124import org .springframework .web .bind .annotation .RequestParam ;
@@ -42,8 +45,14 @@ private void postConstruct() {
4245 restTemplate .getMessageConverters ().add (0 , new KeyParametersMessageConverter (apiKey , sharedSecret ));
4346 }
4447
48+ @ ModelAttribute
49+ public void setFramingResponseHeader (HttpServletResponse response ) {
50+ response .setHeader ("X-Frame-Options" , "DENY" );
51+ }
52+
4553 @ RequestMapping ("/" )
46- String redirect () {
54+ String redirect (final HttpSession session ) {
55+ session .invalidate ();
4756 return "redirect:checkout" ;
4857 }
4958
@@ -63,7 +72,9 @@ String checkout(final HttpSession session, final Model model) {
6372 }
6473
6574 @ RequestMapping (value = "/receipt" , method = RequestMethod .POST )
66- String receipt (@ RequestParam final Map <String , Object > postParams , final HttpSession session , final Model model ) {
75+ String receipt (@ RequestParam Map <String , Object > postParams , final HttpSession session , final Model model ) {
76+ postParams = validateUntrustedParameters (postParams );
77+
6778 // Read in the public key to use and remove it from the session
6879 PublicKey flexPublicKey = (PublicKey ) session .getAttribute ("flexPublicKey" );
6980 session .removeAttribute ("flexPublicKey" ); // no longer needed
@@ -89,9 +100,18 @@ String receipt(@RequestParam final Map<String, Object> postParams, final HttpSes
89100 * For demonstration purposes, all post parameters are added to the view
90101 * model to display data received from cardholder's browser.
91102 */
92-
93103 model .addAttribute ("postParams" , postParams );
94104 return "receipt" ;
95105 }
96106
107+ private Map <String , Object > validateUntrustedParameters (Map <String , Object > parameters ) {
108+ Map <String , Object > retVal = new HashMap <>();
109+ // Each parameter must undergo proper validation / sanitization.
110+ // The type of validation to be implemented will vary between individual
111+ // Flex API integrations. It is merchant's responsibility to implement adequate
112+ // parameter validation for production deployments.
113+ parameters .forEach ((k , v ) -> retVal .put (k , v ));
114+ return retVal ;
115+ }
116+
97117}
0 commit comments